Search code examples
javaspringannotationsscopejavabeans

Create prototype scoped Spring bean with annotations?


Is it possible to convert the following XML configuration to an annotation based one?

<bean id="myBean" class="my.package.MyBeanClass" scope="prototype" />

I'm using Spring 2.5.6.


Solution

  • You can use the @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) annotation.

    @Service
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public class CustomerService {
        // ...
    }
    
    1. Spring API Docs.
    2. Example of the mapping.
    3. Scope annotation reference.