首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >容器的弹簧PostConstruct

容器的弹簧PostConstruct
EN

Stack Overflow用户
提问于 2016-04-25 09:28:43
回答 1查看 476关注 0票数 2

在加载完所有been之后,如何在Spring容器中运行一些代码?我知道我可以对单个bean使用@PostConstruct,但是在调用所有的 PostConstructs之后,我想运行这段代码。有可能吗?

--更新--

我试着遵循ApplicationListener的方式,这就是实现:

代码语言:javascript
复制
@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);
            }
        }
    }
}

将“初始化”接口应用于我所需要的所有服务,以及如何以这种方式破坏所有自动文件,我不明白为什么但似乎要连接到新的“可初始化”接口:

代码语言:javascript
复制
java.lang.IllegalArgumentException: Can not set com.service.MyService field com.controller.RestMultiController.myService to com.sun.proxy.$Proxy41
EN

回答 1

Stack Overflow用户

发布于 2016-04-25 10:32:40

我觉得你需要这个。

代码语言:javascript
复制
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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36836655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档