2016-07-21 154 views
0

我是Spring Boot的新手,我有一個簡單的應用程序,它有一個控制器和一個Thymeleaf模板。Spring Boot`org.springframework.beans.factory.BeanCreationException:創建bean注入自動裝配依賴項失敗;

當應用程序啓動時,爲了查看是否一切正常,應該運行一個名爲RepoTester的Spring ApplicationListener以檢查服務類的方法。該服務類調用PersonRepository的方法,它簡單地擴展了Spring的CrudRepository。但是我每次去嘗試運行應用程序,它屬於在給我的留言:

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)}

但我不明白爲什麼。

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("[email protected]"); 
    person = personService.save(person);   

Person類只是:

public class Person { 

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

隨着gettersetter爲每個屬性的方法。

的應用程序也有一個屬性文件名爲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 

服務類的代碼很簡單:

@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); 
    }  

和存儲庫是:

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> { 
} 

我的Maven文件是:

http://maven.apache.org/xsd/maven-4.0.0.xsd「> 4.0.0 webapps.city2016.micro RegistrationSpringBootThymeleaf1 1.0-快照 罐
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> 

任何人都可以建議我在哪裏錯了?

回答

0

我嘗試了所有已發佈的答案,但沒有成功,因此應用程序已通過手動編寫的DAO類轉換爲Spring JDBC,並且完美地工作。

雖然有關JPA的任何建議都會受到歡迎。

0

你需要註釋的人與JPA註釋:

@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() { ... } 
+0

不幸的是會出現同樣的問題。 –

+0

你註釋過所有的獲得者嗎?你是否也將EnableJpaRepositories放回配置文件? –

+0

即使實際變量的獲取者本身被註釋了,該項目仍然失敗。 –

0

你可以在這裏閱讀所有要求: http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-jpa-and-spring-data

或檢查,你已經做了在本教程中提到的一切: http://spring.io/guides/gs/accessing-data-jpa/

特別檢查您是否有:

  • 主類註釋與@Entity
  • @Id和類型的泛型類型的存儲庫(整數)相匹配註釋實體的主鍵註釋@SpringBootApplication
  • 實體類。
  • 您的實體和存儲庫類位於主類或更低級別的包級別。
+0

使用'@ SpringBootApplication'註釋的主類:是。使用'@ Entity'註釋的實體類:是。主鍵... :是的。您的實體和存儲庫類:是的。而且這個可憐的應用程序仍然失敗! –

相關問題