TypechoJoeTheme

香草物语

统计
登录
用户名
密码
/
注册
用户名
邮箱
输入密码
确认密码

理解SpringBoot 中的@AliasFor注解

Laughing博主
2021-12-05
/
0 评论
/
995 阅读
/
204 个字
/
百度已收录
12/05
本文最后更新于2024年03月16日,已超过187天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!

感觉Spring Boot中的@AliasFor注解是一个既熟悉又陌生的注解。
说熟悉,是因为我们经常使用的比如@Service@RestController@Repository甚至@SpringBootApplication中都有他们的身影。
说陌生,是因为其实从来没有真正用过这个注解。
@AliasFor注解有两个作用:

  1. 定义一个注解中的两个属性互为别名。
  2. 桥接其他注解的属性。

一、定义一个注解中的两个属性互为别名。

我们以ComponentScan注解为例,先来看下ComponentScan的定义

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Repeatable(ComponentScans.class)
public @interface ComponentScan {
    @AliasFor("basePackages")
    String[] value() default {};

    @AliasFor("value")
    String[] basePackages() default {};

    Class<?>[] basePackageClasses() default {};

    Class<? extends BeanNameGenerator> nameGenerator() default BeanNameGenerator.class;

    Class<? extends ScopeMetadataResolver> scopeResolver() default AnnotationScopeMetadataResolver.class;

    ScopedProxyMode scopedProxy() default ScopedProxyMode.DEFAULT;

    String resourcePattern() default "**/*.class";

    boolean useDefaultFilters() default true;

    ComponentScan.Filter[] includeFilters() default {};

    ComponentScan.Filter[] excludeFilters() default {};

    boolean lazyInit() default false;

    @Retention(RetentionPolicy.RUNTIME)
    @Target({})
    public @interface Filter {
        FilterType type() default FilterType.ANNOTATION;

        @AliasFor("classes")
        Class<?>[] value() default {};

        @AliasFor("value")
        Class<?>[] classes() default {};

        String[] pattern() default {};
    }
}

以上代码,我们关注点在于


    @AliasFor("basePackages")
    String[] value() default {};

    @AliasFor("value")
    String[] basePackages() default {};

这段代码,代表着,在ComponentScan注解中,basePackages属性与value是一样的,可以相互调用。
我们可以测试一下

@ComponentScan(basePackages = {"net.xiangcaowuyu.demo"})
@Component
@EnableAutoConfiguration
@Slf4j
public class DemoApplication {

    public static void main(String[] args) {

        ComponentScan componentScan = AnnotatedElementUtils.getMergedAnnotation(DemoApplication.class, ComponentScan.class);
        assert componentScan != null;
        System.out.println(Arrays.toString(componentScan.basePackages()));
        System.out.println(Arrays.toString(componentScan.value()));

        SpringApplication.run(DemoApplication.class, args);
    }

}

二、桥接其他注解的属性

这次,我们看下@Service的代码

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Service {
    @AliasFor(
        annotation = Component.class
    )
    String value() default "";
}

可以看到,@Servicevalue属性,桥接到了@Component注解的value属性。

@ComponentScan(basePackages = {"net.xiangcaowuyu.demo"})
@Component
@EnableAutoConfiguration
@Slf4j
public class DemoApplication {

    public static void main(String[] args) {

        Component component = AnnotatedElementUtils.getMergedAnnotation(BookService.class, Component.class);
        assert component != null;
        System.out.println(component.value());

        SpringApplication.run(DemoApplication.class, args);
    }

}
JavaSpring Bootspring
朗读
赞(1)
赞赏
感谢您的支持,我会继续努力哒!
版权属于:

香草物语

本文链接:

https://www.xiangcaowuyu.net/java/understand-the-aliasfor-annotation-in-springboot.html(转载时请注明本文出处及文章链接)

评论 (0)