`
faith789510
  • 浏览: 1185 次
社区版块
存档分类
最新评论

MyAnnotationBeanFactoryPostProcessor

阅读更多
Spring自定义的注解扫描实现Demo
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.Constants;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import org.springframework.core.type.filter.TypeFilter;


/**
 * BeanDefinitionRegistryPostProcessor||BeanFactoryPostProcessor 接口调用流程
 * @author Stony
 * 1.从AbstractApplicationContext.refresh()方法进去,
 * 2.obtainFreshBeanFactory()->refreshBeanFactory()->loadBeanDefinitons()
 * 	2.1 AnnotatedBeanDefinitionReader->ClassPathBeanDefinitionScanner
 *  2.2 XmlBeanDefinitionReader ->loadBeanDefinitions()
 * 3.prepareBeanFactory()准备工作后->postProcessBeanFactory(),调用了invokeBeanFactoryPostProcessors(beanFactory)方法。
 * 4.获取全部BeanFactoryPostProcessor 接口类,执行AbstractApplicationContext.invokeBeanFactoryPostProcessors()->遍历全部实体执行postProcessBeanFactory()
 * 5.initApplicationEventMulticaster()初始化多路广播,->registerListeners()注册 ApplicationEvent事件
 * 6.finishRefresh() 通知ContextRefreshedEvent事件
 */
public class MyAnnotationBeanFactoryPostProcessor implements BeanFactoryPostProcessor, ApplicationContextAware{

	ApplicationContext ac;
	String annotationPackage;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.ac = applicationContext;
		
	}
	
	public String getAnnotationPackage() {
		return annotationPackage;
	}

	public void setAnnotationPackage(String annotationPackage) {
		this.annotationPackage = annotationPackage;
	}

	Pattern COMMA_SPLIT_PATTERN = Pattern.compile("\\s*[,]+\\s*");
	
	/**
	 * {@link BeanDefinitionRegistryPostProcessor}
	 */
	@Override
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
		if (beanFactory instanceof BeanDefinitionRegistry) {
			//definition scanner
			ClassPathBeanDefinitionScanner scanner = new MyAnnotationScan((BeanDefinitionRegistry) beanFactory, true);
			
			//scan packages 
			String[] packages = COMMA_SPLIT_PATTERN.split(annotationPackage);
			scanner.scan(packages);
		}
	}
	
	static class MyAnnotationScan extends ClassPathBeanDefinitionScanner{
		public MyAnnotationScan(BeanDefinitionRegistry registry,boolean useDefaultFilters) {
			super(registry, useDefaultFilters);
			init();
		}

		private void init() {
			TypeFilter filter = new AnnotationTypeFilter(MyAnnotation.class);
			addIncludeFilter(filter);
			addExcludeFilter(new TypeFilter() {
				@Override
				public boolean match(MetadataReader metadataReader,MetadataReaderFactory metadataReaderFactory) throws IOException {
					  String className = metadataReader.getClassMetadata().getClassName();
			          return className.endsWith("package-info");
				}
			});
		}
	}
}
//########################################################################
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyAnnotation {
	
	public String value() default "";
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics