2010-03-01 11 views
6

Sono consapevole del fatto che la seguente implementazione di un PropertyPlaceholderConfigurer è possibile:Primavera: livello di programmazione utilizzare PropertyPlaceholderConfigurer su nessuno Fagioli singelton

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()); 
    }} 

Con questo nel file di configurazione:

<?xml version="1.0" encoding="UTF-8"?> 

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

<bean id="testBean" class="com.spring.ioc.TestClass"> 
    <property name="first" value="${first.prop}"/> 
    <property name="second" value="${second.prop}"/> 
</bean> 

Tuttavia, questo mi sembra che le modifiche apportate al testBean verranno visualizzate su tutti i bean di test.

Come si utilizza propertyPlaceHolderCongfigurer in modo tale che sia possibile applicarlo a singole istanze del bean e avere accesso a ciascuna di queste istanze?

Spero che la domanda abbia un senso. Qualsiasi aiuto sarebbe molto apprezzato.

risposta

2

Per impostazione predefinita, i bean di primavera sono singleton, ovvero le chiamate successive a context.getBean("testBean") restituirebbero la stessa istanza. Se si desidera loro di tornare diverse istanze, è necessario impostare un scope = "prototype" sulla definizione di fagioli:

<bean id="testBean" class="com.spring.ioc.TestClass" scope = "prototype"> 
... 
+0

Prima festeggio voglio solo confermare che premendo il pulsante context.refresh solito aggiornare le istanze passate di questo fagiolo? – Babyangle86

+0

Sì, non lo farebbe. – axtavt

+0

Brillante. Grazie. – Babyangle86

Problemi correlati