Spring JPA 数据存储库无法为扩展 CrudRepository 的接口创建 bean
Spring JPA Data Repository failed to create bean for interface that extends CrudRepository
我在使用 Spring JPA 存储库时遇到问题。我创建了:
- 一个基本用户域class (
@Entity
),
- 一个接口
UserDao
扩展了 CrudRepository
- 和服务层实现。
当我运行项目时,由于UserDao
的bean创建异常而失败。据我所知,Spring JPA 存储库有责任为此接口创建 bean(因为它扩展了 CrudRepository
)并将其注入到需要的地方。
这是我遇到的错误:
WARN :
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
- Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'UserController': Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: private edu.sjsu.services.UserService
edu.sjsu.controllers.UserController.userService; nested exception is
org.springframework.beans.factory.BeanCreationException: Error
creating bean with name 'userServiceImpl': Injection of autowired
dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field: edu.sjsu.models.UserDao
edu.sjsu.services.UserServiceImpl.userDao; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No
qualifying bean of type [edu.sjsu.models.UserDao] 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)}
at
org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
UserDao.Java:
package edu.sjsu.models;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
public interface UserDao extends CrudRepository<User, Long> {}
RootConfig.Java:
@Configuration
@ComponentScan(basePackages={"edu.sjsu"}, excludeFilters={@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)})
@Import(JpaConfig.class)
public class RootConfig {}
JpaConfig.java
我认为如果使用 Spring JPA 我什至不需要这个,但我仍然按照指南和教程创建了这个配置 class。
@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
public class JpaConfig {
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.HSQL).build();
}
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("edu.sjsu");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
}
如果您需要其他配置和 java class 了解情况,请告诉我。
在EnableJpaRepositories
中使用basePackages
:
@Configuration
@EnableJpaRepositories(basePackages = "edu.sjsu.models")
@EnableTransactionManagement
public class JpaConfig { ... }
默认情况下,EnableJpaRepositories
将为 Spring 个数据存储库扫描注释配置 class 的包。因为,我猜,你的配置和存储库 classes 在不同的包中,你应该告诉 Spring Data JPA 要扫描哪个基础包以找到 JPA 存储库。
我在使用 Spring JPA 存储库时遇到问题。我创建了:
- 一个基本用户域class (
@Entity
), - 一个接口
UserDao
扩展了CrudRepository
- 和服务层实现。
当我运行项目时,由于UserDao
的bean创建异常而失败。据我所知,Spring JPA 存储库有责任为此接口创建 bean(因为它扩展了 CrudRepository
)并将其注入到需要的地方。
这是我遇到的错误:
WARN : org.springframework.web.context.support.AnnotationConfigWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'UserController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private edu.sjsu.services.UserService edu.sjsu.controllers.UserController.userService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: edu.sjsu.models.UserDao edu.sjsu.services.UserServiceImpl.userDao; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [edu.sjsu.models.UserDao] 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)} at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
UserDao.Java:
package edu.sjsu.models;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.CrudRepository;
public interface UserDao extends CrudRepository<User, Long> {}
RootConfig.Java:
@Configuration
@ComponentScan(basePackages={"edu.sjsu"}, excludeFilters={@Filter(type=FilterType.ANNOTATION, value=EnableWebMvc.class)})
@Import(JpaConfig.class)
public class RootConfig {}
JpaConfig.java
我认为如果使用 Spring JPA 我什至不需要这个,但我仍然按照指南和教程创建了这个配置 class。
@Configuration
@EnableJpaRepositories
@EnableTransactionManagement
public class JpaConfig {
@Bean
public DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.HSQL).build();
}
@Bean
public EntityManagerFactory entityManagerFactory() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan("edu.sjsu");
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public PlatformTransactionManager transactionManager() {
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
}
如果您需要其他配置和 java class 了解情况,请告诉我。
在EnableJpaRepositories
中使用basePackages
:
@Configuration
@EnableJpaRepositories(basePackages = "edu.sjsu.models")
@EnableTransactionManagement
public class JpaConfig { ... }
默认情况下,EnableJpaRepositories
将为 Spring 个数据存储库扫描注释配置 class 的包。因为,我猜,你的配置和存储库 classes 在不同的包中,你应该告诉 Spring Data JPA 要扫描哪个基础包以找到 JPA 存储库。