简体   繁体   English

Spring Boot`org.springframework.beans.factory.BeanCreationException:创建bean的错误注入自动绑定的依赖项失败;

[英]Spring Boot `org.springframework.beans.factory.BeanCreationException: Error creating bean Injection of autowired dependencies failed;

I am a Spring Boot newbie and I have a simple application which has a controller and a Thymeleaf template. 我是一名Spring Boot新手,我有一个简单的应用程序,其中包含控制器和Thymeleaf模板。

When the application starts, in order to see if everything is working, a Spring ApplicationListener called RepoTester should run to check the methods of a service class. 当应用程序启动时,为了查看是否一切正常,应该运行一个名为RepoTester的Spring ApplicationListener来检查服务类的方法。 The service class calls methods of the PersonRepository which simply extends Spring's CrudRepository . 服务类调用PersonRepository方法,该方法只是扩展了Spring的CrudRepository But every time I go to try running the application, it falls over giving me the message: 但是,每次我尝试运行该应用程序时,它都会给我以下信息:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repoTester': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private webapps.city2016.micro.code.service.PersonService webapps.city2016.micro.code.bootstrap.RepoTester.personService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private webapps.city2016.micro.code.repo.PersonRepository webapps.city2016.micro.code.service.PersonServiceImpl.personRepository; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [webapps.city2016.micro.code.repo.PersonRepository] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

But I cannot see why. 但我不明白为什么。

The ApplicationListener class is simply: ApplicationListener类很简单:

@Component
public class RepoTester implements ApplicationListener<ContextRefreshedEvent> {

@Autowired
private PersonService personService;

private static final Logger logger = Logger.getLogger(RepoTester.class);   

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
    logger.info(RepoTester.class.getName() + ".onApplicationEvent() method called."); 

    // Create Person.
    Person person = new Person();
    person.setName("Cleo Markham");
    person.setAddress("48 Alysbury Road");
    person.setTelephone("34561287");
    person.setEmail("c.laithwaite@collosus.net");
    person = personService.save(person);        

The Person class is simply: Person类很简单:

public class Person {

    // Attributes.    
    private Integer personId;
    private String name;
    private String address;
    private String telephone;
    private String email;

With getter and setter methods for each attribute. 每个属性都有gettersetter方法。

The application also has a properties file called application.properties specifying the Derby database to use: 该应用程序还有一个名为application.properties的属性文件,该文件指定要使用的Derby数据库:

spring.datasource.driverClassName=org.apache.derby.jdbc.ClientDriver
spring.datasource.url=jdbc:derby://localhost:1527/Library
spring.datasource.username=username
spring.datasource.password=password

The code of the service class is simply: 服务类的代码很简单:

@Service()
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonRepository personRepository;

    private static final Logger logger = Logger.getLogger(PersonServiceImpl.class);   

    public PersonServiceImpl() {
    }

    @Override
    public void delete(Integer personId) {
        logger.info(PersonServiceImpl.class.getName() + ".delete() method called."); 

        personRepository.delete(personId);
    }

    @Override
    public boolean exists(Integer personId) {
        logger.info(PersonServiceImpl.class.getName() + ".exists() method called."); 

        return personRepository.exists(personId);
    }

    @Override
    public Person findOne(Integer personId) {
        logger.info(PersonServiceImpl.class.getName() + ".findOne() method called."); 

        return personRepository.findOne(personId);
    }

    @Override
    public Iterable<Person> findAll() {
        logger.info(PersonServiceImpl.class.getName() + ".findAll() method called."); 

        return personRepository.findAll();
    }

    @Override
    public Person save(Person person) {
        logger.info(PersonServiceImpl.class.getName() + ".save() method called."); 

        return personRepository.save(person);
    }    

And the repository is: 仓库是:

package webapps.city2016.micro.code.repo;

import org.springframework.data.repository.CrudRepository;

import webapps.city2016.micro.code.model.Person;

public interface PersonRepository extends CrudRepository<Person, Integer> {
} 

My Maven file is: 我的Maven文件是:

http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 webapps.city2016.micro RegistrationSpringBootThymeleaf1 1.0-SNAPSHOT jar http://maven.apache.org/xsd/maven-4.0.0.xsd“> 4.0.0 webapps.city2016.micro注册SpringBootThymeleaf1 1.0-SNAPSHOT jar
UTF-8 1.8 1.8 UTF-8 1.8 1.8

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
    <relativePath />
</parent>

<dependencies>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derbyclient</artifactId>
        <version>10.12.1.1</version>
    </dependency>      
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>  
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>                    
</dependencies>

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

<name>RegistrationSpringBootThymeleaf1</name>

Can anyone advise where I am wrong? 谁能告诉我我错了吗?

I tried both of the answers posted but without success so the application has been converted over to Spring JDBC with a manually written DAO class and it works perfectly. 我尝试了发布的两个答案,但都没有成功,因此该应用程序已通过手动编写的DAO类转换为Spring JDBC,并且运行良好。

Any advice concerning JPA would be welcome though. 任何有关JPA的建议都将受到欢迎。

You need to annotate Person with JPA annotation: 您需要使用JPA注释对Person进行注释:

@Entity
@Table(name = "person")
public class Person {

    // Attributes.    
    private Integer personId;
    private String name;
    private String address;
    private String telephone;
    private String email;

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Integer getPersonId() { ... }

    @Basic
    @Column(name = "name")
    public String getName() { ... }

You can read all the requirements here: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-jpa-and-spring-data 您可以在此处阅读所有要求: http : //docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-jpa-and-spring-data

Or check that you have done everything that's mentioned in this tutorial: http://spring.io/guides/gs/accessing-data-jpa/ 或检查您是否已完成本教程中提到的所有内容: http : //spring.io/guides/gs/accessing-data-jpa/

Particularly check that you have: 特别检查一下您是否具有:

  • Main class annotated with @SpringBootApplication @SpringBootApplication注释的主类
  • Entity class annotated with @Entity @Entity注释的实体类
  • The primary key on the entity annotated with @Id and that the type matches the generics type of your Repository (Integer). @Id注释的实体上的主键,其类型与存储库(通用)的泛型类型匹配。
  • Your entity and repository classes are at the package level of the main class or below. 您的实体和存储库类位于主类或以下的包级别。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.springframework.beans.factory.BeanCreationException:创建名称为&#39;appConfig&#39;的bean时出错:自动连接依赖项的注入失败 - org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'appConfig': Injection of autowired dependencies failed org.springframework.beans.factory.BeanCreationException:创建名称为&#39;playerDAOImpl&#39;的bean时出错:自动连接依赖项的注入失败 - org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'playerDAOImpl': Injection of autowired dependencies failed org.springframework.beans.factory.BeanCreationException:创建名称为&#39;userController&#39;的bean时出错:自动连接依赖项的注入失败 - org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed org.springframework.beans.factory.BeanCreationException:创建名为“flickrConfiguration”的bean时出错:注入自动装配的依赖项 - org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flickrConfiguration': Injection of autowired dependencies org.springframework.beans.factory.BeanCreationException:自动连接依赖项的注入失败; - org.springframework.beans.factory.BeanCreationException: Injection of autowired dependencies failed; org.springframework.beans.factory.BeanCreationException:在 Spring 中创建 bean 时出错 - org.springframework.beans.factory.BeanCreationException: Error creating bean in Spring Spring Boot 项目“org.springframework.beans.factory.BeanCreationException:创建名为‘entityManagerFactory’的 bean 时出错”错误 - Spring Boot Project "org.springframework.beans.factory.BeanCreationException:Error creating bean with name 'entityManagerFactory'" ERROR 错误org.springframework.beans.factory.BeanCreationException:创建bean Spring Boot时出错 - Error org.springframework.beans.factory.BeanCreationException: Error creating bean Spring Boot Spring 启动 BookStore 项目“org.springframework.beans.factory.BeanCreationException:创建名为“bookStoreRepo”的 bean 时出错” - Spring Boot BookStore Project "org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookStoreRepo'" org.springframework.beans.factory.BeanCreationException:org.springframework.beans.factory.BeanCreationException:创建bean时出错 - org.springframework.beans.factory.BeanCreationException:org.springframework.beans.factory.BeanCreationException: Error creating bean
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM