@Conditional根据满足某一个特定的条件创建一个特定的Bean(基于条件的Bean的创建,即使用@Conditional注解)。
比方说,当一个jar包在一个类的路径下的时候,自动配置一个或多个Bean,或者只有某个Bean被创建才会去创建另外一个Bean。
通过实现Condition接口,并重写期matches方法来构造判断条件。若在windows系统洗运行程序,则输出列表命令dir,若在linux操作系统下运行程序,则输出命令为:ls.
1.判断条件定义
判断windows的条件
1 package ch2.conditional2; 2 3 //条件注解,并复写此类 4 import org.springframework.context.annotation.Condition; 5 //条件注解容器 6 import org.springframework.context.annotation.ConditionContext; 7 import org.springframework.core.type.AnnotatedTypeMetadata; 8 9 public class LinuxCondition implements Condition { 10 11 @Override 12 public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { 13 // TODO Auto-generated method stub 14 return context.getEnvironment().getProperty("os.name").contains("Linux"); 15 } 16 17 }
判定linux的条件
1 package ch2.conditional2; 2 3 //条件注解接口类,复写metches 4 import org.springframework.context.annotation.Condition; 5 import org.springframework.context.annotation.ConditionContext; 6 import org.springframework.core.type.AnnotatedTypeMetadata; 7 8 public class WindowsCondition implements Condition { 9 10 @Override 11 public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) { 12 // TODO Auto-generated method stub 13 return context.getEnvironment().getProperty("os.name").contains("Windows"); 14 } 15 16 }
2不同系统下的Bean类
1接口
1 package ch2.conditional2; 2 3 public interface ListService { 4 5 public String showListCmd(); 6 }
2windows下要创建的Bean类
1 package ch2.conditional2; 2 3 import ch2.conditional2.ListService; 4 5 public class WindowsListService implements ListService { 6 7 @Override 8 public String showListCmd() { 9 // TODO Auto-generated method stub 10 return "dir"; 11 } 12 13 }
3linux下要创建的Bean类
1 package ch2.conditional2; 2 3 import ch2.conditional2.ListService; 4 5 public class LinuxListService implements ListService { 6 7 @Override 8 public String showListCmd() { 9 // TODO Auto-generated method stub 10 return "ls"; 11 } 12 13 }
3配置类
package ch2.conditional2; //配置类声明 import org.springframework.context.annotation.Configuration; //bean声明 import org.springframework.context.annotation.Bean; //条件限制 import org.springframework.context.annotation.Conditional; //声明为配置类 @Configuration public class ConditionConfig { //注解为Bean @Bean //注解条件判断 @Conditional(WindowsCondition.class) public ListService windowsListService() { return new WindowsListService(); } //注解为Bean @Bean //注解条件判断 @Conditional(LinuxCondition.class) public ListService linuxListService() { return new LinuxListService(); } }
4运行
package ch2.conditional2; 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() ); context.close(); } }
运行结果:Windows 10系统的查看目录的命令是: dir
原文地址:https://www.cnblogs.com/achengmu/p/8269985.html
时间: 2024-10-04 13:08:00