springboot 扫描自定义注解

作者:じ☆ve宝贝

发布时间:2018-03-09T15:53:33

package cn.studyjava;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * <p>
 * Description: 扫描自定义的注解
 * </p>
 * 
 * @author: zsljava
 * @version 1.0.0
 * <p>
 * History:
 * -----------------------------------------------
 * @Date: 2018年3月8日 下午1:44:53
 * @author: zsljava
 * @version 1.0.0
 * @OP: Create
 * -----------------------------------------------
 * </p>
 *
 * @since
 * @see
 */
@Component
public class ScanAnnotation implements ApplicationListener<ContextRefreshedEvent> {

	/**
	 * @Title: onApplicationEvent
	 * @Description: TODO
	 * @param event
	 * @see org.springframework.context.ApplicationListener#onApplicationEvent(org.springframework.context.ApplicationEvent)
	 */
	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		Map<String, Object> services = event.getApplicationContext().getBeansWithAnnotation(Table.class);// 自定义的注解
		
		Iterator<String> it = services.keySet().iterator();
		Map<String, Object> map = new HashMap<>(); 
		while(it.hasNext()){
			String key = it.next();
			System.out.println(key + ":" + services.get(key));
		}
	}

}