@Autowired在可运行内部无法正常工作 [英] @Autowired not working inside a runnable
本文介绍了@Autowired在可运行内部无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个可运行的任务,正在尝试自动接线,但是当我这样做时,该任务无法运行. 当我自动在可运行区域之外的田野进行工作时,它可以正常工作.为什么会这样呢?除了在内部自动装配以外,还有其他更干净的方法可以在可运行内部获取自动装配字段的新实例吗?
I have a runnable task where i am trying to Autowire field but when i do it the task doesn't run . When i Autowire the field outside the runnable it works fine . Why is this happening ? Also is there any other cleaner way to get new instances of a autowired field inside runnable other than Autowiring it inside ?
这是我的可运行方法`
Runnable task = new Runnable() {
@Autowired
ICruxPanelClientService CruxPanelClientService;
public void run (){
CruxPanelClientService.setCruxWebServiceBaseURL("http://10.41.181.23:8080");
CronCruxModel m = new CronCruxModel();
m = model1.get(model_var);
System.out.println("EXECUTING");
System.out.println(m.getService_status() + " ---------EXEexecution");
System.out.println(m.getCat_name() + "Executing Name ");
// time = m.getService_time();
UpdateCategoryRequest updateCategoryRequest = new UpdateCategoryRequest();
CategoryModel categoryModel = new CategoryModel();
categoryModel.setColor(m.getCat_color());
categoryModel.setIcon(m.getCat_icon());
categoryModel.setIconWhite(m.getCat_icon_white());
categoryModel.setName(m.getCat_name());
categoryModel.setId(m.getCat_id());
categoryModel.setKey(m.getCat_catkey());
categoryModel.setIndexOrder(m.getCat_indexOrder());
updateCategoryRequest.setCategory(categoryModel);
CruxPanelClientService.updateCategory(updateCategoryRequest);
GetServiceDataIdByCategoryIdRequest request1 = new GetServiceDataIdByCategoryIdRequest();
request1.setId(m.getCat_id());
GetServiceDataIdByCategoryIdResponse response1 = CruxPanelClientService.getServiceDataIdByCategoryId(request1);
ArrayList<ServiceModel> service = new ArrayList<ServiceModel>();
service = response1.getServiceModels();
JSONArray json = new JSONArray();
if(m.getService_order_succ_msg()==null)
{
json = new JSONArray();
}
else {
json = new JSONArray(m.getService_order_succ_msg());
}
String message = m.getService_order_succ_msg();
for (int j=0;j<service.size();j++)
{
UpdateServiceMasterRequest req = new UpdateServiceMasterRequest();
ServiceModel s = new ServiceModel();
s=service.get(j);
;
JSONObject obj = new JSONObject();
if(json.length()==0 )
{
String ms = null;
s.setOrderSuccessMessage(ms);
req.setServiceModel(s);
}
else {
String message1 = json.get(j).toString();
if(message1.equals(null) || message1.equals("")) {
String ms = null;
s.setOrderSuccessMessage(ms);
req.setServiceModel(s);
}
else {
s.setOrderSuccessMessage(message1);
req.setServiceModel(s);
}
}
CruxPanelClientService.updateServiceMaster(req);
}
m.setService_status("executed");
UpdateCronCruxRequest q = new UpdateCronCruxRequest();
q.setCronCruxModel(m);
CruxPanelClientService.updateCronCrux(q);
}
};`
推荐答案
问题是spring无法控制可运行对象的创建.有两种可能的解决方案:
The problem is spring doesn't control creation of your runnable. There are couple possible solutions:
- 将可运行的创建放入某些服务,存储库,控制器,组件或spring以前处理过的工作中:
示例:
@Service
public class SomeService {
@Autowired
private ICruxPanelClientService cruxPanelClientService;
public Runnable newRunnable() {
return new Runnable() {
public void run() {
cruxPanelClientService <- will be visible here and injected
}
}
}
}
- 使用原型范围创建可运行为Bean
示例:
@Configuration
public class Runnableconfiguration {
@Bean
@Scope("prototype")
public Runnable newRunnbale(final ICruxPanelClientService cruxPanelClientService) {
return new Runnable() {
public void run() {
cruxPanelClientService <- will be visible here
}
}
}
}
这篇关于@Autowired在可运行内部无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文