[英]Autowired bean is null in thread location
how can I access to exampleService from the thread ? 如何从线程访问exampleService? In new thread is null.
在新线程中为null。 ExampleService is an annotated @Service class.
ExampleService是带注释的@Service类。 SessionFactory I set from dao and it is no null.
我从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
}
}
}
You need to Annotate the Service Class with @Service, And for using that object you need to Autowire it. 您需要使用@Service注释服务类,并且要使用该对象,需要对其进行自动装配。 If i am not wrong, In your "SimpleThread" class, you are using not autowiring the property "protected ExampleService exampleService", which is a not having @Autowired annotation.
如果我没有记错的话,在您的“ SimpleThread”类中,您正在使用的不是自动装配属性“ protected ExampleService exampleService”,这是没有@Autowired注释的属性。 @Service, @Controller, @Repository & @Component
@ Service,@ Controller,@ Repository和@Component
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.