为什么80%的码农都做不了架构师?>>>
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
/**
* @ClassName ModelInital
* @Description 初始化注释Model
* @Date 2019/3/28 0028 15:14
* @Version 0.1
**/
@Component
public class ModelInital {
@Autowired
private ConfigurableListableBeanFactory beanFactory;
@Autowired
private ApplicationContext applicationContext;
private static final String RESOURCE_PATTERN = "com.xxx";
/**
* 初始化方法
*/
@PostConstruct
public void init() {
// 使用自定义扫描类,针对@Model进行扫描
AnnotationScanner scanner = AnnotationScanner.getScanner((BeanDefinitionRegistry) beanFactory, Model.class);
scanner.doScan(RESOURCE_PATTERN).forEach(beanDefinitionHolder -> {
Object o = applicationContext.getBean(beanDefinitionHolder.getBeanName());
Class> clazz = o.getClass();
Model model = clazz.getAnnotation(Model.class);
String newName = model.tableName();
Map
import lombok.Setter;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathBeanDefinitionScanner;
import org.springframework.core.type.filter.AnnotationTypeFilter;
import java.lang.annotation.Annotation;
import java.util.Set;
/**
* @ClassName AnnotationScanner
* @Description 自定义Model注解扫描器
* @Date 2019/3/28 0028 14:44
* @Version 0.1
**/
public class AnnotationScanner extends ClassPathBeanDefinitionScanner {
/**
* 实体类对应的AnnotationClazz
*/
@Setter
private Class extends Annotation> selfAnnotationClazz;
/**
* 传值使用的临时静态变量
*/
private static Class extends Annotation> staticTempAnnotationClazz = null;
/**
* 因构造函数无法传入指定的Annotation类,需使用静态方法来调用
* @param registry
* @param clazz
* @return
*/
public static synchronized AnnotationScanner getScanner(BeanDefinitionRegistry registry, Class extends Annotation> clazz){
staticTempAnnotationClazz = clazz;
AnnotationScanner scanner = new AnnotationScanner(registry);
scanner.setSelfAnnotationClazz(clazz);
return scanner;
}
private AnnotationScanner(BeanDefinitionRegistry registry) {
super(registry);
}
// 构造函数需调用函数,使用静态变量annotationClazz传值
@Override
public void registerDefaultFilters() {
// 添加需扫描的Annotation Class
this.addIncludeFilter(new AnnotationTypeFilter(staticTempAnnotationClazz));
}
// 以下为初始化后调用的方法
@Override
public Set doScan(String... basePackages) {
return super.doScan(basePackages);
}
@Override
public boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
return super.isCandidateComponent(beanDefinition)
&& beanDefinition.getMetadata().hasAnnotation(this.selfAnnotationClazz.getName());
}
}
import java.lang.annotation.*;
/**
* @ClassName Model
* @Description 转换类实体注解
* @Date 2019/3/28 0028 11:10
* @Version 0.1
**/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Model {
/**
* 表名
* @return
*/
String tableName() default "";
}
import java.lang.annotation.*;
/**
* @ClassName ModelProperty
* @Description 实体属性注解
* @Date 2019/3/28 0028 11:13
* @Version 0.1
**/
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ModelProperty {
/**
* 对应的字段名
* @return
*/
String value() default "";
}
使用方式就是让spring扫描到改组件
在自己的Pojo上面注解@Model就可以将model的相关注释@ModelProperty注入到spring容器中
使用的时候直接从spring容器中取就可以了
MutablePropertyValues mutablePropertyValues = beanFactory.getBeanDefinition(刚刚自定义的名字).getPropertyValues();
Map commentsMap = new HashMap<>();
mutablePropertyValues.forEach(propertyValue -> {
commentsMap.put(propertyValue.getName(), propertyValue.getValue());
});