[英]Autowired bean is null in thread location
如何从线程访问exampleService? 在新线程中为null。 ExampleService是带注释的@Service类。 我从dao设置的SessionFactory,它不是null。
public class ExampleInterceptor extends EmptyInterceptor {
private Logger logger = Logger.getLogger(getClass());
private static final long serialVersionUID = 1L;
@Autowired
SessionFactory sessionFactory;
@Autowired
ExampleService exampleService;
public void setExampleService(ExampleService exampleService) {
this.exampleService = exampleService;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public void afterTransactionCompletion(Transaction tx) {
new Thread(new SimpleThread(exampleService)).start();
}
protected class SimpleThread implements Runnable {
protected ExampleService exampleService;
public SimpleThread() {
};
public SimpleThread(ExampleService exampleService) {
this.exampleService = exampleService;
}
@Override
public void run() {
exampleService.method(); // is null at this location
}
}
}
您需要使用@Service注释服务类,并且要使用该对象,需要对其进行自动装配。 如果我没有记错的话,在您的“ SimpleThread”类中,您正在使用的不是自动装配属性“ protected ExampleService exampleService”,这是没有@Autowired注释的属性。 @ Service,@ Controller,@ Repository和@Component
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.