Nicolas Frankel is an IT consultant with 10 years experience in Java / JEE environments. He likes his job so much he writes technical articles on his blog and reviews technical books in his spare time. He also tries to find other geeks like him in universities, as a part-time lecturer. Nicolas is a DZone MVB and is not an employee of DZone and has posted 232 posts at DZone. You can read more from them at their website. View Full User Profile

Changing Default Spring Bean Scope

02.04.2013
| 4111 views |
  • submit to reddit

 By default, Spring beans are scoped singleton, meaning there’s only one instance for the whole application context. For most applications, this is a sensible default; then sometimes, not so much. This may be the case when using a custom scope, which is the case, on the product I’m currently working on. I’m not at liberty to discuss the details further: suffice to say that it is very painful to configure each and every needed bean with this custom scope.

Since being lazy in a smart way is at the core of developer work, I decided to search for a way to ease my burden and found it in the BeanFactoryPostProcessor class. It only has a single method - postProcessBeanFactory(), but it gives access to the bean factory itself (which is at the root of the various application context classes).

From this point on, the code is trivial even with no prior experience of the API:

public class PrototypeScopedBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
 
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException {
 
        for (String beanName : factory.getBeanDefinitionNames()) {
 
            BeanDefinition beanDef = factory.getBeanDefinition(beanName);
 
            String explicitScope = beanDef.getScope();
 
            if ("".equals(explicitScope)) {
 
                beanDef.setScope("prototype");
            }
        }
    }
}
The final touch is to register the post-processor in the context . This is achieved by treating it as a simple anonymous bean:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 
    <bean class="ch.frankel.blog.spring.scope.PrototypeScopedBeanFactoryPostProcessor" />
 
</beans>

Now, every bean which scope is not explicitly set will be scoped prototype.

Sources for this article can be found attached in Eclipse/Maven format.


Published at DZone with permission of Nicolas Frankel, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Martin Vaněk replied on Mon, 2013/02/04 - 6:02am

silly but nice

Nicolas Frankel replied on Mon, 2013/02/04 - 8:55am in response to: Martin Vaněk

Silliness depends on your point of view. I have a use-case when default beans have to have a scope narrower than singleton. 

Martin Vaněk replied on Mon, 2013/02/04 - 10:51am in response to: Nicolas Frankel

Aha. I thought that default scope CAN be defined by some beans element attribute, similarly to default-init-method, default-autowire etc, but it is not! Then this is NOT silly at all.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.