spring容器初始化后作一些初始化工做,以及spring boot 事件监听

spring普通项目中使用方式

方法一implements ApplicationListener<ContextRefreshedEvent> 这个接口java

方法二:其实更简单的方法是使用注解:`@PostConstruct`,只须要在须要启动的时候执行的方法上标注这个注解就搞定了。spring

方法三、方法实现InitializingBean或者ServletContextAwarespringboot

package org.lanqiao.rpc.demo04_pb_with_netty.demo02_with_rpc.media;

import java.lang.reflect.Method;
import java.util.Map;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

/**
 * spring容器初始化成功后,执行,
 * @author Administrator
 *
 */
//@Component
public class InitMedia implements ApplicationListener<ContextRefreshedEvent>,Ordered{

	
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		Map<String, Object> beanMap = event.getApplicationContext().getBeansWithAnnotation(Controller.class);

		for(String key : beanMap.keySet()){
			Object bean = beanMap.get(key);
			Method[] methods = bean.getClass().getDeclaredMethods();
			if(methods==null || methods.length==0){
				continue;
			}
			
			for(Method m : methods){
				if(m.isAnnotationPresent(Remote.class)){
					Remote r = m.getAnnotation(Remote.class);
					String cmd = r.value();
					MethodBean methodBean = new MethodBean();
					methodBean.setBean(bean);
					methodBean.setMethod(m);
					Media.methodBeans.put(cmd, methodBean);
					System.out.println("cmd==="+cmd);
				}
				
				
			}
			
			
		}
		
		
	}

	@Override
	public int getOrder() {
		return Ordered.HIGHEST_PRECEDENCE;
	}

}

spring boot在启动过程当中增长事件监听机制

支持的事件类型有6种(下面是常见4种)app

ApplicationStartedEventide

ApplicationEnvironmentPreparedEventspa

ApplicationPreparedEvent.net

ApplicationFailedEventnetty

实现监听步骤:code

1.监听类实现ApplicationListener接口 
2.将监听类添加到SpringApplication实例blog

ApplicationStartedEvent

ApplicationStartedEvent:spring boot启动开始时执行的事件

建立对应的监听类 MyApplicationStartedEventListener.Java

package com.lkl.springboot.listener;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;

/**
 * spring boot 启动监听类
 * 
 * @author liaokailin
 * @version $Id: MyApplicationStartedEventListener.java, v 0.1 2015年9月2日 下午11:06:04 liaokailin Exp $
 */
public class MyApplicationStartedEventListener implements ApplicationListener<ApplicationStartedEvent> {

    private Logger logger = LoggerFactory.getLogger(MyApplicationStartedEventListener.class);

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event) {
        SpringApplication app = event.getSpringApplication();
        app.setShowBanner(false);// 不显示banner信息
        logger.info("==MyApplicationStartedEventListener==");
    }
}

 

参考资料:

Spring容器中的Bean几种初始化方法和销毁方法的前后顺序:

http://www.javashuo.com/article/p-zxkeqvpi-mm.html

当spring 容器初始化完成后执行某个方法:

http://blog.csdn.net/ye1992/article/details/52022450

spring boot实战(第二篇)事件监听

http://blog.csdn.net/liaokailin/article/details/48186331

相关文章
相关标签/搜索