But what is the life of these bean instances? Are they created new each time a bean is requested? Or do we get the same instance even if we ask for it a hundred times?
This is called 'scope' of a bean.
Beans can have 5 scopes:
1. Singleton - this means there is exactly one instance of the bean, this bean never dies, everyone get this same bean instance no matter how many times they ask for it.
2. Prototype - this means that a new instance of the bean is created each time someone asks for it. If a hundred requests are made for it, a hundred independent instances will get created. Each instance will get garbage collected when it is no longer in use - just like any other object.
3. Request - this means a new instance is created for each HTTP request. The instance dies when the HTTP request completes.
4. Session - this means a new instance for each new HTTP session. When the session ends, the instance dies.
5. Global session - I am not completely clear about this scope.
Let us see what it takes to convert our beans to Request scope (by default, they are session scope).
First, we need to update out Spring context to include the Request scope.
To do this, open your SpringUtils.java, and update it to look like this:
(notice that we have changed the type of springContext to ClassPathXmlApplicationContext)
package org.confucius;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.request.RequestScope;
import org.springframework.web.context.request.SessionScope;
public class SpringUtils {
private static ClassPathXmlApplicationContext springContext = null;
public static ApplicationContext getSpringContext() {
if (springContext == null){
springContext = new ClassPathXmlApplicationContext("spring-beans.xml");
springContext.getBeanFactory().registerScope("session", new SessionScope());
springContext.getBeanFactory().registerScope("request", new RequestScope());
}
return springContext;
}
}
Next, we need to add a Listener to our web.xml - so Spring knows when new requests are made.
Update your web.xml to look like this:
<web-app>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>shop</servlet-name>
<servlet-class>org.confucius.ShopServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>checkout</servlet-name>
<servlet-class>org.confucius.CheckoutServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>shop</servlet-name>
<url-pattern>/shop</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>checkout</servlet-name>
<url-pattern>/checkout</url-pattern>
</servlet-mapping>
</web-app>
Finally, update your bean declarations to specify the request scope.
Your bean-cart.xml will look like this:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="cart" class="org.confucius.Cart" scope="request"/>
</beans>
bean-shop.xml looks like this:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="shop" class="org.confucius.Shop" scope="request">
<property name="cart" ref="cart"/>
</bean>
</beans>
bean-checkout.xml looks like this:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="checkout" class="org.confucius.Checkout" scope="request">
<property name="cart" ref="cart"/>
</bean>
</beans>
If you build and deploy HelloWorld.war, then browse to:
http://localhost:8080/HelloWorld/shop
no matter how many times you refresh the browser, there will be only one item in the cart.
And if you browse to:
http://localhost:8080/HelloWorld/checkout
there will always be zero items in the cart.
This is because a new instance of the beans are getting created each time you refresh.
No comments:
Post a Comment