码迷,mamicode.com
首页 > 编程语言 > 详细

spring boot: 条件注解@Condition

时间:2018-01-11 20:29:30      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:contains   conf   ice   查看   cmd   gen   不同   运行   lis   

@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

 

spring boot: 条件注解@Condition

标签:contains   conf   ice   查看   cmd   gen   不同   运行   lis   

原文地址:https://www.cnblogs.com/achengmu/p/8269985.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!