Blog on Spring and DWR. AJAX with Spring made easy!

Scoping in Spring 2.0

Filed under: Java, Spring — Bram Smeets at 10:38 pm on Tuesday, September 12, 2006

For long one of the limitations of Spring has been that it only supports two kinds of lifecycles for its managed beans.
By default all beans managed by Spring are singletons. By adding the singleton="false" attribute to a bean definition this bean will become a prototype which means a new instance will be created each time the bean is injected or looked up.

However, up untill now it was not easily possible to scope your bean definitions either per request or per session. As of version 2.0, Spring offers more advanced scoping of your Spring-managed beans. You can define any arbitrary scope for a bean using a ScopedProxyFactoryBean and a specific scope implementation. When using a scoped bean, a proxy will be created for every reference to the scoped bean. The proxy will determine the actual instance it will point to based on the context in which the bean is called.

Spring comes with two build-in scopes: request scope and session scope, but it is also possible to implement your own scope. When using request scope, each request will obtain its own instance of the scoped bean. In case of session scope, each session will obtain its own instance. Changing values on the bean instance will therefore only be visible in the specific context (either the request or session). In order for the scoping to work, the current request (and therefore the session as well) need to be set on the RequestHolder class. You can either set this manually by using the bind(HttpServletRequest) method, or use one of the convenience classes for this. The latter is the preferred way, as this will also handle unbinding of the current request to avoid memory leaks. The convenience classes are: RequestBindingFilter (a servlet filter) and RequestBindingHandlerInterceptor (a handler interceptor).

Setting up scoping for a target bean is similar to setting up target sources as explained in the previous section. Below is an example of a session scoped business object.

<bean id="sessionScopedBean" class="org.springframework.aop.target.scope.HttpSessionScopedProxyFactoryBean"> <property name="targetBeanName" value="businessObjectTarget"/> <property name="sessionKey" value="sessionKey"/>
</bean>

Note that the target object –”businessObjectTarget” in the example– must be a prototype (so setting the singleton="false" attribute on its definition). This allows the ScopedProxyFactoryBean implementation to create new instances of the target as necessary. See the Javadoc for ScopedProxyFactoryBean and the subclasses you wish to use for information about it’s properties.

Next to the HttpSessionScopedProxyFactoryBean in the above example, Spring also offers a HttpRequestScopedProxyFactoryBean that allows for a request scope. Configuration of this proxy factory bean works similar to the session scoped counterpart:

<bean id="requestScopedBean" class="org.springframework.aop.target.scope.HttpRequestScopedProxyFactoryBean"> <property name="targetBeanName" value="businessObjectTarget"/> <property name="sessionKey" value="sessionKey"/>
</bean>

Note: Public fields of scoped beans will not be proxied and therefore will not be scoped. Only method calls are proxied and will therefore be scoped.

As mentioned earlier, you can also define you own scope, for instance a conversation scope based on some kind of conversation id or a user scope which works using a cookie and persists and retrieves user data based on the cookie id. To get started on implementing your own scope, have a look at the JavaDoc of ScopeMap.

Scope tag

Using the aop namespace in the configuration namespace support, it is possible to apply scoping in a much more easy way by setting the scope attribute of a bean definition. The attribute can either be set to request or session to apply respectively request- or session-scoping of your bean. The following example is identical to the previous example of a request-scoped bean.

<bean id="businessObjectTarget" class="..." scope="request"> <aop:scoped-proxy/>
</bean>

Note that you have to declare the aop:scoped-proxy tag as a nested tag in order to inform the Spring container to create a proxy for the bean. In order for this to work, make sure to add the following namespace to your beans declaration.

xmlns:aop="http://www.springframework.org/schema/aop"

For more information on the new scoping in Spring have a look at the Spring reference manual, which (as usual) gives a more thorough description of the new scoping mechanism. It also provides more information on defining your won custom scopes.

JTeam is hiring!

Filed under: Ajax, Java, Spring — Bram Smeets at 6:00 pm on Monday, September 11, 2006

JTeam is currently looking for Java developers, architects and business analysts. We are looking for people at all experience levels, including interns with a background in IT and interest in learning to build real-world applications using the Java programming language.

Working at JTeam is something I have always considered a rewarding experience. You get to work in an innovative environment, working with the Java frameworks that are used mostly today, like Spring, Hibernate, DWR, and etcetera. An interesting area, at least in my opinion, we are currently doing more and more in is the usage of Ajax and the so-called Web 2.0. We help our customers to rethink their conventional web applications and making use of existing technologies to build richer and more userfriendly GUIs, while still benefiting of the huge install base of the modern webbrowser.

So if you live in The Netherlands (preferably in the vicinity of Amsterdam) and are looking for a new challenge, send your resume and a short motivation to jobs@jteam.nl, or have a look at our current job openings. Mentioning that you read this blog entry will increase your changes of getting a job dramatically ;-)
Also, join us at the Java Meetup on September 29th in Amsterdam, to get to know us. Many of JTeam’s employees will be there…

 


You are viewing a mobilized version of this site...
View original page here

Mobilized by Mowser Mowser