我有一个Spring批处理应用程序,它有一个Spring上下文配置,通常每个批处理作业都会引用这个配置。这样,每个批处理作业都使用相同的实体管理器。
批处理-context.xml:
<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:
<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
<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:
属性覆盖方法似乎不够。这是一个有效的配置,但是在运行时检查实体管理器之后,如下所示:
for (EntityType<?> entity : manager.getMetamodel().getEntities()) {
String name = entity.getName();
System.out.println(name);
}它只包含来自com.example.domain包的实体。
还有人有其他的想法吗?
发布于 2013-01-16 20:19:39
按照现在的方式,您确实定义了两个单独的beans一个名为entityManagerFactory,另一个称为entityManagerFactoryWithExtraPackages。
有几种方法可以解决您的请求:
entityManagerFactory定义为抽象的,那么您将最终拥有一个bean。发布于 2016-09-12 06:25:59
只需更换这个:
<property
name="packagesToScan"
value ="com.example.domain,com.example.domain.abstraction"/>在这方面:
<property name="packagesToScan">
<array>
<value>com.example.domain</value>
<value>com.example.domain.abstraction</value>
</array>
</property>发布于 2013-01-16 20:15:31
如果它适合您的包组织,您可以尝试
<property name="packagesToScan" value="com.example.domain.*" />在批处理-context.xml中,因此您的ExampleBatch.xml不再需要“覆盖”父服务器。
另一种挖掘方法是使用占位符;在批处理-context.xml中,您可以使用:
<property name="packagesToScan" value="${packageList}" />在ExampleBatch.xml中,您将使用适当的值声明占位符,例如:http://www.mkyong.com/spring/spring-propertyplaceholderconfigurer-example/
https://stackoverflow.com/questions/14366558
复制相似问题