To inject a non-spring POJO into a Spring Boot application:
- create an ApplicationListener
- in the listener call registerSingleton with the non-spring object
- register the listener with the app
this allows the field to be initialized early during the application lifecycle
@RestController
@SpringBootApplication
public class SpringLegacy {
public SpringLegacy() {}
@Lazy
@Autowired
@Qualifier("legacyBean")
Object myBean;
@RequestMapping("/dir")
public Object dir() {
return "hello world " + myBean;
}
static class LegacyObject {}
public static void main(final String[] args) throws Exception {
Object obj = new LegacyObject();
SpringApplication app = new SpringApplication(SpringLegacy.class);
ApplicationListener lis = new ApplicationListener() {
boolean first = true;
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (first & event instanceof ContextRefreshedEvent) {
((GenericApplicationContext) (((ContextRefreshedEvent) event).getApplicationContext())).
getBeanFactory().registerSingleton("legacyBean", obj);
first = false;
}
}
};
app.setListeners(Arrays.asList(lis));
app.run(args);
}
}
full source is https://github.com/nqzero/spring-legacy
run the maven project, and then browse to localhost:8080/dir - you should see "LegacyObject" in the output
No comments:
Post a Comment