第十一章 Spring条件注解@Conditional

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/gongm24/article/details/56671905

第六章 Profile中有介绍,通过@Profile可以根据profile的不同,创建不同的Bean。Spring4提供了一个更通用的基于条件的Bean的创建,即@Conditional注解

下面的示例将以不同的操作系统为条件,实现在Windows系统下运行程序,输出dir,若在Linux系统下运行程序,则输出ls>

一、通过实现Condition接口,定义判断条件

package demo2.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 context, AnnotatedTypeMetadata arg1) {
        return context.getEnvironment().getProperty("os.name").contains("Windows");
    }

}
package demo2.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 context, AnnotatedTypeMetadata arg1) {
        return context.getEnvironment().getProperty("os.name").contains("Linux");
    }

}

二、定义Service接口

package demo2.service;

public interface ListService {

    public String showListCmd();

}

三、定义Service的不同实现

package demo2.service;

public class WindowsListService implements ListService {

    @Override
    public String showListCmd() {
        return "dir";
    }

}
package demo2.service;

public class LinuxListService implements ListService {

    @Override
    public String showListCmd() {
        return "ls";
    }

}

四、配置类
通过@Conditional注解实现条件判断

package demo2.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import demo2.condition.LinuxCondition;
import demo2.condition.WindowsCondition;
import demo2.service.ListService;
import demo2.service.WindowsListService;

@Configuration
@ComponentScan("demo2")
@EnableAspectJAutoProxy
@EnableAsync
@EnableScheduling
public class DIConfig {

    @Bean
    @Conditional(WindowsCondition.class)
    public ListService windowsListService() {
        return new WindowsListService();
    }

    @Bean
    @Conditional(LinuxCondition.class)
    public ListService linuxListService() {
        return new WindowsListService();
    }

}

五、运行

package demo2;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import demo2.config.DIConfig;
import demo2.service.ListService;

public class Main {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext cxt = new AnnotationConfigApplicationContext();

        cxt.getEnvironment().setActiveProfiles("dev");
        cxt.register(DIConfig.class);
        cxt.refresh();

        ListService ds = cxt.getBean(ListService.class);
        System.out.println(ds.showListCmd());
        cxt.close();
    }

}

结果

dir
展开阅读全文

没有更多推荐了,返回首页