微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Spring 3.1:DataSource没有自动连接到@Configuration类

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

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;
    }

    ...
}

春/ 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

解决方法

我发现错误原因,只有在我手动定义PersistenceAnnotationBeanPostProcessor时才会发生错误
@Bean
   public PersistenceAnnotationBeanPostProcessor persistenceAnnotationBeanPostProcessor() {
           // enable injection of EntityManager to beans with @PersistenceContext annotation
           return new PersistenceAnnotationBeanPostProcessor();
   }

对不起,因为我没有在问题中发布完整的代码(因为我认为这个bean没关系).当我删除此定义时,一切都按预期工作.另外我发现在我的情况下这个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评论)

原文地址:https://www.jb51.cc/java/128433.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐