Spring系列之手写自定义的@Enable*注解

神奇的@Enable*注解

在我们日常开发普通的spring-framework项目过程中@Enable用的不是很多,但在SpringBoot开发过程,我们经常会遇到@Enable开始的好多注解,比如@EnableWebMvc、@EnableEurekaServer、@EnableAsync、@EnableScheduling等,今天我们就来分析下这些注解到底是如何工作的,并写手自定的@Enable注解。

@Enable*实现的原理

以@EnableWebMvc为例,先看其源码

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {

可以看出,其本质是使用了@Import,导入的DelegatingWebMvcConfiguration类注解了@Configuration,表明是一个配置类。总结下,@Enable注解其实使用了@Import注解,将需要用到的类注入到容器中,这样我们就可以直接从容器中取出模块锁需要的bean对象使用。

手写自定义的@Enable*注解

1、自定义注解类 EnableMyMvc,此处使用的@Import的ImportSelector的实现类导入
tip:@Import具体使用可翻阅历史博客

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(MyMvcSelector.class)
public @interface EnableMyMvc {
}

2、上MyMvcSelector和MyMvc类代码

public class MyMvcSelector implements ImportSelector {
    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[]{MyMvc.class.getName()};
    }
}
public class MyMvc {
    public void doMvc(){
        System.out.println("MyMvc doMvc");
    }
}

3、看启动类和执行结果

@ComponentScan
@EnableMyMvc
public class SpringApplicationContext {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringApplicationContext.class);
        //得到MyMvc类的实例
        MyMvc myMvc = context.getBean(MyMvc.class);
        myMvc.doMvc();
        //关闭容器
        context.close();
    }
}

运行结果
从运行结果可知,由于我们在启动类加@EnableMyMvc注解,会将MyMvc类加载到spring容器中。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值