2

我有以下结构:

公共模块

  • 包含公共模块相关、服务和持久化api
  • 包含一个公共持久性上下文test-common-persistence-context.xml

搜索模块(取决于通用模块)

  • 包含搜索模块相关的模型bean(标有JPA Entity注解)、服务和持久化api
  • 包含搜索模块相关的 spring 上下文文件

booking-module (依赖于 Common Module) - 包含与预订模块相关的模型 bean(用 JPA Entity 注释标记)、服务和持久性 api - 包含与预订模块相关的 spring 上下文文件

在公共模块中,我有test-common-persistence-context.xml 。对于类型为org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBeansessionFactory bean, 我需要将属性“packagesToScan”值设置为 JPA 所在的包存在实体注释标记的模型 bean。没有它,我得到异常Unknown entity: MY_ENTITY_NAME

通用模块:test-common-persistence-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

            <!-- Property Place Holder -->
            <context:property-placeholder location="classpath:test-common-persistence-bundle.properties" />

            <!-- Data Source -->        
            <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <property name="driverClassName" value="${test.db.driverClassName}"/>
                <property name="url" value="${test.db.jdbc.url}"/>
                <property name="username" value="${test.db.username}"/>
                <property name="password" value="${test.db.password}"/>
            </bean>

            <!--
                Hibernate Configuration
            --> 
            <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
                <property name="dataSource" ref="dataSource"/>
                <property name="packagesToScan" value="${test.packages.to.scan.jpa.annotations}"/>
                <property name="hibernateProperties">
                    <value>
                        <!-- SQL dialect -->
                        hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
                        hibernate.hbm2ddl.auto=update
                    </value>
                </property>      
            </bean>

            <!-- Transaction Manager -->
            <bean id="txManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory" ref="sessionFactory"/>
            </bean>

            <tx:annotation-driven transaction-manager="txManager"/>

    </beans>

在我的公共模块中没有 JPA 实体,所以我没有要扫描的包,所以我将“packagesToScan”属性值保留为空

但是,我需要在我的搜索模块和预订模块测试 ( SearchPersistenceTestBase.java ) 中使用相同的持久性上下文,以便检测到相应模块中的 JPA 实体。

搜索模块:SearchPersistenceTestBase.java

    @Ignore
    @ContextConfiguration(locations = {
            "classpath:test-common-persistence-context.xml",
            "classpath:test-search-spring-context.xml"})
    @TransactionConfiguration(transactionManager="txManager", defaultRollback=true)
    public class SearchPersistenceTestBase extends AbstractTransactionalJUnit4SpringContextTests {

    }

有人可以指导我如何使用上面显示的设置来实现这种期望的行为吗?

**我尝试过的方法**

我想使用一个额外的 java.lang.String 类型 bean,它的值是从属性文件中设置的

 <bean id="entityPackagesToScan" class="java.lang.String">
     <constructor-arg value="${test.packages.to.scan.jpa.annotations}" />
  </bean>

其中test.packages.to.scan.jpa.annotations在test-common-persistence-bundle.properties中定义为空

然后我覆盖了test-search-spring-context.xml中的 bean 定义

测试搜索-spring-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<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:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

        <!-- Property Place Holder -->
        <context:property-placeholder location="classpath:test-search-bundle.properties" />

        .. context-component scan elements here

        <bean id="entityPackagesToScan" class="java.lang.String">
            <constructor-arg value="${test.packages.to.scan.jpa.annotations}" />
        </bean>
</beans>

其中test.packages.to.scan.jpa.annotations在 test-search-bundle.properties中定义为“com.search.model

但这没有用,我得到了异常Unknown entity: MY_ENTITY_NAME

谢谢,
吉涅什

4

1 回答 1

3

好的,我解决了这个问题。org.springframework.beans.factory.config.PropertyOverrideConfigurer是需要的。我在这里发布解决方案以供参考,以防有人遇到我在第一篇文章中提到的类似问题:

通用模块:test-common-persistence-context.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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:tx="http://www.springframework.org/schema/tx"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

            <!-- Property Place Holder -->
            <context:property-placeholder location="classpath:test-common-persistence-bundle.properties" />

            <!-- Data Source -->        
            <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <property name="driverClassName" value="${test.db.driverClassName}"/>
                <property name="url" value="${test.db.jdbc.url}"/>
                <property name="username" value="${test.db.username}"/>
                <property name="password" value="${test.db.password}"/>
            </bean>

            <!--
                Hibernate Configuration
            --> 
            <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" >
                <property name="dataSource" ref="dataSource"/>
                <!-- Not setting "packagesToScan" property here.As common-module doesn't contain any JPA entities -->
                <property name="hibernateProperties">
                    <value>
                        <!-- SQL dialect -->
                        hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
                        hibernate.hbm2ddl.auto=update
                    </value>
                </property>      
            </bean>

            <!-- Transaction Manager -->
            <bean id="txManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory" ref="sessionFactory"/>
            </bean>

            <tx:annotation-driven transaction-manager="txManager"/>

    </beans>

搜索模块:test-search-spring-context.xml

 <?xml version="1.0" encoding="UTF-8"?>
   <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:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

        .. context-component scan elements here

        <!-- 
        Holds the overridden value for the org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
        (id="sessionFactory" bean in test-common-persistence-context.xml (common module))
        "packagesToScan" property value set for search module.

        E.g. sessionFactory.packagesToScan=com.search.model.persistent 
        -->
    <context:property-override location="classpath:test-search-bundle.properties"/>
</beans>

搜索模块:test-search-bundle.properties

     ########### Packages to scan JPA annotation
     # This is used by org.springframework.beans.factory.config.PropertyOverrideConfigurer
     # to override   org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean's
     # packagesToScan property value which is different for each module i.e. 
     # common-module does not contain any persistent models
     # search-module and booking-module contains persistent models
     # which needs to be scanned by Spring container to detect them as JPA entities

     sessionFactory.packagesToScan=com.search.model.persistent

谢谢,
吉涅什

于 2011-12-19T08:51:20.920 回答