Spring singleton bean 與 prototype bean 的依賴

本文同步至: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-demosbeanScope 目錄this

參考

相關文章
相關標籤/搜索