Create prototype scoped Spring bean with annotations?

user321068 picture user321068 · Mar 29, 2012 · Viewed 46.8k times · Source

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.

Answer

ManuPK picture ManuPK · Mar 29, 2012

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.