spring笔记-AnnotationAttributes

1.注解的解析转换

通常对注解的解析之后,需要对注解的信息进行对象存储转换,
比如EnableMongoRepositories注解,有13个属性,如下定义

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(MongoRepositoriesRegistrar.class)
public @interface EnableMongoRepositories {

    /**
     * Alias for the {@link #basePackages()} attribute. Allows for more concise annotation declarations e.g.:
     * {@code @EnableJpaRepositories("org.my.pkg")} instead of {@code @EnableJpaRepositories(basePackages="org.my.pkg")}.
     */
    String[] value() default {};

    /**
     * Base packages to scan for annotated components. {@link #value()} is an alias for (and mutually exclusive with) this
     * attribute. Use {@link #basePackageClasses()} for a type-safe alternative to String-based package names.
     */
    String[] basePackages() default {};

    /**
     * Type-safe alternative to {@link #basePackages()} for specifying the packages to scan for annotated components. The
     * package of each class specified will be scanned. Consider creating a special no-op marker class or interface in
     * each package that serves no purpose other than being referenced by this attribute.
     */
    Class<?>[] basePackageClasses() default {};

    /**
     * Specifies which types are eligible for component scanning. Further narrows the set of candidate components from
     * everything in {@link #basePackages()} to everything in the base packages that matches the given filter or filters.
     */
    Filter[] includeFilters() default {};

    /**
     * Specifies which types are not eligible for component scanning.
     */
    Filter[] excludeFilters() default {};

    /**
     * Returns the postfix to be used when looking up custom repository implementations. Defaults to {@literal Impl}. So
     * for a repository named {@code PersonRepository} the corresponding implementation class will be looked up scanning
     * for {@code PersonRepositoryImpl}.
     * 
     * @return
     */
    String repositoryImplementationPostfix() default "Impl";

    /**
     * Configures the location of where to find the Spring Data named queries properties file. Will default to
     * {@code META-INFO/mongo-named-queries.properties}.
     * 
     * @return
     */
    String namedQueriesLocation() default "";

    /**
     * Returns the key of the {@link QueryLookupStrategy} to be used for lookup queries for query methods. Defaults to
     * {@link Key#CREATE_IF_NOT_FOUND}.
     * 
     * @return
     */
    Key queryLookupStrategy() default Key.CREATE_IF_NOT_FOUND;

    /**
     * Returns the {@link FactoryBean} class to be used for each repository instance. Defaults to
     * {@link MongoRepositoryFactoryBean}.
     * 
     * @return
     */
    Class<?> repositoryFactoryBeanClass() default MongoRepositoryFactoryBean.class;

    /**
     * Configures the name of the {@link MongoTemplate} bean to be used with the repositories detected.
     * 
     * @return
     */
    String mongoTemplateRef() default "mongoTemplate";

    /**
     * Whether to automatically create indexes for query methods defined in the repository interface.
     * 
     * @return
     */
    boolean createIndexesForQueryMethods() default false;

    /**
     * Configures whether nested repository-interfaces (e.g. defined as inner classes) should be discovered by the
     * repositories infrastructure.
     */
    boolean considerNestedRepositories() default false;
}

2.解析方法

2.1 ImportBeanDefinitionRegistrar接口的registerBeanDefinitions方法中有一个AnnotationMetadata类型的参数,可以调用getAnnotationAttributes方法来获取信息

public interface AnnotatedTypeMetadata {
        //...
    Map<String, Object> getAnnotationAttributes(String annotationName);

 

2.2 AnnotationAttributes

AnnotationAttributes可以更好的对Map进行封装处理,直接在构造函数中传入Map实例即可

 

2.3 AnnotatedElementUtils

可以使用AnnotatedElementUtils的getMergedAnnotationAttributes方法直接返回AnnotationAttributes实例对象

3.基于AnnotationAttributes更好的封装

比如上面说的EnableMongoRepositories,在实际中转换成对象则是AnnotationRepositoryConfigurationSource,如下代码

public class AnnotationRepositoryConfigurationSource extends RepositoryConfigurationSourceSupport {

    private static final String QUERY_LOOKUP_STRATEGY = "queryLookupStrategy";

    private final AnnotationAttributes attributes;

    public AnnotationRepositoryConfigurationSource(AnnotationMetadata metadata, Class<? extends Annotation> annotation,
            ResourceLoader resourceLoader, Environment environment) {
        this.attributes = new AnnotationAttributes(metadata.getAnnotationAttributes(annotation.getName()));
    }
    public Object getQueryLookupStrategyKey() {
        return attributes.get(QUERY_LOOKUP_STRATEGY);
    }
}

封装后,在实际使用时会有更好的体验


转自:https://www.jianshu.com/p/9fb8dfb02461

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值