Java 运行时添加PropertySourcesPlaceholderConfigurer bean

Java 运行时添加PropertySourcesPlaceholderConfigurer bean,java,spring,Java,Spring,在我们的web应用程序设计中,我们需要两个spring上下文xml文件。一个将在部署时加载,另一个将在运行时以编程方式加载 propertysourcesplaceplaceconfigurer在两个xml文件中都配置了bean。但它只适用于已部署的SpringXMLpropertysourcesplaceconfigurer在运行时加载的第二个xml文件中定义的bean不会对该xml中定义的属性生效 示例Spring.xml部署时的加载 <bean class="org.springfr

在我们的web应用程序设计中,我们需要两个spring上下文xml文件。一个将在部署时加载,另一个将在运行时以编程方式加载

propertysourcesplaceplaceconfigurer在两个xml文件中都配置了bean。但它只适用于已部署的SpringXMLpropertysourcesplaceconfigurer在运行时加载的第二个xml文件中定义的bean不会对该xml中定义的属性生效

示例Spring.xml部署时的加载

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer" id="Deployment_props">
<property name="placeholderPrefix" value="${APZ:"/>
<property name="placeholderSuffix" value="}"/>
<property name="location" value="classpath:Server/META-INF/Env.properties"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>


<bean class="com.dao.HttpDetails" id="Admin_http" scope="request">
<property name="callType" value="POST"/>
<property name="timeOut" value="${APZ:timeout}"/>
</bean>
Spring-app.xml中定义的所有内容都正常工作,除了PropertyPlaceholderConfigurerbean。
如果您有任何建议或解决方法,我们将不胜感激。

为什么要使用这种装置来加载文件?为什么有两个不同的文件?区别应该在于属性,而不是bean的整个加载和定义?看起来您正在非常努力地围绕框架工作,而不是使用框架。@M.Deinum我们正在实现多租户。因此,当在rutime添加新租户时,我们将加载与该租户相关的所有bean xml和属性文件,而无需重新启动应用程序服务器。第二个xml文件是新租户的配置。您实际上是在替换bean,而不是扩展它们。另外,每个租户加载bean通常是个坏主意(您基本上是为每个租户再次加载完整的应用程序,这比多租户的目的要好得多)。每个租户都有不同的bean ID和不同的bean定义,这取决于他们使用的服务。我们的应用程序运行良好,除了使用这些PropertyPlaceHolders,您仍然(重新)加载不应该重新加载的部件(以及多租户用途的不同)。您只在beanfactory而不是applicationcontext中加载bean。因此,像bean的后处理或bean定义这样的事情根本不起作用。为什么要使用这种装置来加载文件?为什么有两个不同的文件?区别应该在于属性,而不是bean的整个加载和定义?看起来您正在非常努力地围绕框架工作,而不是使用框架。@M.Deinum我们正在实现多租户。因此,当在rutime添加新租户时,我们将加载与该租户相关的所有bean xml和属性文件,而无需重新启动应用程序服务器。第二个xml文件是新租户的配置。您实际上是在替换bean,而不是扩展它们。另外,每个租户加载bean通常是个坏主意(您基本上是为每个租户再次加载完整的应用程序,这比多租户的目的要好得多)。每个租户都有不同的bean ID和不同的bean定义,这取决于他们使用的服务。我们的应用程序运行良好,除了使用这些PropertyPlaceHolders,您仍然(重新)加载不应该重新加载的部件(以及多租户用途的不同)。您只在beanfactory而不是applicationcontext中加载bean。所以像bean的后处理或bean定义这样的事情根本不起作用。
<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer" id="Runtime_props">
<property name="placeholderPrefix" value="${APZ:"/>
<property name="placeholderSuffix" value="}"/>
<property name="location" value="classpath:Admin/Env.properties"/>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
</bean>


<bean class="com.dao.HttpDetails" id="Service_http" scope="request">
<property name="callType" value="POST"/>
<property name="timeOut" value="${APZ:time}"/>
</bean>
        WebApplicationContext context = Frameworks.getWebAppContext();
        AutowireCapableBeanFactory factory = context.getAutowireCapableBeanFactory();
        BeanDefinitionRegistry registry = (BeanDefinitionRegistry) factory;
        XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(registry);
        xmlReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);

        try {
            xmlReader.loadBeanDefinitions(
                    new InputSource(AppIntialization.class.getClassLoader().getResourceAsStream("Spring-app.xml")));
        } catch (BeanDefinitionStoreException e) {

        }