项目应用之-如何在ioc容器初始化以后自动触发某个方法完成某些业务逻辑或者配置

本人的解决方案如下:

实现InitializingBean接口并实现afterPropertiesSet( ) 方法,该方法会在ioc容器初始化时被执行

@Component
public class InitialDemo implements InitializingBean {
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("ioc容器初始化");
//todo 初始化自己项目需要的配置或者填充需要的业务逻辑 } }

输入结果:

信息: Could not instantiate TestExecutionListener class [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their dependencies) available.
六月 18, 2018 10:45:30 上午 org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
信息: Could not instantiate TestExecutionListener class [org.springframework.test.context.transaction.TransactionalTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their dependencies) available.
六月 18, 2018 10:45:31 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring.xml]
六月 18, 2018 10:45:31 上午 org.springframework.context.support.GenericApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@6dde5c8c: startup date [Mon Jun 18 10:45:31 CST 2018]; root of context hierarchy
ioc容器初始化

这是在项目应用中经常会用到的小技巧,比如需要在容器初始化完成以后初始化某些必要的配置,我认为这是一种比较好的实现方式之一,后续要其它的方式会继续补充。

posted @ 2018-06-18 12:20  大熊好好写代码  阅读(383)  评论(0编辑  收藏  举报