Spring singleton bean 与 prototype bean 的依赖

简介:

问题

我们知道,Spring bean 默认的 scope 是 singleton(单例),但有些场景(比如多线程)需要每次调用都生成一个实例, 此时 scope 就应该设为 prototype。如:

@Component @Scope("prototype")
public class DadTask implements Runnable {
 static Logger logger = Logger.getLogger(DadTask.class);

 @Autowired
 DadDao dadDao;

 private String name;
 /**
 * 
 */ public DadTask setDadTask(String name) {
 this.name = name;
 return this;
 }

 /* (non-Javadoc)
 * @see java.lang.Runnable#run()
 */ @Override public void run() {
 logger.info("DadTask:"+this + ";DadDao:"+dadDao + ";"+dadDao.sayHello(name) );
 //logger.info("Dad:"+name);
 }

}

但是,如果 singlton bean 依赖 prototype bean ,通过依赖注入方式, prototype bean 在 singlton bean 实例化时会创建一次(只一次), 比如:

@Service public class UserService {

 @Autowired private DadTask dadTask;

 public void startTask() {
 ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(2);

 scheduledThreadPoolExecutor.scheduleAtFixedRate(dadTask.setDadTask("Lily"), 1000, 2000, TimeUnit.MILLISECONDS);
 scheduledThreadPoolExecutor.scheduleAtFixedRate(dadTask.setDadTask("Lucy"), 1000, 2000, TimeUnit.MILLISECONDS);
 }
}

我希望调度“Lily” 和 “Lucy” 两个线程,实际上,它只给我初始化一个实例(这样就线程非安全了)。

解决

如果 singlton bean 想每次都去创建一个新的 prototype bean 的实例, 需要通过方法注入的方式。 可以通过实现 ApplicationContextAware 接口,来获取到 ApplicationContext 实例, 继而通过 getBean 方法来获取到 prototype bean 的实例。我们的程序需要修改如下:

@Service public class UserService implements ApplicationContextAware {

 @Autowired private DadTask dadTask;

 private ApplicationContext applicationContext;

 public void startTask() {
 ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(2);

 // 每次都拿到 DadTask 的实例
 dadTask = applicationContext.getBean("dadTask", DadTask.class);
 scheduledThreadPoolExecutor.scheduleAtFixedRate(dadTask.setDadTask("Lily"), 1000, 2000, TimeUnit.MILLISECONDS);
 dadTask = applicationContext.getBean("dadTask", DadTask.class);
 scheduledThreadPoolExecutor.scheduleAtFixedRate(dadTask.setDadTask("Lucy"), 1000, 2000, TimeUnit.MILLISECONDS);
 }

 @Override public void setApplicationContext (ApplicationContext applicationContext) throws BeansException {
 this.applicationContext = applicationContext;

 }
}

OK ,问题解决

源码

https://github.com/waylau/spring-framework-4-demosbeanScope 目录

参考

目录
相关文章
|
25天前
|
缓存 Java Spring
Spring 框架中 Bean 的生命周期
Spring 框架中 Bean 的生命周期
32 1
|
1天前
|
XML 人工智能 Java
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
|
8天前
|
存储 缓存 Java
Spring解决循环依赖
Spring解决循环依赖
|
9天前
|
Java 数据库连接 开发者
浅谈Spring的Bean生命周期
浅谈Spring的Bean生命周期
18 1
|
14天前
|
XML Java 数据格式
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
19 0
Bean工厂探秘:解析Spring底层工厂体系BeanFactory的神奇之道
|
24天前
|
XML Java 程序员
作为Java程序员还不知道Spring中Bean创建过程和作用?
作为Java程序员还不知道Spring中Bean创建过程和作用?
15 0
|
29天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
43 0
|
2月前
|
缓存 Java Maven
Spring Boot自动配置原理
Spring Boot自动配置原理
48 0
|
1月前
|
缓存 安全 Java
Spring Boot 面试题及答案整理,最新面试题
Spring Boot 面试题及答案整理,最新面试题
111 0
|
1月前
|
前端开发 搜索推荐 Java
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革