![](/img/trans.png)
[英]Autowired Environment variable is NULL when @Bean annotated method is executed
[英]Autowired annotated bean gets null in Listener class
我在方法后得到空的日志服務 。
我使用了@ Component,@ ComponentScan,@ Service和@Configuration,但是沒有一個起作用。
這是我的偵聽器類
@Component
public class LogOrder {
private static LogService logService;
@Autowired
public void setLogService(LogService logService) {
LogOrder.logService = logService;
}
@PostUpdate
private void after(Order order) {
logService.log("Logged");
}
}
這是我的實體課
@EntityListeners(LogOrder.class)
@Entity
public class Order{
}
這是LogService接口及其實現
public interface LogService {
void send("");
}
@Service(value = "logService")
public class LogServiceImpl implements LogService {
private final SomeOtherService someOtherService;
@Autowired
public LogServiceImpl(SomeOtherService someOtherService) {
this.someOtherService = someOtherService;
}
public void send(String someText) {
SomeTemplate someTemplate = someOtherService.someTemplate();
someTemplate.convertAndSend(someText);
}
}
我該怎么辦才能使LogService不為null。
您正在嘗試自動連接靜態字段。
private static LogService logService;
從屬性中刪除靜態 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.