我是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;
隨着getter
和setter
爲每個屬性的方法。
的應用程序也有一個屬性文件名爲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>
任何人都可以建議我在哪裏錯了?
不幸的是會出現同樣的問題。 –
你註釋過所有的獲得者嗎?你是否也將EnableJpaRepositories放回配置文件? –
即使實際變量的獲取者本身被註釋了,該項目仍然失敗。 –