2011-09-14 115 views
5

我正在使用Spring MVC 3.1.0M2并试图将我的配置移至java bean。但是,我遇到以下错误:Spring 3.1:DataSource未自动装配到@Configuration类

2011-09-14 18:43:42.301:WARN:/:unavailable org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration#0': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: void org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration.setConfigurers(java.util.Collection); nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class ru.mystamps.web.config.DbConfig: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean ru.mystamps.web.config.DbConfig.entityManagerFactory()] threw exception; nested exception is java.lang.IllegalArgumentException: DataSource must not be null

映射从web.xml

<context-param> 
    <param-name>spring.profiles.default</param-name> 
    <param-value>dev</param-value> 
</context-param> 

<servlet> 
    <servlet-name>spring</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <init-param> 
     <param-name>contextClass</param-name> 
     <param-value> 
      org.springframework.web.context.support.AnnotationConfigWebApplicationContext 
     </param-value> 
    </init-param> 
    <init-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value> 
      ru.mystamps.web.config.MvcConfig, 
      ru.mystamps.web.config.DbConfig 
     </param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

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<String, String> jpaProperties = new HashMap<String, String>(); 
     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

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd 
    http://www.springframework.org/schema/jdbc 
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd"> 

    <context:property-placeholder location="classpath:spring/database.properties" /> 

    <beans profile="dev"> 
     <bean id="dataSource" 
      class="org.apache.commons.dbcp.BasicDataSource" 
      destroy-method="close"> 
      <property name="driverClassName" value="${db.driverClassName}" /> 
      <property name="url" value="${db.url}" /> 
      <property name="username" value="${db.username}" /> 
      <property name="password" value="${db.password}" /> 
     </bean> 
    </beans> 

    <beans profile="test"> 
     <jdbc:embedded-database id="dataSource" type="HSQL"> 
      <jdbc:script location="classpath:test-data.sql" /> 
     </jdbc:embedded-database> 
    </beans> 

</beans> 

我前在导入datasource.xml之后将会创建这个bean dataSource,但我总是得到这个错误。

TIA

+0

也许东西是错误的配置文件?你有没有试过运行它? – axtavt

+0

@axtavt当我删除配置文件,并保留'dataSource'与'jdbc:embedded-database'我有同样的错误。 –

回答

3

我发现错误的原因,当我手动定义只出现PersistenceAnnotationBeanPostProcessor

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

我很抱歉,因为我没有发布完整的代码在我的问题(因为我认为,这个Bean不物)。当我删除这个定义时,所有的工作如预期。此外,我发现,在我的情况下,该豆已注册:

Note: A default PersistenceAnnotationBeanPostProcessor will be registered by the "context:annotation-config" and "context:component-scan" XML tags. Remove or turn off the default annotation configuration there if you intend to specify a custom PersistenceAnnotationBeanPostProcessor bean definition.

(从org.springframework.orm/src/main/java/org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor.java报价评论)

0

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

+0

我试过了 - 静态内部类和单独的类 - 没有成功,仍然是同样的错误。 (如果它不是答案,那么为什么不把它作为注释发布呢:)) –

+0

你可以发布你的尝试在Java代码中配置数据源吗? –

+0

看到那里:http://pastebin.ubuntu.com/689938/我也尝试使用分离的类没有运气。当它工作的时候有一点 - 当我在DbConfig类中定义一个'DataSource'时(没有配置文件和任何类)。 –