spring boot: 条件注解@Condition

@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-07-31 23:20:09

spring boot: 条件注解@Condition的相关文章

spring boot 条件注解表达式

@Configuration @ConditionalOnClass(JAXRSServerFactoryBean.class) @ConditionalOnExpression("'${cxf.jaxrs.component-scan}'=='true' && '${cxf.jaxrs.classes-scan}'!='true'") @Import(SpringComponentScanServer.class) protected static class Jax

峰哥说技术: 05-Spring Boot条件注解注解

Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 05 峰哥说技术  Spring Boot条件注解 @EnableAutoConfiguration开启自动化配置,零配置就能运行,那么它的原理是什么?要理解这个,必须首先理解什么是条件注解.抛开 Spring Boot,我们来单纯的看看在 Spring 中条件注解的用法.首先我们来创建一个普通的 Maven 项目,然后引入 spring-context,如下 1)创建一个接口Food package

spring boot 之注解

@EnableAutoConfiguration有这个注解,spring boot 就会根据classpat中的jar依赖,来猜测你将要开发一个什么样的spring应用.比如:spring-boot-starter-web这个依赖,则springboot就会自动嵌入tomcat和springMVC的相关依赖,并在启动的时候,启动为一个web应用.并会查找相应的配置参数,如果没有,则使用默认值.如:server.port=8080: @Component它是一个类级注解.当应用是通过注解来配置或x

Spring Boot Annotations 注解

1.概述 Spring Boot通过其自动配置功能使Spring更容易配置. 在本教程中,我们将探讨org.springframework.boot.autoconfigure和org.springframework.boot.autoconfigure.condition包中的注释. 2. @SpringBootApplication 我们使用此批注来标记Spring Boot应用程序的主类: @SpringBootApplication封装@Configuration,@EnableAuto

spring boot 的注解

(1)@SpringBootApplication 申明让spring boot自动给程序进行必要的配置,这个配置等同于: @Configuration ,@EnableAutoConfiguration 和 @ComponentScan 三个配置. 示例代码: package com.kfit; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.Spr

spring boot常用注解使用小结

1.@RestController和@RequestMapping注解 4.0重要的一个新的改进是@RestController注解,它继承自@Controller注解. 4.0之前的版本,Spring MVC的组件都使用@Controller来标识当前类是一个控制器servlet.使用这个特性,我们可以开发REST服务的时候不需要使用@Controller,而专门的@RestController. 当你实现一个RESTful web services的时候,response将一直通过respo

Spring Boot MyBatis注解:@MapperScan和@Mapper

最近参与公司的新项目架构搭建,在使用mybatis的注解时,和同时有了不同意见,同事认为使用@Mapper注解简单明了,而我建议使用@MapperScan,直接将mapper所在的目录扫描进去就行,而且@Mapper需要在每一个mapper上都添加,繁琐.同事又说--我们可以用逆向工程自动生产entity,mapper,service时,将注解加上,很方便的,于是各执一词. 下面是我整理的这两种方法的比较: 使用@Mapper注解 为了让DemoMapper能够让别的类进行引用,我们可以在Dem

Spring Boot缓存注解@Cacheable、@CacheEvict、@CachePut使用

从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我们在调用一个缓存方法时会把该方法参数和返回结果作为一个键值对存放在缓存中,等到下次利用同样的参数来调用该方法时将不再执行该方法,而是直接从缓存中获取结果进行返回.所以在使用Spring Cache的时候我们要保证我们缓存的方法对于相同的方法参数要有相同的返回结果. 使用Spring Cache需要我们做两方面的事:      

Spring Boot AutoConfiguration注解@ConditionalXXXX之前生今世

1.注解@Conditional的定义 @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) public @interface Conditional { /** * All {@link Condition}s that must {@linkplain Condition#matches match} * in order for the component to be reg