首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加额外的包扫描到实体管理器使用Spring配置?

添加额外的包扫描到实体管理器使用Spring配置?
EN

Stack Overflow用户
提问于 2013-01-16 19:47:17
回答 3查看 13.1K关注 0票数 4

我有一个Spring批处理应用程序,它有一个Spring上下文配置,通常每个批处理作业都会引用这个配置。这样,每个批处理作业都使用相同的实体管理器。

批处理-context.xml:

代码语言:javascript
复制
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <!-- ... -->

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="myPersistenceUnit" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="packagesToScan" value="com.example.domain" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider</prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="hibernate.jdbc.fetch_size">100</prop>
                <prop key="hibernate.jbc.batch_size">1000</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.use_sql_comments">false</prop>
            </props>
        </property>
    </bean>

    <!-- ... -->

</beans>

现在,在我的特定批处理作业上下文(称为ExampleBatch.xml)中,我希望添加另一个包来扫描到已经定义的entityManagerFactory bean。这个是可能的吗?

ExampleBatch.xml:

代码语言:javascript
复制
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">

    <!-- ... -->    

    <import resource="classpath:batch-context.xml" />

    <bean id="entityManagerFactoryWithExtraPackages"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
        parent="entityManagerFactory">

        <!-- How do I override the packagesToScan property on the already defined entityManagerFactory bean?-->
        <property 
            name="packagesToScan" 
            value ="com.example.domain,com.example.domain.abstraction"
        />
    </bean>

    <!-- ... -->

</beans>

我现在拥有它的方式是行不通的,因为它抱怨"No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2

在这种情况下,试图覆盖"packagesToScan“属性是正确的方法吗?有更好的方法来完成这个行为吗?

编辑:

我能够使用property-override功能完成我所需要的工作。下面是我使用的更新的ExampleBatch.xml

代码语言:javascript
复制
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:batch="http://www.springframework.org/schema/batch"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd">

    <!-- ... -->    

    <import resource="classpath:batch-context.xml" />

    <context:property-override properties-ref="entityManagerOverride"/>

    <bean id="entityManagerOverride"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="properties">
            <util:properties>
                <prop key="entityManagerFactory.packagesToScan">com.example.domain,com.example.batch.module.domain</prop>
            </util:properties>
        </property>
    </bean>

    <!-- ... -->

</beans>

到目前为止,Spring并没有对我大喊这是一个无效的配置。还没有确定这是否真的产生了预期的结果。

编辑2:

属性覆盖方法似乎不够。这是一个有效的配置,但是在运行时检查实体管理器之后,如下所示:

代码语言:javascript
复制
for (EntityType<?> entity : manager.getMetamodel().getEntities()) {
    String name = entity.getName();
    System.out.println(name);
}

它只包含来自com.example.domain包的实体。

还有人有其他的想法吗?

EN

回答 3

Stack Overflow用户

发布于 2013-01-16 20:19:39

按照现在的方式,您确实定义了两个单独的beans一个名为entityManagerFactory,另一个称为entityManagerFactoryWithExtraPackages

有几种方法可以解决您的请求:

  1. 只要去掉其中一个beans -将定义合并为一个。我只想这不是你的选择,否则你就不会问了。
  2. entityManagerFactory定义为抽象的,那么您将最终拥有一个bean。
  3. 使用属性覆盖机制。这适合这种情况,在这种情况下,您无法控制'top‘bean,尽管您希望重新配置(实际上覆盖)在那里定义的bean的属性值。
票数 1
EN

Stack Overflow用户

发布于 2016-09-12 06:25:59

只需更换这个:

代码语言:javascript
复制
<property 
        name="packagesToScan" 
        value ="com.example.domain,com.example.domain.abstraction"/>

在这方面:

代码语言:javascript
复制
<property name="packagesToScan">
     <array>
          <value>com.example.domain</value>
          <value>com.example.domain.abstraction</value>
     </array>
 </property>
票数 1
EN

Stack Overflow用户

发布于 2013-01-16 20:15:31

如果它适合您的包组织,您可以尝试

代码语言:javascript
复制
<property name="packagesToScan" value="com.example.domain.*" />

在批处理-context.xml中,因此您的ExampleBatch.xml不再需要“覆盖”父服务器。

另一种挖掘方法是使用占位符;在批处理-context.xml中,您可以使用:

代码语言:javascript
复制
<property name="packagesToScan" value="${packageList}" />

在ExampleBatch.xml中,您将使用适当的值声明占位符,例如:http://www.mkyong.com/spring/spring-propertyplaceholderconfigurer-example/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14366558

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档