Sring Boot 自动装配


title: Sring Boot 自动装配 date: 2019-07-18 categories:java

  • SpringBoot tags:
  • SpringBoot
  • @EnableAutoConfiguration
  • @Condition
  • spring.factories top: true toc: true

SpringBoot和新特性笔记。spring

  • 注解模式装配

    @sevice @Conrtroller @Repository @Componentspringboot

    这几个注解在Srping源码的文章中已经将结果了,这里就不在赘述了。ide

  • 条件(Condition)装配

    Condition 注解做为条件,若是符合条件则将bean注入到IOC中,反之则不注入,实际是使用 @Conditional注解来实现,继承 Condition 接口,经过 matches 方法进行逻辑判断是否符合条件。测试

    1: 建立ConditionOnSysProperty注解ui

    /** * @Auther: lantao * @Date: 2019-07-18 17:52 * @Company: 随行付支付有限公司 * @maill: lan_tao@suixingpay.com * @Description: TODO */
    @Retention(RetentionPolicy.RUNTIME)
    @Target({ElementType.TYPE, ElementType.METHOD})
    @Documented
    @Conditional({SysPropertyCondition.class})
    public @interface ConditionOnSysProperty {
    
        String value() default "lantao";
    }
    复制代码

    2:建立@Condtional所须要的条件 SysPropertyConditionspa

    /** * @Auther: lantao * @Date: 2019-07-18 17:52 * @Company: 随行付支付有限公司 * @maill: lan_tao@suixingpay.com * @Description: TODO */
    public class SysPropertyCondition implements Condition {
    
        /** * 匹配方法 * * @param context * @param metadata * @return */
        @Override
        public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
            // 获取 ConditionOnSysProperty 注解的属性
            Map<String, Object> attributes = metadata.getAnnotationAttributes(ConditionOnSysProperty.class.getName());
    
            String value = String.valueOf(attributes.get("value"));
    
            // 获取本机的user.name值
            String propertieValue = System.getProperties().get("user.name").toString();
    
            // 对比
            return value.equals(propertieValue);
        }
    }
    复制代码

    3:建立COnditionBootStrap测试code

    /** * @Auther: lantao * @Date: 2019-07-18 17:44 * @Company: 随行付支付有限公司 * @maill: lan_tao@suixingpay.com * @Description: TODO */
    public class ConditionBootStrap {
    
        @Bean
        // 这了由于value是lantao, 正好user.name也是lantao,因此条件成立,会将bean注入到ioc中
        @ConditionOnSysProperty(value = "lantao")
        private String helloWorld() {
            return "Hello World ! Condition";
        }
    
    
        public static void main(String[] args) {
            // 这种方式能够不使用 SpringBootApplication 注解
            ConfigurableApplicationContext context = new SpringApplicationBuilder(ConditionBootStrap.class).run(args);
    
            // 获取名为 helloWorld 的Bean,判断 ConditionOnSysProperty 条件是否生效
            String helloWorld = context.getBean("helloWorld", String.class);
    
            System.out.println(helloWorld);
    
            // 关闭
            context.close();
        }
    }
    
    结果:
    Hello World ! Condition
    复制代码
  • @Enable 模块装配

    Eanble注解内部使用@Importbean注入到ioc中,@import注解中能够直接放入bean,也能够作更灵活的配置,使用继承了ImportSeletor接口的bean,能够根据@Enable注解的属性(attrbutes)进行灵活的动态判断cdn

    1: 建立 EnableHelloWorld继承

    /** * @Auther: lantao * @Date: 2019-07-18 18:03 * @Company: 随行付支付有限公司 * @maill: lan_tao@suixingpay.com * @Description: TODO */
    
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    // 直接使用没法灵活判断
    //@Import(TestConfiguration.class)
    
    // 使用 HelloWorldImportSeletor 能够有更灵活的判断
    @Import(HelloWorldImportSeletor.class)
    public @interface EnableHelloWorld {
    
        String name() default "lantao";
    }
    复制代码

    2: 建立TestConfiguration和Test1Configuration

    public class TestConfiguration {
        @Bean
        private String helloWorld() {
            return "hello World! Enable";
        }
    }
    
    public class Test1Configuration {
        @Bean
        public String buzhidao() {
            return "不知道";
        }
    }
    
    复制代码

    3: 建立HelloWorldImportSeletor

    /** * @Auther: lantao * @Date: 2019-07-19 09:55 * @Company: 随行付支付有限公司 * @maill: lan_tao@suixingpay.com * @Description: TODO */
    public class HelloWorldImportSeletor implements ImportSelector {
    
        @Override
        public String[] selectImports(AnnotationMetadata importingClassMetadata) {
            // 获取EnableHelloWorld注解的属性
            Map<String, Object> attributes = importingClassMetadata.getAnnotationAttributes(EnableHelloWorld.class.getName());
            // 根据attributes 灵活判断注入那个bean
            if ("lantao".equals(attributes.get("name"))) {
                System.out.println(TestConfiguration.class.getName());
                return new String[]{TestConfiguration.class.getName()};
            }
            System.out.println(TestConfiguration.class.getName());
            return new String[]{Test1Configuration.class.getName()};
        }
    }
    复制代码

    4:建立bootStrap测试

    /** * @Auther: lantao * @Date: 2019-07-18 18:04 * @Company: 随行付支付有限公司 * @maill: lan_tao@suixingpay.com * @Description: TODO */
    @EnableHelloWorld
    //@EnableHelloWorld(name = "buzhidao")
    public class EnableBootStrap {
        public static void main(String[] args) {
    
            // 这种方式能够不使用 SpringBootApplication 注解
            ConfigurableApplicationContext context = new SpringApplicationBuilder(EnableBootStrap.class).run(args);
    
            // 获取名为 helloWorld 的Bean,判断 ConditionOnSysProperty 条件是否生效
            String helloWorld = context.getBean("helloWorld", String.class);
          // 这里能够获取 bean名称为 'buzhidao',须要注解@EnableHelloWorld(name = "buzhidao"),
          // 由于@EnableHelloWorld的name默认值是lantao,符合Condition的条件判断
    // String helloWorld = context.getBean("buzhidao", String.class);
    
            System.out.println(helloWorld);
    
            // 关闭
            context.close();
        }
    }
    
    结果:
    hello World! Enable
    复制代码
  • 工厂模式装配

    自定义spring.factories,工厂模式装配能够自定义starter。

    1: 建立SpringFactoriesConfiguration

    /** * @Auther: lantao * @Date: 2019-07-22 10:10 * @Company: 随行付支付有限公司 * @maill: lan_tao@suixingpay.com * @Description: TODO */ 
    @EnableHelloWorld
    @ConditionOnSysProperty(value = "lantao")
    @Configuration
    public class SpringFactoriesConfiguration {
        // 这里没有写 bean 的注入,直接引用 Condition 和 eanble 模块注解。
    
        // eanble注解内部使用import将bean注入到ioc中,@import注解中能够直接放入bean,也能够作更灵活的配置使用继承了ImportSeletor接口的bean,
        // 能够根据enable注解的属性(attrbutes)进行灵活的动态判断
    
        // Condition 注解做为条件,若是符合条件则将bean注入到IOC中,反之则不注入,实际是使用 @Conditional注解来实现,经过继承 Condition 接口,
        // 经过 matches 方法进行逻辑判断是否符合条件。
    }
    复制代码

    2: 建立 META-INF/spring.factories

    # Auto Configure
    org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
    com.lantao.springboot_leran.spring_factories_leran.config.SpringFactoriesConfiguration
    复制代码

    3: 建立 SpringFactoriesBootStrap 引导类

    /** * @Auther: lantao * @Date: 2019-07-19 16:01 * @Company: 随行付支付有限公司 * @maill: lan_tao@suixingpay.com * @Description: TODO */
    @EnableAutoConfiguration
    public class SpringFactoriesBootStrap {
    
        public static void main(String[] args) {
            ConfigurableApplicationContext context = new SpringApplicationBuilder(SpringFactoriesBootStrap.class).run(args);
            String helloWorld = context.getBean("helloWorld", String.class);
            System.out.println(helloWorld);
            context.close();
        }
    }
    
    结果:
    hello World! Enable
    复制代码

相关文章
相关标签/搜索