我在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);