public class PnFileTGIComputeThread implements Runnable {
@Resource
private AppUsedService appUsedService;
// AppUsedService appUsedService = (AppUsedService) AllBean.getBean("appUsedService");
public String taskId;
public int cityId;
public PnFileTGIComputeThread(String name, int cityId){
this.taskId = name;
this.cityId = cityId;
}
@Override
public void run() {
try {
this.appUsedService.doSaveAzTaskAppUsedInfoCity(Integer.valueOf(taskId),cityId);
} catch (Exception e){
e.printStackTrace();
}
}
} 新建了一個線程,然后再主線程中去實例化本線程,啟動線程。DUG問題是,線程啟動后,參數也都傳過來了,但是通過注解來注入的service一直是null值。
老辦法,翻了度娘的牌子,找到問題,在線程中為了線程安全,是防注入。沒辦法,要用到這個類啊。只能從bean工廠里拿個實例了
public class AllBean implements ApplicationContextAware{
private static ApplicationContext applicationContext;
public void setApplicationContext(ApplicationContext context) {
AllBean.applicationContext = context;
}
public static Object getBean(String name){
return applicationContext.getBean(name);
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
}
getbean方法,獲取上下文中的bean, 不過呢要有點問題,這個AllBean類需要在在Bean工廠中注冊下
<bean id="allBean" class="xxxxx.AllBean" />想要啥東西,現在都可以直接去getBean,例如:
AppUsedService appUsedService = (AppUsedService) AllBean.getBean("appUsedService"); 好的,線程正常啟動了。
