不适合所有BeanPostProcessors处理的类

时间:2012-09-10 20:15:56

标签: spring hibernate unit-testing jpa-2.0

在Maven中设置Spring + Spring Data JPA + QueryDSL + JPA 2.0 + Hibernate的配置时遇到了很多麻烦。我已经解决了很多问题,但是这个问题让我很头疼= /。

我遇到以下异常:

Bean 'dataSource' of type [class org.springframework.jdbc.datasource.DriverManagerDataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

我在谷歌看了一下,大多数时候这个问题是由缺少注释“@Transactional”引起的。我试图注释我的方法,但它没有解决任何问题。我绝对不知道它来自哪里=(。

这是我的测试代码:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/applicationContext.xml")
public class BaseTest {

    public BaseTest() {
    }

    @Before
    public void setUp() throws IOException {
        Dataset.loadDatabase();
    }

    @Test
    public void load(){}
}

类DataSet只是在数据库中执行一些保存以查看它是否正常工作。

我的ApplicationContext.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:jpa="http://www.springframework.org/schema/data/jpa"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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/data/jpa
       http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

        <context:annotation-config/>
        <jpa:repositories base-package="com.ae.repository" />
        <context:component-scan base-package="com.ae.service" />

        <!-- Data Source -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName"><value>com.mysql.jdbc.Driver</value></property>
            <property name="url"><value>jdbc:mysql://localhost:3306/academia</value></property>
            <property name="username"><value>root</value></property>
            <property name="password"><value>root</value></property>
        </bean>

        <!-- JPA EntityManagerFactory -->  
        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">  
            <property name="dataSource" ref="dataSource"/>  
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
                    <prop key="hibernate.id.new_generator_mappings">true</prop>
                </props>
            </property>
        </bean>  

        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">  
            <property name="entityManagerFactory" ref="entityManagerFactory"/>  
        </bean> 
        <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />
        <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/>   
        <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>  
</beans>

我不确定我的依赖项是否也正确设置:

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

        <!-- hibernate + jpa -->
        <dependency>  
            <groupId>org.hibernate</groupId>  
            <artifactId>hibernate-entitymanager</artifactId>  
            <version>4.1.6.Final</version>
        </dependency>  
        <dependency>  
            <groupId>org.slf4j</groupId>  
            <artifactId>slf4j-log4j12</artifactId>  
            <version>1.6.6</version>  
        </dependency> 
        <dependency>  
            <groupId>org.apache.derby</groupId>  
            <artifactId>derby</artifactId>  
            <version>10.3.2.1</version>  
            <scope>test</scope>  
        </dependency>

        <!-- Spring JPA -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>javax.sql</groupId>
            <artifactId>jdbc-stdext</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>

        <!-- MySQL Connector -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>

        <!-- Query DSL -->
        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-apt</artifactId>
            <version>2.7.2</version>
            <scope>provided</scope>
        </dependency>    

        <dependency>
            <groupId>com.mysema.querydsl</groupId>
            <artifactId>querydsl-jpa</artifactId>
            <version>2.7.2</version>
        </dependency>

        <!-- Spring Framework -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>

        <!-- Apache commons lang -->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>

        <!-- Apache commons IO -->
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>

        <!-- Other -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>3.1.2.RELEASE</version>
            <scope>test</scope>
            <type>jar</type>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>3.1.2.RELEASE</version>
        </dependency>
    </dependencies>

感谢您阅读;)

1 个答案:

答案 0 :(得分:22)

此警告没有任何意义,您不必担心,因为您不需要将任何后处理器应用于DataSource

从技术上讲,这意味着某些bean后处理器(我认为是事务性的)取决于你的DataSource,因此DataSource必须在初始化后处理器之前完全初始化,以便后处理器无法拦截DataSource的初始化。

如果您对要将后处理器应用到的bean有这样的警告,您需要担心。