[英]What is the best way of extending spring application context?
I need to extend spring applicatioContext xml file with new beans definitions and then add references to them to list, which is a property of one bean: Basic applicationContext xml file: 我需要用新的bean定义扩展spring applicatioContext xml文件,然后将对它们的引用添加到列表中,这是一个bean的属性:基本applicationContext xml文件:
<bean id="myBean" class="com.example.MyBean">
<property name="providers">
<list>
<ref bean="provider1">
</list>
</property>
</bean>
<bean id="provider1" class="com.example.Provider">
Depends on instance of application I have different providers, so I need to add them to the list. 取决于应用程序实例,我有不同的提供程序,因此我需要将它们添加到列表中。 Now I have the additional beans definitions in database and use
BeanFactoryPostProcessor
to add them to the context and then add references to them to the list of providers. 现在,我在数据库中有了其他bean定义,并使用
BeanFactoryPostProcessor
将它们添加到上下文中,然后将对它们的引用添加到提供程序列表中。 But I use @Transactional
annotation on myBean
and automatic transaction management ( tx:annotation-driven
) and because of using BeanFactoryPostProcessor
the transaction annotations are not processed. 但是我在
myBean
和自动事务管理( tx:annotation-driven
)上使用@Transactional
注释,由于使用BeanFactoryPostProcessor
,因此未处理事务注释。
So I need another way to extend the application context and then the list of providers. 因此,我需要另一种方法来扩展应用程序上下文,然后扩展提供程序列表。 What can I use?
我可以使用什么?
My idea is to have xml file which at the beginning is empty and then fill it by data from database and then import it somehow in applicationContext. 我的想法是让xml文件开头是空的,然后用数据库中的数据填充它,然后以某种方式将其导入applicationContext中。 Is it possible?
可能吗?
Thanks for your help 谢谢你的帮助
Use @PostConstruct method in your MyBean class that loads and fills provider list from database. 在MyBean类中使用@PostConstruct方法,该方法从数据库加载和填充提供程序列表。
@PostConstruct
public void initialize() {
providers.addAll(providerService.findAll());
}
Put all database related code into Service/Dao class and annotate it with @Transactional
annotation 将所有与数据库相关的代码放入Service / Dao类,并使用
@Transactional
批注进行批注
You can override or extend the bean definitions in multiple ways. 您可以通过多种方式覆盖或扩展bean定义。 In short, here is one way..
简而言之,这是一种方法。
main Application Context xml: 主要应用程序上下文xml:
<bean id="myBaseBean" class="com.example.MyBean" abstract="true">
<property name="providers">
<list merge="true">
<ref bean="provider1" />
</list>
</property>
</bean>
<!-- default bean definition -->
<bean id="myBean" parent="myBaseBean">
<property name="providers">
<list merge="true">
</list>
</property>
</bean>
<bean id="provider1" class="com.example.Provider">
Some extendedApplication Context xml: 一些extendedApplication Context xml:
<bean id="myBean" parent="myBaseBean">
<property name="providers">
<list merge="true">
<ref bean="someOtherProvider" />
</list>
</property>
</bean>
<!-- bean definition of some other provider -->
This is nothing to do with Transactions
You have to handle the transactions as usual for every other bean. 这与
Transactions
无关,您必须照常处理其他所有bean的事务。
NOTE: All the application context files will be loaded/override based on the order of the file names you mention while creating the ApplicationContext
. 注意:将根据您在创建
ApplicationContext
提到的文件名的顺序来加载/覆盖所有应用程序上下文文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.