Spring Data JPA,Spring 3.1.1,无xml配置,不会创建存储库bean

时间:2012-05-09 21:28:12

标签: spring jpa spring-3 servlet-3.0 spring-data-jpa

尝试使用 Spring Data JPA 使用<repositories />自动生成DAO对象,其中base-package链接包含DAO接口的包,如:

public interface UserDAO extends JpaRepository<User, String> {
}

但它无法连接服务bean中的DAO对象,确切的错误是:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ACLService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private pkg.service.UserServ pkg.service.ACLServ.userServ; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServ': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private pkg.repositories.UserDAO pkg.service.UserServ.userDAO; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [pkg.repositories.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:287)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at pkg.config.WebAppInit.onStartup(WebAppInit.java:35)

应用程序引导程序从 WebAppInit.Java 开始,因为它实现了WebApplicationInitializer接口,web.xml代码是:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"></web-app>

WebAppInit.Java 将onStartup方法编码为:

public class WebAppInit implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
    AnnotationConfigWebApplicationContext context  = 
        new AnnotationConfigWebApplicationContext();
    context.register(ApplicationContextConfig.class);
    context.refresh();
    ...

然后 ApplicationContextConfig 类使用@Configuration注释,代码为:

@Configuration
@PropertySource("classpath:application.properties")
@ImportResource("classpath:*springDataConfig.xml")
@Import({BasicDataSourceConfig.class,PersistenceSpringDataJpaConfig.class})
@ComponentScan(basePackages={"pkg.service","pkg.utils"})
public class ApplicationContextConfig {
}

所以这只是Java配置的主要/入口点,然后它遵循 application.properties (不包括但只是要求它),然后是 springDataConfig.xml 代码:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/data/jpa"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa
    http://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

  <repositories base-package="pkg.repositories" />

</beans:beans>

BasicDataSourceConfig.Java 将DataSource @Bean配置为:

@Bean
public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    ...

PersistenceSpringDataJpaConfig.Java LocalContainerEntityManagerFactoryBean 配置为:

@Configuration
public class PersistenceSpringDataJpaConfig {
    ...

    @Autowired
    DataSource dataSource;

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(){
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    ...

如果我切断了删除/评论代码的依赖关系,其他文件没有直接关系:

@Autorire 
private UserDAO userDAO
UserService类中的

;应用程序运行没有错误,我的意思是除了访问服务bean中的dao对象时的空指针异常。

所以问题是: 为什么Spring Data JPA不会创建userDAO bean?

PS:我故意摆脱所有@Transactions管理层来简化它,除了它应该没有交易工作,不是吗?

0 个答案:

没有答案