package com.rajesh.learnjava.controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.rajesh.learnjava.bean.ControllerClass;
public class App {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationcontext.xml");
ControllerClass helloWorld = (ControllerClass) context.getBean("controller");
System.out.println("1st service : " + helloWorld.getServiceClass().getStr());
ControllerClass helloWorld2 = (ControllerClass) context.getBean("controller");
System.out.println("1st service : " + helloWorld2.getServiceClass().getStr());
System.out.println("########## Below is the service call with lookup-method option in spring and see the results ##########");
ControllerClass controller = (ControllerClass) context.getBean("controller");
System.out.println("2nd service : " + controller.getServiceClasswithLookupMethod().getStr());
ControllerClass controller2 = (ControllerClass) context.getBean("controller");
System.out.println("2nd service : " + controller2.getServiceClasswithLookupMethod().getStr());
}
}
ControllerClass.java
package com.rajesh.learnjava.bean;
public class ControllerClass {
ServiceClass serviceClass;
ServiceClasswithLookupMethod serviceClasswithLookupMethod;
public ServiceClass getServiceClass() {
return serviceClass;
}
public void setServiceClass(ServiceClass serviceClass) {
this.serviceClass = serviceClass;
}
public ServiceClasswithLookupMethod getServiceClasswithLookupMethod() {
return serviceClasswithLookupMethod;
}
public void setServiceClasswithLookupMethod(
ServiceClasswithLookupMethod serviceClasswithLookupMethod) {
this.serviceClasswithLookupMethod = serviceClasswithLookupMethod;
}
}
ServiceClass.java
package com.rajesh.learnjava.bean;
public class ServiceClass {
private int counter;
public String getStr() {
return " counter is:" + (++counter);
}
}
applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ws=" https://jax-ws.java.net/spring/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
https://jax-ws.java.net/spring/core
https://jax-ws.java.net/spring/core.xsd
https://jax-ws.java.net/spring/servlet
https://jax-ws.java.net/spring/servlet.xsd">
<bean id="controller" class="com.rajesh.learnjava.bean.ControllerClass" scope="singleton">
<property name="serviceClass" ref="serviceClass" />
<lookup-method name="getServiceClasswithLookupMethod" bean="serviceClasswithLookupMethod"/>
</bean>
<bean id="serviceClass" class="com.rajesh.learnjava.bean.ServiceClass" scope="prototype">
</bean>
<bean id="serviceClasswithLookupMethod" class="com.rajesh.learnjava.bean.ServiceClasswithLookupMethod" scope="prototype">
</bean>
<bean id="myBean" class="com.mycompany.MyPrototypeBean" scope="prototype">
<aop:scoped-proxy/>
</bean>
</beans>
Below is the output of the program App.java If you observer the output closely, the first two lines, the count is incremented from 1 to 2 because we are getting same serviceClass instance in both the cases
whereas we are getting counter value as 1 and 1 for the 2nd set of statements. This is because, we are getting new instance of ServiceClass (using the lookup-method) each time into the Controller class even though COntroller class in defined with singleton scope.
1st service : counter is:1
1st service : counter is:2
########## Below is the service call with lookup-method option in spring and see the results ##########
2nd service : counter is:1
2nd service : counter is:1
No comments:
Post a Comment