推广

手写Spring Boot@Enablexxx模块驱动

iseeyu2年前 (2024-02-22)推广142

spring-boot-enable代码目录结构.png

  • springboot项目默认扫描引导类目录下有@Componet注解的类或@Componet注解的派生注解,如@Configuration、@Service等注解,而@Import注解的功能是装载导入类,为了演示@Import注解的功能实现@Enable模块,示例代码没写在引导类SpringBootEnableApplication的com.dayue.springbootenable包目录下,避免被springboot扫描并自动装载,故写在了com.dayue.enable.xxxx包目录下。

  • (1)方式一:“注解驱动”,@Import导入xxxConfiguration类
    • domain Student类
    @Data
    public class Student {
    
        private String name;
    
        private Integer age;
    }
    
    • domain Teacher类
    @Data
    public class Teacher {
    
        private String name;
    
        private Integer age;
    }
    
    • HelloWorldConfiguration类
    @Configuration
    @Slf4j
    public class HelloWorldConfiguration {
    
        @Bean
        public Student student() {
            log.info("初始化student");
            return new Student();
        }
    }
    
    • @EnableHelloWorld注解
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Import(HelloWorldConfiguration.class)
    public @interface EnableHelloWorld {
    }
    

    启动项目,我们会发现Student bean并没有被springboot扫描并装载,然后在引导类SpringBootEnableApplication上面加注解@EnableHelloWorld,再次启动项目,通过日志可以看到Student bean已经被初始化了。

    @SpringBootApplication
    @EnableHelloWorld
    public class SpringBootEnableApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBootEnableApplication.class, args);
        }
    
    }
    

    方式一注解驱动,启动项目.png

    (2)方式二:接口编程,@Import导入实现ImportSelector类

    使用接口编程方式实现@Enable模块,需要实现ImportSelector类或ImportBeanDefinitionRegistrar类,相对于方式一,导入xxxConfiguration类,实现ImportSelector类和实现ImportBeanDefinitionRegistrar类的方式弹性更大,可以动态地选择一个或者多个@Componet类进行导入,使用的是Spring注解元信息AnnotationMetadata作为方法参数。

    示例代码通过一个接口,两个接口实现类,在@Enable模块中通过传入枚举值实现动态选择其中一个实现类注册为Spring Bean供controller层来使用。

    • ActionType枚举类
    public enum ActionType {
    
        /**
         * 老师
         */
        TEACHER,
        /**
         * 学生
         */
        STUDENT
    }
    
    • @EnableActionServer注解,后面实现ImportSelector类通过枚举值来动态选择实现类,老师上课或者学生上课
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Import(ServerImportSelector.class)
    public @interface EnableActionServer {
    
        /**
         * 设置动作类型,默认为老师上课
         */
        ActionType serverType() default ActionType.TEACHER;
    }
    
    
    • ActionServer接口类
    public interface ActionServer {
    
        /**
         * 动作
         */
        String action();
    }
    
    • StudentActionServerImpl实现类
    @Slf4j
    @Service
    public class StudentActionServerImpl implements ActionServer {
    
        @Bean
        public Student studentone() {
            log.info("初始化student bean");
            return new Student();
        }
    
        @Override
        public String action() {
            log.info("学生上课");
            return "学生上课";
        }
    
    }
    
    • TeacherActionServerImpl实现类
    @Slf4j
    @Service
    public class TeacherActionServerImpl implements ActionServer {
    
        @Bean
        public Teacher teacherone() {
            log.info("初始化teacher bean");
            return new Teacher();
        }
    
        @Override
        public String action() {
            log.info("老师上课");
            return "老师上课";
        }
    }
    
    • ServerImportSelector实现类,实现ImportSelector的selectImports方法,方法参数是Spring注解元信息AnnotationMetadata,通过该参数可以获取注解@EnableActionServer相关元信息,如@EnableActionServer注解的属性方法serverType(),返回其定义好的值,代码通过判断定义的ActionType来选择返回不同的类名。
    @Slf4j
    public class ServerImportSelector implements ImportSelector {
    
        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            //获取注解@EnableActionServer元信息
            Map<String, Object> annotationAttributes = importingClassMetadata.getAnnotationAttributes(EnableActionServer.class.getName());
            assert annotationAttributes != null;
            //通过获取的注解@EnableActionServer元信息,取定义好的方法属性,并把对象Object强制转换为ActionType枚举类型
            ActionType serverType = (ActionType) annotationAttributes.get("serverType");
            log.info("serverType:{},getServerType(serverType):{}", serverType, getServerType(serverType));
            //最后返回要选择的类名,注册为spring bean组件
            return new String[]{getServerType(serverType)};
        }
        
        //封装一层,通过枚举值返回不同的类名
        public String getServerType(ActionType serverType) {
            Map<ActionType, String> serverClassMap; serverClassMap = new HashMap<>(2);
            serverClassMap.put(ActionType.TEACHER, TeacherActionServerImpl.class.getName());
            serverClassMap.put(ActionType.STUDENT, StudentActionServerImpl.class.getName());
            return serverClassMap.get(serverType);
        }
    
    }
    

    在controller层注入ActionServer,然后启动项目,可以发现报错了。报错是因为没有定义好Spring Bean,题外话:当然可以通过@Bean的方式去注入某个实现类或者在实现类上加@ Server注解再选择其中之一的实现类去注入。

    • ActionServerController类
    @RestController
    @RequestMapping("/api/actionServer")
    public class ActionServerController {
    
        @Autowired
        private ActionServer actionServer;
    
        @GetMapping("/actionType")
        public String actionType() {
            return actionServer.action();
        }
    }
    

    注入ActionServer类报错.png

    最后,我们也用实现ImportSelector接口类的方式去实现@Enable模块,那么在引导类上加上注解@EnableActionServer(serverType = ActionType.STUDENT)即可,再次启动项目

    @SpringBootApplication
    @EnableActionServer(serverType = ActionType.STUDENT)
    public class SpringBootEnableApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(SpringBootEnableApplication.class, args);
        }
    
    }
    

    方式二启动项目.png

    浏览器访接口.png

    (3)方式三:接口编程,@Import导入实现ImportBeanDefinitionRegistrar类

    ImportBeanDefinitionRegistrar相对于ImportSelector而言,编程复杂度更高,除了注解元信息AnnotationMetadata作为入参外,接口将定义Bean的注册交给开发人员。示例代码复用方式二的选择实现类逻辑,故直接通过new ServerImportSelector()的方式来复用实现好的selectImports方法,入参即为注解元信息AnnotationMetadata,返回类名,最后完成注册返回类名的类为Spring Bean。

    • ServerImportBeanDefinitionRegistrar实现ImportBeanDefinitionRegistrar类
    public class ServerImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    
        @Override
        public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
            //复用前面实现好的ImportSelector
            ImportSelector importSelector = new ServerImportSelector();
            //筛选ClassNames集合
            String[] selectClassNames = importSelector.selectImports(importingClassMetadata);
            Stream.of(selectClassNames)
                    //转化为 BeanDefinitionBuilder 对象
                    .map(BeanDefinitionBuilder::genericBeanDefinition)
                    //转化为 BeanDefinition 对象
                    .map(BeanDefinitionBuilder::getBeanDefinition)
                    //注册 BeanDefinition 到 BeanDefinitionRegistry
                    .forEach(beanDefinition -> BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinition, registry));
        }
    }
    

    接着修改前面写好的注解@EnableActionServer注解的@Import导入类,修改为@Import(ServerImportBeanDefinitionRegistrar.class)

    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    //@Import(ServerImportSelector.class)
    @Import(ServerImportBeanDefinitionRegistrar.class)
    public @interface EnableActionServer {
    
        /**
         * 设置动作类型,默认为老师上课
         */
        ActionType serverType() default ActionType.TEACHER;
    }
    

    再次启动项目即可观察结果。

    参考资料:
    《Spring Boot编程思想》

    扫描二维码推送至手机访问。

    版权声明:本文由西安泽虎代运营发布,如需转载请注明出处。

    转载请注明出处https://www.0291.com.cn/post/56267.html

    相关文章

    android接入微信支付

    android接入微信支付

    image 4.在清单文件中配置微信支付 (android:exported=”true” 必须加) <activity android:name=".wxapi.WXPayEntryActivity" android...

    陈志武:财富是怎样产生的?

    陈志武:财富是怎样产生的?

      说到财富,我们会认为一个国家富不富,关键取决于其自然资源的多少。小时候在湖南上学,我们学到中国“地大物博”的事实,并认识到正因为是这些丰富的自然资源,所以我们中国是多么富有。到了美国,我们发现美国也是“地大物博”,而且美国更富有。当然,相比之下,日本的自然资源有限,尤其...

    代运营项目了几十万短视频,积攒了这些实战经验。

    代运营项目了几十万短视频,积攒了这些实战经验。

    短视频火起来之后,不少企业想快速入局,在高流量的短视频池子里为自己的品牌或产品赢得一席之地。比起自己从头开始招兵搭建团队,再花上几个月的时间去运营账号,面对最后时间搭进去了粉丝还是寥寥无几的风险,代运营的一句冲天宣传“x个月帮你运营粉丝几十万/上百万”,显得尤其让人心动。 笔者全程经手操盘短视频的...

    CRM应用策略:企业级销售管理完整指南

    CRM应用策略:企业级销售管理完整指南

    与被玩坏了的“企业级理解”不同,企业级销售是一门扎扎实实的、严肃的、影响公司存亡的课题。毕竟,100万的产品合同和向个人出售100元的产品,这两种销售截然不同。向企业出售高额产品的销售过程被称为企业级销售或复杂销售,销售周期可能持续数月。它涉及与潜在客户建立关系并制定解决他...

    门迪:遭性侵了还要去查查别人有没有钱?够现实

    门迪:遭性侵了还要去查查别人有没有钱?够现实

    曼城后卫门迪在被多名女性指控强奸,但最近据镜报透露,指控门迪的女性在警方通报之前在网络上用谷歌搜索门迪的财富收入和身价,并且律师在法庭上曾经质问过该女性的动机。门迪目前身上背着10项性侵罪名,目前还在法院审理当中,警方发布通告称在年初一位自称被门迪性侵的女性在报警之前就在网...

    B站推广怎么投放,博主太难相处啦

    B站的分为几种:起飞,商单,商单起飞,CPC起飞:是指优质被平台推荐。商单:是指博主接了广告被满屏刷“恰饭了 恰饭了 恰饭了“1:为什么要选择在B站投放Z时代(90/00后),所有的行业报告都写年轻人群消费能力高balabala的,感觉这样认定还是有点片面,潜力比较大是确实...

    现在,非常期待与您的又一次邂逅

    我们努力让每一部企业宣传片和抖音短视频成为商业大片