简体   繁体   English

自动连线的带注释的Bean在侦听器类中为null

[英]Autowired annotated bean gets null in Listener class

I get null logservice in after method. 方法得到空的日志服务

I used @Component, @ComponentScan, @Service and @Configuration, but none worked. 我使用了@ Component,@ ComponentScan,@ Service和@Configuration,但是没有一个起作用。

Here is my Listener class 这是我的侦听器

@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");
   }

}

Here is my Entity class 这是我的实体

@EntityListeners(LogOrder.class)
@Entity
public class Order{
}

Here is LogService interface and its implementation 这是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);
    }
}

What can I do to get LogService not null. 我该怎么办才能使LogService不为null。

You are trying to autowire a static field. 您正在尝试自动连接静态字段。

   private static LogService logService;

Remove static from the attribute. 从属性中删除静态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM