【问题标题】:Spring 3.1: DataSource not autowired to @Configuration classSpring 3.1:DataSource 未自动连接到 @Configuration 类
【发布时间】:2011-11-16 23:28:50
【问题描述】:

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

2011-09-14 18:43:42.301:WARN:/:unavailable org.springframework.beans.factory.BeanCreationException:创建名为 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration#0' 的 bean 时出错:注入自动装配依赖失败;嵌套异常是 org.springframework.beans.factory.BeanCreationException:无法自动装配方法:void org.springframework.transaction.annotation.AbstractTransactionManagementConfiguration.setConfigurers(java.util.Collection);嵌套异常是 org.springframework.beans.factory.BeanCreationException:在类 ru.mystamps.web.config.DbConfig 中创建名称为“entityManagerFactory”的 bean 时出错:bean 的实例化失败;嵌套异常是 org.springframework.beans.factory.BeanDefinitionStoreException: 工厂方法 [public org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean ru.mystamps.web.config.DbConfig.entityManagerFactory()] 抛出异常;嵌套异常是 java.lang.IllegalArgumentException: DataSource 不能为 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

【问题讨论】:

  • 配置文件可能有问题?你试过在没有它们的情况下运行它吗?
  • @axtavt 当我删除配置文件并只留下dataSourcejdbc:embedded-database 时,我遇到了同样的错误。

标签: java spring spring-mvc


【解决方案1】:

我找到了错误原因,只有在我手动定义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的评论)

【讨论】:

    猜你喜欢
    • 2015-10-05
    • 2018-04-27
    • 1970-01-01
    • 2011-12-31
    • 2020-05-03
    • 1970-01-01
    • 2012-11-15
    • 2019-03-21
    • 2019-07-21
    相关资源
    最近更新 更多