D、关于单例与原型
Solon 容器托管的对象,默认是单例的。也可以是原型的(即每次获取,都重新建造实例并注入)。
1、构建单例托管对象
@Component
组件注解(默认是单例)
@Component
public class DemoService{ }
@Bean
注解(只有单例)
@Configuration
public class DemoConfig {
@Bean
public DataSource db1(@Inject("${demo.db1}") HikariDataSource ds){
return ds;
}
}
- 手动构建
Solon.context().wrapAndPut(DemoService.class);
//或
//Solon.context().wrapAndPut(DemoService.class, new DemoService());
2、构建原型托管对象
@Component
组件注解(添加非单例注解申明)
@Singleton(false)
@Component
public class DemoService{ }
- 手动构建
BeanWrap bw = Solon.context().wrap(DemoService.class);
bw.singletonSet(false);
Solon.context().putWrap(DemoService.class, bw)