1 定义EnableScanner注解,作用是对指定的packages文件夹下的类进行特殊处理,比如设置特殊值等!
package com.cyb.myenable;
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;
import org.springframework.context.annotation.Import;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(ScannerPackageRegistrar.class)
public @interface EnableScanner {
String[] packages();
}
2 定义注解引入的执行类,并将其注册到spring容器中
package com.cyb.myenable;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
*作者 : iechenyb<br>
*类描述: 说点啥<br>
*创建时间: 2019年2月1日
*/
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
public class ScannerPackageRegistrar implements ImportBeanDefinitionRegistrar {
Log log = LogFactory.getLog(ScannerPackageRegistrar.class);
public void registerBeanDefinitions(AnnotationMetadata annotationMetadata,
BeanDefinitionRegistry beanDefinitionRegistry) {
System.out.println(
"packages value::" + annotationMetadata.getAnnotationAttributes(EnableScanner.class.getName()));
String[] arrs = (String[]) annotationMetadata.getAnnotationAttributes(EnableScanner.class.getName())
.get("packages");
List<String> packages = Arrays.asList(arrs);
System.out.println(packages);
BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(MyBeanDefinitionProcessor.class);
bdb.addPropertyValue("packages", packages);
beanDefinitionRegistry.registerBeanDefinition(MyBeanDefinitionProcessor.class.getName(),
bdb.getBeanDefinition());
}
}
3 定义bean前后置处理器,对指定包里的类进行特殊的初始化。
package com.cyb.myenable;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
/**
*作者 : iechenyb<br>
*类描述: 所有的bean定义时添加前后处理逻辑<br>
*创建时间: 2019年2月1日
*/
public class MyBeanDefinitionProcessor implements BeanPostProcessor{
Log log = LogFactory.getLog(MyBeanDefinitionProcessor.class);
private List<String> packages;
public List<String> getPackages() {
return packages;
}
public void setPackages(List<String> packages) {
this.packages = packages;
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
for (String pkg : packages) {
if(bean.getClass().getName().startsWith(pkg)){
System.out.println("instance bean:"+bean.getClass().getName());
}
}
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
4 定义配置类,启动注解
package com.cyb.myenable;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Configuration;
/**
*作者 : iechenyb<br>
*类描述: 说点啥<br>
*创建时间: 2019年2月1日
*/
@Configuration
@EnableScanner(packages={"com.cyb.adapter","com.cyb.advice"})
public class ScannerConfiguration {
Log log = LogFactory.getLog(ScannerConfiguration.class);
}