创建名为“dietaController”的 bean 时出错

Posted

技术标签:

【中文标题】创建名为“dietaController”的 bean 时出错【英文标题】:Error creating bean with name 'dietaController' 【发布时间】:2020-05-16 16:17:34 【问题描述】:

所以,我有一个这样的问题: 2020-01-30 22:54:20.059 错误 8040 --- [main] os.boot.SpringApplication:应用程序运行失败

org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“dietaController”的bean时出错:通过字段“dietaRepository”表示的依赖关系不满足;嵌套异常是 org.springframework.beans.factory.BeanCreationException:创建名为“dietaRepository”的 bean 时出错:调用 init 方法失败;嵌套异常是 java.lang.IllegalArgumentException: Not a managed type: class com.diet4you.LapkoEkaterina.Entity.Dieta 我的 pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.diet4you</groupId>
    <artifactId>LapkoEkaterina</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>

    </parent>
    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
            <version>2.2.2.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.3.2.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.2.3.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate.validator</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.16.Final</version>
        </dependency>

        <dependency>
            <groupId>javax.xml.bind</groupId>
            <artifactId>jaxb-api</artifactId>
            <version>2.3.0</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

        </plugins>
    </build>
</project>

我的实体:

@Entity
@Table(name = "diety")
public class Dieta 
    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    @Column(name="DIETA_ID") private int dietaId;
    @NotEmpty
    @Column(unique=true, name ="DIETA_NAZWA")
    private String nazwa;
    @NotEmpty
    @Column(name="OPIS") private String opis;
    public Dieta() 
    public Dieta (int dietaId,String nazwa, String opis )
     this.dietaId = dietaId;
    this.nazwa = nazwa;
    this.opis = opis; 
    public int getDietaId() 
        return dietaId; 
        public void setDietaId(int dietaId) 
        this.dietaId = dietaId; 
        public String getNazwa() 
        return nazwa; 
        public void setNazwa(String nazwa) 
        this.nazwa = nazwa; 
        public String getOpis() 
        return opis; 
        public void setOpis(String opis) 
        this.opis = opis; 

我的仓库:

@Repository
public interface DietaRepository extends JpaRepository<Dieta, String> 
    Dieta findByName (String name);

还有我的控制器:

@RestController
public class DietaController 
    @Autowired
    private DietaRepository dietaRepository;
    @GetMapping("/dieta")
    public List<Dieta> getAllNotes() 
        return dietaRepository.findAll();
    


应用java类:

@Configuration
@SpringBootApplication
@EnableJpaRepositories
@EntityScan ( basePackages  =  " com.diet4you.LapkoEkaterina" )
public class Application 
    public static void main(String[] args) 
        SpringApplication.run(Application.class, args);
    

【问题讨论】:

我认为实体类不是自动检测的。请通过this question 我看到了,没用 如前所述,您的班级看不到您的存储库。在错误消息中很明显。你能在上面添加你的类的包吗?如果你有注释@SpringBootApplication 您是否在配置类中添加了类似@EntityScan(basePackages = "com.diet4you.LapkoEkaterina.Entity.") 的内容。你需要告诉spring boot这个实体必须被扫描才能被spring管理 我添加了课程:) 【参考方案1】:

    @EntityScan ( basePackages = " com.diet4you.LapkoEkaterina" )"com.diet4you... 之间删除空格

    extends JpaRepository&lt;Dieta, String&gt;更改为extends JpaRepository&lt;Dieta, Integer&gt;

    remove or fix Dieta findByName (String name) at repository interface,这是错误的

【讨论】:

以上是关于创建名为“dietaController”的 bean 时出错的主要内容,如果未能解决你的问题,请参考以下文章

使用spring security的spring boot:创建名为“securityFilterChainRegistration”的bean时出错

idea提示log4j:WARN No appenders could be found for logger

No bean named 'cxf' is defined

如何解决google colab中“没有名为'tools'的模块”?

创建 Django 模型时出现“parent_id may not be NULL”

使用子类操作类