Spring 3.1:DataSource未自动连接到@Configuration类

我正在使用Spring MVC 3.1.0M2并尝试将我的配置移动到java bean。 但我遇到以下错误:

2011-09-14 18:43:42.301:警告:/:不可用org.springframework.beans.factory.BeanCreationException:创建名为’org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration#0’的bean时出错:注入自动连接的依赖项失败; 嵌套exception是org.springframework.beans.factory.BeanCreationException:无法自动assembly方法:void org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration.setConfigurers(java.util.Collection); 嵌套exception是org.springframework.beans.factory.BeanCreationException:在类ru.mystamps.web.config.DbConfig中定义名为’entityManagerFactory’的bean时出错:bean的实例化失败; 嵌套exception是org.springframework.beans.factory.BeanDefinitionStoreException:工厂方法[public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean ru.mystamps.web.config.DbConfig.entityManagerFactory()]抛出exception; 嵌套exception是java.lang.IllegalArgumentException:DataSource不能为null

web.xml映射:

  spring.profiles.default dev   spring  org.springframework.web.servlet.DispatcherServlet   contextClass  org.springframework.web.context.support.AnnotationConfigWebApplicationContext    contextConfigLocation  ru.mystamps.web.config.MvcConfig, ru.mystamps.web.config.DbConfig   1  

DbConfig.java

 @Configuration @EnableTransactionManagement @ImportResource("classpath:spring/datasource.xml") public class DbConfig { @Autowired private DataSource dataSource; @Bean public JpaVendorAdapter jpaVendorAdapter() { final HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); jpaVendorAdapter.setDatabasePlatform(dialectClassName); jpaVendorAdapter.setShowSql(showSql); return jpaVendorAdapter; } @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory() { final LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean(); entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapter()); entityManagerFactory.setDataSource(dataSource); final Map jpaProperties = new HashMap(); jpaProperties.put("hibernate.format_sql", formatSql); jpaProperties.put("hibernate.connection.charset", "UTF-8"); jpaProperties.put("hibernate.hbm2ddl.auto", hbm2ddl); entityManagerFactory.setJpaPropertyMap(jpaProperties); return entityManagerFactory; } @Bean public PlatformTransactionManager transactionManager() { final JpaTransactionManager transactionManager = new JpaTransactionManager(); transactionManager.setEntityManagerFactory(entityManagerFactory().getObject()); return transactionManager; } ... } 

spring/datasource.xml

                 

我希望在导入datasource.xml之后创建bean dataSource但我总是遇到这个错误。

TIA

我发现错误原因,只有在我手动定义PersistenceAnnotationBeanPostProcessor时才会发生错误:

  @Bean public PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor() { // enable injection of EntityManager to beans with @PersistenceContext annotation return new PersistenceAnnotationBeanPostProcessor(); } 

对不起,因为我没有在问题中发布完整的代码(因为我认为这个bean没关系)。 当我删除此定义时,一切都按预期工作。 另外我发现在我的情况下这个bean已经注册了:

注意:默认的PersistenceAnnotationBeanPostProcessor将由“context:annotation-config”和“context:component-scan”XML标记注册。 如果要指定自定义PersistenceAnnotationBeanPostProcessor bean定义,请删除或关闭默认注释配置。

(引自org.springframework.orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java

我知道这不能回答实际问题,但为什么不使用注释定义数据源呢? 我有一个非常类似的设置使用没有XML,但没有尝试结合这两种方法。