Java 在Spring上下文中外部注入属性

Java 在Spring上下文中外部注入属性,java,spring,properties-file,Java,Spring,Properties File,我的项目有两个xml文件。第一个文件刚刚加载,一些bean被实例化,调用一些方法会给我一个用户名和密码。必须将此用户名和密码发送到第二个xml文件,该文件中配置了异步侦听器。我一加载上下文,监听器就会启动。我想在加载xml之前将我拥有的用户名和密码传递给它。您可以使用PropertyPlaceHolderConfigure实现这一点。 假设先加载springConfigXml1.xml(其中包含包含以下方法的bean),然后执行setNamePassword方法,然后再加载springConfi

我的项目有两个xml文件。第一个文件刚刚加载,一些bean被实例化,调用一些方法会给我一个用户名和密码。必须将此用户名和密码发送到第二个xml文件,该文件中配置了异步侦听器。我一加载上下文,监听器就会启动。我想在加载xml之前将我拥有的用户名和密码传递给它。

您可以使用PropertyPlaceHolderConfigure实现这一点。 假设先加载springConfigXml1.xml(其中包含包含以下方法的bean),然后执行setNamePassword方法,然后再加载springConfigXml2.xml(其中包含AsyncListenerClass):

    public void setNamePassword(){
        //some code
        PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
        Properties properties = new Properties();
        properties.setProperty("property.userName", "username");
        properties.setProperty("property.password", "password");
        configurer.setProperties(properties);
                       //Include below line if you have another 
                       //PropertyPlaceholderConfigurer in springConfigXml2.xml       
                        configurer.setIgnoreUnresolvablePlaceholders(true); 
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
        context.addBeanFactoryPostProcessor(configurer);
        context.setConfigLocation("springConfigXml2.xml");
        context.refresh();
        //some code
    }



    springConfigXml2.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
        <bean id="asyncListener" class="com.example.AsyncListenerClass">
            <property name="userName" value="${property.userName}"/>
            <property name="password" value="${property.password}"/>
        </bean>
    </beans>
public void setNamePassword(){
//一些代码
PropertyPlaceholderConfigurer=新的PropertyPlaceholderConfigurer();
属性=新属性();
properties.setProperty(“property.userName”、“userName”);
properties.setProperty(“property.password”、“password”);
configurer.setProperties(属性);
//如果您还有其他的问题,请在下面的行中包括
//springConfigXml2.xml中的PropertyPlaceHolderConfiger
configurer.setIgnoreUnsolvablePlaceholders(true);
ClassPathXmlApplicationContext上下文=新的ClassPathXmlApplicationContext();
addBeanFactoryPostProcessor(配置器);
setConfigLocation(“springConfigXml2.xml”);
context.refresh();
//一些代码
}
springConfigXml2.xml

但随后将为上下文生命设置用户名和密码。

您可以使用PropertyPlaceHolderConfigure实现这一点。 假设先加载springConfigXml1.xml(其中包含包含以下方法的bean),然后执行setNamePassword方法,然后再加载springConfigXml2.xml(其中包含AsyncListenerClass):

    public void setNamePassword(){
        //some code
        PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
        Properties properties = new Properties();
        properties.setProperty("property.userName", "username");
        properties.setProperty("property.password", "password");
        configurer.setProperties(properties);
                       //Include below line if you have another 
                       //PropertyPlaceholderConfigurer in springConfigXml2.xml       
                        configurer.setIgnoreUnresolvablePlaceholders(true); 
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext();
        context.addBeanFactoryPostProcessor(configurer);
        context.setConfigLocation("springConfigXml2.xml");
        context.refresh();
        //some code
    }



    springConfigXml2.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
        <bean id="asyncListener" class="com.example.AsyncListenerClass">
            <property name="userName" value="${property.userName}"/>
            <property name="password" value="${property.password}"/>
        </bean>
    </beans>
public void setNamePassword(){
//一些代码
PropertyPlaceholderConfigurer=新的PropertyPlaceholderConfigurer();
属性=新属性();
properties.setProperty(“property.userName”、“userName”);
properties.setProperty(“property.password”、“password”);
configurer.setProperties(属性);
//如果您还有其他的问题,请在下面的行中包括
//springConfigXml2.xml中的PropertyPlaceHolderConfiger
configurer.setIgnoreUnsolvablePlaceholders(true);
ClassPathXmlApplicationContext上下文=新的ClassPathXmlApplicationContext();
addBeanFactoryPostProcessor(配置器);
setConfigLocation(“springConfigXml2.xml”);
context.refresh();
//一些代码
}
springConfigXml2.xml

但随后将为上下文生命设置用户名和密码。

嗨,prasad,这对我很有用。然而,有一个小问题,在springConfigXml2.xml中,我添加了一个属性文件,如下所示:
classpath:jndi.properties
看起来它被这个文件覆盖了。如何将两者都包括在内?在方法中包括这一点:configurer.setIgnoreUnsolvablePlaceholders(true);很高兴,这对你有帮助。此冲突是因为方法中设置的占位符配置器(例如p1)覆盖了springConfigXml2.xml中设置的占位符配置器(例如p2)的属性。beanfactorypostprocessor在p2之前加载p1,p1覆盖p2中的属性。是的,我理解这一点,但不知道如何不覆盖它。我猜您使用的整个概念是在加载上下文之前设置要使用的属性,对吗?嗨,prasad,这对我很有用。然而,有一个小问题,在springConfigXml2.xml中,我添加了一个属性文件,如下所示:
classpath:jndi.properties
看起来它被这个文件覆盖了。如何将两者都包括在内?在方法中包括这一点:configurer.setIgnoreUnsolvablePlaceholders(true);很高兴,这对你有帮助。此冲突是因为方法中设置的占位符配置器(例如p1)覆盖了springConfigXml2.xml中设置的占位符配置器(例如p2)的属性。beanfactorypostprocessor在p2之前加载p1,p1覆盖p2中的属性。是的,我理解这一点,但不知道如何不覆盖它。我猜您使用的整个概念是在加载上下文之前设置要使用的属性,对吗?