Home > Java > Changing default Spring bean scope

Changing default Spring bean scope

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.

email
Send to Kindle
Categories: Java Tags:
  1. February 4th, 2013 at 01:05 | #1

    Hi Nicolas,
    couldn’t you use spring AOP’s API for this also ?
    In you case all the beans must inherit from PrototypeScopedBeanFactoryPostProcessor.

    With AOP one could just name those prototype beans differently, like:

    .*prototype.*

    prototype

    and you would be done without a single line of java, that would be even more laziness, right ?

  2. February 4th, 2013 at 01:08 | #2

    sorry the code does not appear in my previous post but it can be found in section 7.2.4.1.1 of http://static.springsource.org/spring/docs/2.5.4/reference/aop-api.html

    where we add a property which name is scope and value is prototype

  3. February 4th, 2013 at 16:10 | #3

    Hi Loïc,

    Thanks for taking the time to read my articles instead of wrestling Grizzly bears :-P Must really be freezing up there.

    Yet, there seem to be a misunderstading: I do not need to inherit from my PostProcessor; I just have to declare it in the Spring Beans Definition File. Just read the documentation hyperlinked in the article.

    Now, in general, I tend to be wary of AOP since it’s quite powerful but may also have unwanted side-effects. For example, will you use compile-time weaving or runtime weaving? Both have pros and cons.

    Finally, from what I understand (without the XML formatting…), you would have to create a poincut over the Spring factory itself:
    - since there’s a nominal solution, I’d prefer to use the one described
    - how would you achieve the same result in a Spring integration test (see attached sources)?

  1. No trackbacks yet.