多模块项目spring 3.0 AnnotationSessionFactoryBean packagesToScan属性值以模块方式设置

时间:2011-12-16 15:18:04

标签: hibernate spring session factory multi-module

我有以下结构:

共模块

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

搜索模块(取决于公共模块)

  • 包含与搜索模块相关的模型bean(标有JPA Entity annotations),服务和持久性api
  • 包含与搜索模块相关的弹簧上下文文件

预订模块(取决于公共模块)      - 包含预订模块相关的模型bean(标有JPA Entity annotations),服务和持久性api      - 包含与预订模块相关的春季上下文文件

在通用模块中,我有 test-common-persistence-context.xml 。对于具有类型的 sessionFactory bean org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 我需要将属性“packagesToScan”值设置为包含标记模型bean的JPA实体注释的包。没有它,我得到异常< i>未知实体: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定义

测试的搜索 - 弹簧 - 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

但是这没有用,我得到了例外未知实体:MY_ENTITY_NAME

谢谢,
Jignesh

1 个答案:

答案 0 :(得分: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

谢谢,
Jignesh