在静态类实例化期间,自动装配的对象为null

时间:2018-10-17 17:55:57

标签: spring-boot nullpointerexception autowired

我在Spring Boot中有一个主班,如下所示:

public class Application extends MicroService {
private static Application app = null;

public static synchronized Application getApp() {
    if(app == null) {
        app = new Application();
    }
    return app;
}

public static void main(String[] args) {
    getApp().run(args);
}
}  

基类

@Service
@ComponentScan({"com.example.microservice"})
@SpringBootApplication
public class MicroService {

private static final Logger LOGGER = LoggerFactory.getLogger(MicroService.class);
private static ConfigurableApplicationContext context;

private MicroServiceProperties properties;
private KafkaSender kafkaSender;

@Autowired
public void setProperties(MicroServiceProperties properties) {
    this.properties = properties;
}

@Autowired
public void setKafkaSender(KafkaSender kafkaSender) {
    this.kafkaSender = kafkaSender;
}

当我像这样访问自动装配对象时,我得到了NullPointerException。

Application.getApp().getKafkaSender().send(data);

0 个答案:

没有答案