微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – Spring:在没有Singelton Beans的情况下以编程方式使用PropertyPlaceHolderConfigurer

我知道PropertyPlaceHolderConfigurer的以下实现是可能的:

public class SpringStart {
   public static void main(String[] args) throws Exception {
     PropertyPlaceholderConfigurer configurer = new PropertyPlaceholderConfigurer();
     Properties properties = new Properties();
     properties.setProperty("first.prop","first value");
     properties.setProperty("second.prop","second value");
     configurer.setProperties(properties);

     ClasspathXmlApplicationContext context = new ClasspathXmlApplicationContext();
     context.addbeanfactoryPostProcessor(configurer);

     context.setConfigLocation("spring-config.xml");
     context.refresh();

     TestClass testClass = (TestClass)context.getBean("testBean");
     System.out.println(testClass.getFirst());
     System.out.println(testClass.getSecond());
  }}

配置文件中有这个:

http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd“>

irst" value="${first.prop}"/>
    

但是,这使我看到对testBean所做的更改将显示在所有测试bean上.

如何以这样的方式使用propertyPlaceHolderCongfigurer,我可以将它应用于bean的各个实例,并且可以访问这些实例中的每一个

我希望这个问题有道理.任何帮助将非常感激.

最佳答案
认情况下,Spring bean是单例,即后续对context.getBean(“testBean”)的调用将返回相同的实例.如果希望它们返回不同的实例,则应在bean定义上设置scope =“prototype”:

cope = "prototype"> 
...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐