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

相關文章
相關標籤/搜索