Spring Boot实战笔记(八)-- Spring高级话题(条件注解@Conditional)

时间:2021-12-16 11:50:41

一、条件注解@Conditional

  在之前的学习中,通过活动的profile,我们可以获得不同的Bean。Spring4提供了一个更通用的基于条件的Bean的创建,即使用@Conditional注解。

  @Conditional根据满足某一个特定条件创建一个特定的Bean。比方说,当某一个jar包在一个类路径下的时候,自动配置一个或多个Bean;或者只有某个Bean被创建才会创建另外一个Bean。总的来说,就是根据特定条件来控制Bean的创建行为,这样我们可以利用这个特性进行一些自动的配置。

  下面示例将以不同的操作系统为条件,我们通过实现Condition接口,并重写其matches方法来构造判断条件。若在Windows系统下运行程序,则输出列表命令为dir;若在Linux系统下运行程序,则输出列表命令为ls。

示例:

  1.判断条件定义

  (1)判定Windows的条件

package com.ecworking.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata; public class WindowsCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
return conditionContext.getEnvironment().getProperty("os.name").contains("Windows");
}
}

  (2)判定Linux的条件 

package com.ecworking.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata; public class LinuxCondition implements Condition {
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
return conditionContext.getEnvironment().getProperty("os.name").contains("Linux");
}
}

  (3)判定Mac的条件

package com.ecworking.condition;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata; public class MacCondition implements Condition{
@Override
public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
return conditionContext.getEnvironment().getProperty("os.name").contains("Mac");
}
}

  2.不同系统下的Bean类

  (1)接口

package com.ecworking.condition;

public interface ListService {
String showListCmd();
}

  (2)Windows下所要创建的Bean类

package com.ecworking.condition;

public class WindowsListService implements ListService{
@Override
public String showListCmd() {
return "dir";
}
}

  (3)Linux下所要创建的Bean类

package com.ecworking.condition;

public class LinuxListService implements ListService{
@Override
public String showListCmd() {
return "ls";
}
}

  (4)Mac下所要创建的Bean类

package com.ecworking.condition;

public class MacListService implements ListService{
@Override
public String showListCmd() {
return "ls";
}
}

  3.配置类

package com.ecworking.condition;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration; @Configuration
public class ConditionConfig { @Bean
@Conditional(WindowsCondition.class) // 通过@Conditional注解,符合Windows条件则实例化WindowsListService
public ListService windowsListService(){
return new WindowsListService();
} @Bean
@Conditional(LinuxCondition.class) // 通过@Conditional注解,符合Linux条件则实例化LinuxListService
public ListService linuxListService(){
return new LinuxListService();
} @Bean
@Conditional(MacCondition.class) // 通过@Conditional注解,符合Mac条件则实例化MacListService
public ListService macListService(){
return new MacListService();
}
}

  4.运行

package com.ecworking.condition;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {
public static void main(String[] args){
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConditionConfig.class); ListService listService = context.getBean(ListService.class); System.out.println(context.getEnvironment().getProperty("os.name") + "系统下的列表命令为:" + listService.showListCmd());
}
}

运行结果:

Spring Boot实战笔记(八)-- Spring高级话题(条件注解@Conditional)

  在首次运行的时候只使用了书中Windows和Linux的实例,结果运行 ListService listService = context.getBean(ListService.class); 时报错,因为没有符合条件的实例创建,原因是我使用的是Mac,以前一直以为Mac就是Linux系统,结果发现Mac的操作系统是Mac OS,所以加上MacCondition和MacListService后正常运行。