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();

    }
}

输出结果

ls

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

 

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


17点睛Spring4.1-@Conditional_第1张图片

你可能感兴趣的:(17点睛Spring4.1-@Conditional)