我是一个Spring Boot新手,我有一个简单的应用程序,它有一个控制器和一个Thymeleaf模板。
当应用程序启动时,为了查看是否一切正常,应运行一个名为ApplicationListener
的Spring RepoTester
来检查服务类的方法。服务类调用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("c.laithwaite@collosus.net");
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-SNAPSHOT
罐子
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 :(得分:0)
我尝试了两个已发布但没有成功的答案,因此应用程序已经通过手动编写的DAO类转换为Spring JDBC,并且运行正常。
欢迎任何有关JPA的建议。
答案 1 :(得分:0)
您需要使用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() { ... }
答案 2 :(得分: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/
特别检查您是否有:
@SpringBootApplication
@Entity
@Id
注释的实体上的主键,并且该类型与存储库(整数)的泛型类型匹配。