java 添加额外的包以使用 Spring 配置扫描到实体管理器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14366558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add extra package to scan to entity manager using Spring configuration?
提问by FGreg
I have a Spring Batch application that has a Spring context configuration that normally each batch job would reference. This way each batch job uses the same entity manager.
我有一个 Spring Batch 应用程序,它有一个 Spring 上下文配置,通常每个批处理作业都会引用。这样每个批处理作业都使用相同的实体管理器。
batch-context.xml:
批处理上下文.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>
Now in my specific batch job context (call it ExampleBatch.xml) I want to add another package to scan to the already defined entityManagerFactory bean. Is this possible?
现在在我的特定批处理作业上下文(称为 ExampleBatch.xml)中,我想添加另一个包以扫描到已定义的 entityManagerFactory bean。这可能吗?
ExampleBatch.xml:
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>
The way I have it right now will not work because it complains that "No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2
"
我现在的方式行不通,因为它抱怨“ No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2
”
Is trying to override the "packagesToScan" property the right approach to take in this scenario? Is there a better way to accomplish this behavior?
在这种情况下,是否尝试覆盖“packagesToScan”属性是正确的方法?有没有更好的方法来完成这种行为?
Edit:
编辑:
I was able to accomplish what I needed using the property-override
functionality. Below is the updated ExampleBatch.xml that I went with
我能够使用该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>
So far Spring does not yell at me that this is an invalid configuration. Have yet to determine if this is in fact producing the desired outcome.
到目前为止,Spring 并没有冲我大喊这是一个无效的配置。尚未确定这是否确实产生了预期的结果。
Edit 2:
编辑2:
The property-override method does not appear to be sufficient. It is a valid configuration but after inspecting the entity manager at runtime like this:
属性覆盖方法似乎还不够。这是一个有效的配置,但在运行时检查实体管理器后,如下所示:
for (EntityType<?> entity : manager.getMetamodel().getEntities()) {
String name = entity.getName();
System.out.println(name);
}
It only contains entities from the com.example.domain package.
它只包含来自 com.example.domain 包的实体。
Does anyone have any other ideas?
有没有人有其他想法?
回答by Grzegorz Oledzki
The way you have it now, you really define two separate beans - one called entityManagerFactory
and the other one entityManagerFactoryWithExtraPackages
.
按照您现在的方式,您实际上定义了两个独立的 bean - 一个称为entityManagerFactory
,另一个称为entityManagerFactoryWithExtraPackages
。
There are several ways to solve your very request:
有几种方法可以解决您的要求:
Just get rid of one of the beans - merge the definitions into one. I only guess it's not an option for you, otherwise you wouldn't ask.
Define the
entityManagerFactory
as abstract, then you end up having one bean anyway.Use the property override mechanism. This fits the scenarios, where you are not in control of the 'top' bean and despite that you want to re-configure (literally override the values of the properties of) beans defined there.
只需摆脱其中一个 bean - 将定义合并为一个。我只是猜这不是你的选择,否则你不会问。
将其定义
entityManagerFactory
为抽象,那么无论如何您最终都会拥有一个 bean。使用属性覆盖机制。这适用于您无法控制“顶级”bean 并且尽管您想重新配置(字面上覆盖其属性值)在那里定义的 bean 的场景。
回答by Vlad-HC
Just replace this:
只需替换这个:
<property
name="packagesToScan"
value ="com.example.domain,com.example.domain.abstraction"/>
with this:
有了这个:
<property name="packagesToScan">
<array>
<value>com.example.domain</value>
<value>com.example.domain.abstraction</value>
</array>
</property>
回答by Michael Zilbermann
If it fits your packages organization, you may try
如果它适合您的包裹组织,您可以尝试
<property name="packagesToScan" value="com.example.domain.*" />
In your batch-context.xml, so your ExampleBatch.xml doesn't have anymore to "override" the parent.
在您的 batch-context.xml 中,因此您的 ExampleBatch.xml 不再需要“覆盖”父级。
Another way to dig is using placeholder; in batch-context.xml, you would use :
另一种挖掘方法是使用占位符;在batch-context.xml 中,您将使用:
<property name="packagesToScan" value="${packageList}" />
while in ExampleBatch.xml you would declare the placeholder with the appropriate value, as explained for example here : http://www.mkyong.com/spring/spring-propertyplaceholderconfigurer-example/
而在 ExampleBatch.xml 中,您将使用适当的值声明占位符,例如在此处解释:http://www.mkyong.com/spring/spring-propertyplaceholderconfigurer-example/