本文同步至:http://www.waylau.com/spring-singleton-beans-with-prototype-bean-dependencies/java
##问题git
咱们知道,Spring bean 默认的 scope 是 singleton(单例),但有些场景(好比多线程)须要每次调用都生成一个实例, 此时 scope 就应该设为 prototype。如:github
<!-- more -->spring
@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 的实例。咱们的程序须要修改以下:app
@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 ,问题解决ide
见 https://github.com/waylau/spring-framework-4-demos 中 beanScope
目录this