2010-03-01 10 views
6

Bir PropertyPlaceHolderConfigurer uygulamasının aşağıdaki şekilde uygulanabileceğinin farkındayım:Spring: Tekil Olmayan Fasulyelerde PropertyPlaceHolderConfigurer'ı programlı olarak kullanma

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

Bu yapılandırma dosyasında:

<?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> 

Ancak, bu bana testBean'da yapılan değişikliklerin tüm test çekirdeklerinde gösterileceğine inanıyor

propertyPlaceHolderCongfigurer'ı böyle bir şekilde nasıl kullanırım Ben fasulye bireysel örneklerine uygulayabilirim ve bu örneklerin her birine erişebilir miyim?

Umarız soru mantıklıdır.Herhangi bir yardım çok takdir edilecektir.

cevap

2

Varsayılan olarak, Çekirdek çekirdekleri tekildir, sonraki çağrıları ise context.getBean("testBean") aynı örneği döndürür. Ben sadece isabet context.refresh düğmesi alışkanlık bu fasulye geçmiş örneklerini güncellemenizi onaylamak istiyorum kutlamak önce

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

: Onları farklı örneklerini dönmek isterseniz, fasulye tanımı üzerinde scope = "prototype" belirlesin? – Babyangle86

+0

Evet, olmaz. – axtavt

+0

Parlak. Teşekkürler. – Babyangle86

İlgili konular