• 17点睛Spring4.1-@Conditional


    17.1 @Conditional

    • @Conditional为按照条件配置spring的bean提供了支持,即满足某种条件下,怎么配置对应的bean;
    • 应用场景
      • 当某一个jar包在classpath中的时候,配置某几个bean;
      • 当某一个bean配置好后,会自动配置一个特定的bean;
      • 当某种环境变量被设置后,创建某个bean;
    • @Conditional为敏捷开发所提倡的原则"习惯优于配置"提供了支持;
    • @Conditional是Spring Boot快速开发框架实现"习惯优于配置"的核心技术;

    17.2 示例

    演示在windows和linux系统下,初始化不同的bean,windows和linux作为判断条件;

    17.2.1 构造判断条件

    条件的构造需要类实现Condition接口,并实现matches方法

    • WindowsCondition
    package com.wisely.conditional;
    
    import org.springframework.context.annotation.Condition;
    import org.springframework.context.annotation.ConditionContext;
    import org.springframework.core.type.AnnotatedTypeMetadata;
    
    public class WindowsCondition implements Condition {
    
        public boolean matches(ConditionContext context,
                AnnotatedTypeMetadata metadata) {
            return context.getEnvironment().getProperty("os.name").contains("Windows");
        }
    
    }
    
    • LinuxCondition
    package com.wisely.conditional;
    
    import org.springframework.context.annotation.Condition;
    import org.springframework.context.annotation.ConditionContext;
    import org.springframework.core.type.AnnotatedTypeMetadata;
    
    public class LinuxCondition implements Condition {
    
        public boolean matches(ConditionContext context,
                AnnotatedTypeMetadata metadata) {
            return context.getEnvironment().getProperty("os.name").contains("Linux");
        }
    
    }
    

    17.2.2 编写不同条件下需要的bean

    • 接口
    package com.wisely.conditional;
    
    
    public interface CommandService {
    
        public String showListCommand();
    }
    
    • WindowsCommnadService
    package com.wisely.conditional;
    
    public class WindowsCommnadService implements CommandService {
    
        public String showListCommand() {
            return "dir";
        }
    
    }
    
    • LinuxCommandService
    package com.wisely.conditional;
    
    public class LinuxCommandService implements CommandService {
    
        public String showListCommand() {
            return "ls";
        }
    
    }
    

    17.2.3 编写配置类

    package com.wisely.conditional;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Conditional;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class DemoConfig {
        @Bean
        @Conditional(WindowsCondition.class)
        public CommandService commandService() {
            return new WindowsCommnadService();
        }
    
        @Bean
        @Conditional(LinuxCondition.class)
        public CommandService linuxEmailerService() {
            return new LinuxCommandService();
        }
    
    }
    

    17.2.4 测试

    • windows下
    package com.wisely.conditional;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class Main {
    
        public static void main(String[] args) {
            AnnotationConfigApplicationContext context =
                    new AnnotationConfigApplicationContext("com.wisely.conditional");
            CommandService cs = context.getBean(CommandService.class);
            System.out.println(cs.showListCommand());
            context.close();
        }
    }
    

    输出结果

    dir
    • Linux下(本例没有切换到linux,直接修改os.name为Linux)
    package com.wisely.conditional;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    public class Main {
    
        public static void main(String[] args) {
            System.setProperty("os.name", "Linux");
            AnnotationConfigApplicationContext context =  new AnnotationConfigApplicationContext("com.wisely.conditional");
            CommandService cs = context.getBean(CommandService.class);
            System.out.println(cs.showListCommand());
            context.close();
    
        }
    }
    

    输出结果

  • 相关阅读:
    面试题:Spring的理解
    静态块与静态变量
    构造方法与构造块的执行顺序(区别于static)
    子类调用父类的构造函数几种情况
    使用json遇到的问题
    页面加载,使用ajax查询某个类别,并且给它们添加(拼接)连接
    对某个商品的上下架状态进行改变
    SSH后台管理系统,实现查询+分页
    跟上Java8
    java中值传递和引用传递
  • 原文地址:https://www.cnblogs.com/Jeely/p/11949999.html
Copyright © 2020-2023  润新知