简体   繁体   English

扩展spring应用程序上下文的最佳方法是什么?

[英]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.

相关问题 应用程序启动后在Spring上下文中初始化bean的最佳方法是什么? - Best way to initialize beans in Spring context after application started? 在spring xml配置中使用应用程序常量的最佳方法是什么? - what's the best way to use application constants in spring xml configuration? 在spring应用程序中实现自定义验证的最佳方法是什么? - What is the best way to implement custom validation in spring application? 在 Spring Boot 应用程序中安排任务的最佳方法是什么 - What is best way to schedule task in spring boot application 在Spring应用程序中将产品链接到订单的最佳方法是什么? - What is the best way to link products to an order in a Spring application? 设计Spring Application的最佳实践是我们需要多次创建相同上下文的方式吗? - Is it best practice to design Spring Application in such a way that we need to create same context multiple times? 拥有多个spring“应用程序上下文”的目的是什么 - What is the purpose of having multiple spring "application context" 在常见的 Spring 和 Angular 应用程序的上下文中,API 是什么? - In the context of a common Spring and Angular application, what is the API? Spring中的应用程序上下文有什么作用? - What does application context in Spring do? Spring注入:使用上下文的正确方法是什么 - Spring Injection: What is the correct way of using context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM