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

Spring条件注解@Conditional

时间:2017-09-20 23:29:42      阅读:434      评论:0      收藏:0      [点我收藏+]

标签:实例化   .sh   images   meta   []   nts   size   slist   class   

@Conditional 基于条件的Bean的创建,根据满足某一个特定条件创建特定的Bean,通过实现Condition接口,并重写matches接口来构造判断条件。


下面的实例通过将以不同操作系统作为条件,我们将通过实现Condition接口,并重写其matchess方法来构造判断条件。若在Windows系统下运行程序,则输出列表命令为dir;若在Linux操作系统下运行程序,则输出列表命令为ls
1、判断条件定义
(1)判断Windows的条件
package com.lwh.highlight_spring4.ch3.conditional;

/**
 * Created by luwenhu on 2017/9/20.
 */

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @Conditional 基于条件的Bean的创建,根据满足某一个特定条件创建特定的Bean
 * 通过实现Condition接口,并重写matches接口来构造判断条件
 */
public class WindowsCondition implements Condition{

    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //是否是在Windows系统下运行程序
        return context.getEnvironment().getProperty("os.name").contains("Windows");
    }
}

(2)判定Linux的条件

package com.lwh.highlight_spring4.ch3.conditional;

import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * Created by luwenhu on 2017/9/20.
 */
public class LinuxCondition implements Condition{
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        //判断是否在Linux环境下运行程序
        return context.getEnvironment().getProperty("os.name").contains("Linux");
    }
}

 2、不同系统下的Bean的类

(1)接口

package com.lwh.highlight_spring4.ch3.conditional;

/**
 * Created by luwenhu on 2017/9/20.
 */
//不同系统下Bean的类的接口
public interface ListService {
    public String showListCmd();
}

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

package com.lwh.highlight_spring4.ch3.conditional;

/**
 * Created by luwenhu on 2017/9/20.
 */
//Windows下面所要创建的Bean类
public class WindowsListService implements ListService{
    @Override
    public String showListCmd() {
        return "dir";
    }
}

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

package com.lwh.highlight_spring4.ch3.conditional;

/**
 * Created by luwenhu on 2017/9/20.
 */
//Linux下所要创建的Bean的类
public class LinuxListService implements ListService{
    @Override
    public String showListCmd() {
        return "ls";
    }
}

3、配置类

package com.lwh.highlight_spring4.ch3.conditional;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
 * Created by luwenhu on 2017/9/20.
 */
@Configuration
public class ConditionConfig {

    @Bean
    //使用@Conditional注解,符合Windows条件则实例化windowsListService
    @Conditional(WindowsCondition.class)
    public WindowsListService windowsListService(){
        return new WindowsListService();
    }

    @Bean
    //使用@Conditional注解,符合Linux条件则实例化linuxListService
    @Conditional(LinuxCondition.class)
    public LinuxListService linuxListService(){
        return new LinuxListService();
    }


}

4、运行

package com.lwh.highlight_spring4.ch3.conditional;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * Created by luwenhu on 2017/9/20.
 */
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条件注解@Conditional

标签:实例化   .sh   images   meta   []   nts   size   slist   class   

原文地址:http://www.cnblogs.com/wenhulu/p/7565390.html

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