Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a prototype scoped bean with Spring Javaconfig?

Tags:

java

spring

The old docs for Spring Javaconfig say that I can use

@Bean(scope=DefaultScopes.PROTOTYPE)

to get a prototype bean, but Spring 3.0.5's @Bean doesn't seem to have this property.

Is there any way to control the scope of a bean in Javaconfig?

like image 628
Duncan McGregor Avatar asked Nov 14 '11 17:11

Duncan McGregor


2 Answers

Use @Scope instead.

Also, DefaultScopes is not available in Spring core, but you can use BeanDefinition.SCOPE_PROTOTYPE and BeanDefinition.SCOPE_SINGLETON for convenience.

like image 111
Chris Beams Avatar answered Nov 09 '22 22:11

Chris Beams


You can add @Scope("prototype") for example:

@Bean
@Scope("prototype")
public DemoDao getDao() {
    DemoDao dao = new DemoDao();
    dao.setAddress("annoted:address");
    dao.setName("annoted:name");
    return dao;
}
like image 43
vincent zhang Avatar answered Nov 10 '22 00:11

vincent zhang