public class SpringContextHolder implements ApplicationContextAware{
private static ApplicationContext context;
private static BeanDefinitionRegistry beanDefinitonRegistry;
public static ApplicationContext getApplicationContext() {
return context;
}
public static Object getBean(String name){
return context.getBean(name);
}
public static <T> T getBean(Class<T> clz){
return context.getBean(clz);
}
public static <T> T getBean(String name , Class<T> clz){
return context.getBean(name, clz);
}
@Override
public void setApplicationContext(ApplicationContext ac)
throws BeansException {
context = ac;
//DefaultListableBeanFactory acf = (DefaultListableBeanFactory) app.getAutowireCapableBeanFactory();
ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) context;
beanDefinitonRegistry = (BeanDefinitionRegistry) configurableApplicationContext
.getBeanFactory();
}
/**
* 动态注册bean
* @param beanName
* @param beanDefinition
*/
public synchronized static void registerBean(String beanName, BeanDefinition beanDefinition){
//DefaultListableBeanFactory beanDefinitonRegistry = (DefaultListableBeanFactory) app.getAutowireCapableBeanFactory();
if(!beanDefinitonRegistry.containsBeanDefinition(beanName)){
beanDefinitonRegistry.registerBeanDefinition(beanName, beanDefinition);
}
}
public static void registerBean(BeanDefinition beanDefinition){
//DefaultListableBeanFactory beanDefinitonRegistry = (DefaultListableBeanFactory) app.getAutowireCapableBeanFactory();
String simpleNameString=beanDefinition.getBeanClassName();
if(simpleNameString.contains(".")){
simpleNameString=simpleNameString.substring(simpleNameString.lastIndexOf(".")+1);
}
simpleNameString=StringUtil.lowerFirstChar(simpleNameString);
registerBean(simpleNameString,beanDefinition);
}
public static BeanDefinitionBuilder getBeanDefinitionBuilder(Class clazz){
return BeanDefinitionBuilder.genericBeanDefinition(clazz);
}
}
public class ClassScanner {
private final Logger logger = LoggerFactory.getLogger(getClass());
private static final String RESOURCE_PATTERN = "**/%s/**/*.class";
private ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
public ClassScanner(){
}
@SafeVarargs
public final Set<Class<?>> scan(String[] confPkgs, Class<? extends Annotation>... annotationTags){
Set<Class<?>> resClazzSet = new HashSet<>();
List<AnnotationTypeFilter> typeFilters = new LinkedList<>();
if (ArrayUtils.isNotEmpty(annotationTags)){
for (Class<? extends Annotation> annotation : annotationTags) {
typeFilters.add(new AnnotationTypeFilter(annotation, false));
}
}
if (ArrayUtils.isNotEmpty(confPkgs)) {
for (String pkg : confPkgs) {
String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + String.format(RESOURCE_PATTERN,ClassUtils.convertClassNameToResourcePath(pkg));
try {
Resource[] resources = this.resourcePatternResolver.getResources(pattern);
MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(this.resourcePatternResolver);
for (Resource resource : resources) {
if (resource.isReadable()) {
MetadataReader reader = readerFactory.getMetadataReader(resource);
String className = reader.getClassMetadata().getClassName();
if (ifMatchesEntityType(reader, readerFactory,typeFilters)) {
Class<?> curClass = Thread.currentThread().getContextClassLoader().loadClass(className);
resClazzSet.add(curClass);
}
}
}
} catch (Exception e) {
logger.error("扫描提取[{}]包路径下,标记了注解[{}]的类出现异常", pattern,StringUtils.join(typeFilters,","));
}
}
}
return resClazzSet;
}
/**
* 注册Bean
* @param classList
*/
public void regist(List<Class<?>> classList){
for(Class<?> clzz:classList){
SpringContextHolder.registerBean(SpringContextHolder.getBeanDefinitionBuilder(clzz).getBeanDefinition());
}
}
/**
* 检查当前扫描到的类是否含有任何一个指定的注解标记
* @param reader
* @param readerFactory
* @return ture/false
*/
private boolean ifMatchesEntityType(MetadataReader reader, MetadataReaderFactory readerFactory,List<AnnotationTypeFilter> typeFilters) {
if (!CollectionUtils.isEmpty(typeFilters)) {
for (TypeFilter filter : typeFilters) {
try {
if (filter.match(reader, readerFactory)) {
return true;
}
} catch (IOException e) {
logger.error("过滤匹配类型时出错 {}",e.getMessage());
}
}
}
return false;
}
}