יום שלישי, 14 במאי 2013

Spring context scope

The spring Ioc container is not singleton .every call to ClassPathXmlApplicationContext  generate a separate spring Ioc  context .

Lets define the following spring bean :

<bean id="accountService" class="javaapplicationbasicspring.SimpleAccountService" scope="singleton">
</bean>

(I know scope="singleton” is the default context)

The SimpleAccountService class has one property :

private String mTestValue = "Nothing";

public String getTestValue() {
return mTestValue;
}

public void setTestValue(String mTestValue) {
this.mTestValue = mTestValue;

Lets do the following text :

ClassPathXmlApplicationContext appContextParent = new ClassPathXmlApplicationContext(
new String[] {"resources/ParentSpringXMLConfig.xml"});

ClassPathXmlApplicationContext appContextParent2 = new ClassPathXmlApplicationContext(
new String[] {"resources/ParentSpringXMLConfig.xml"} );

SimpleAccountService theSimpleAccountService =
(SimpleAccountService)appContextParent2.getBean("accountService");

SimpleAccountService theSimpleAccountService2 =
(SimpleAccountService)appContextParent.getBean("accountService");

theSimpleAccountService.setTestValue ("Zvika");

System.out.print ("Hello: " + theSimpleAccountService2.getTestValue())



The result is :


Hello: Nothing


we saw that each application context has its one instance .


I don't like it because an application should take care of managing the single spring context in it deferent layers , modules etc instead of taking care by the spring infrastructure .

אין תגובות:

הוסף רשומת תגובה