在加载完所有been之后,如何在Spring容器中运行一些代码?我知道我可以对单个bean使用@PostConstruct,但是在调用所有的 PostConstructs之后,我想运行这段代码。有可能吗?
--更新--
我试着遵循ApplicationListener的方式,这就是实现:
@Component
public class PostContructListener implements ApplicationListener<ContextRefreshedEvent> {
private static Logger log = LoggerFactory.getLogger(PostContructListener.class);
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
Collection<Initializable> inits= contextRefreshedEvent.getApplicationContext().getBeansOfType(Initializable.class).values();
for (Initializable initializable : inits) {
try{
log.debug("Initialization {} ",initializable.getClass().getSimpleName());
initializable.init();
}catch(Exception e){
log.error("Error initializing {} ",initializable.getClass().getSimpleName(),e);
}
}
}
}将“初始化”接口应用于我所需要的所有服务,以及如何以这种方式破坏所有自动文件,我不明白为什么但似乎要连接到新的“可初始化”接口:
java.lang.IllegalArgumentException: Can not set com.service.MyService field com.controller.RestMultiController.myService to com.sun.proxy.$Proxy41发布于 2016-04-25 10:32:40
我觉得你需要这个。
public class SpringListener implements ApplicationListener<ContextRefreshedEvent>{
public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent ) {
// do things here
}
}http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#context-functionality-events
https://stackoverflow.com/questions/36836655
复制相似问题