java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > @Conditional注解用法

Spring中@Conditional注解用法详解

作者:IT技术分享社区

这篇文章主要介绍了Spring中@Conditional注解用法详解,@Conditional是Spring4版本新提供的一种注解,它的作用是按照设定的条件进行判断,把满足判断条件的bean注册到Spring容器,需要的朋友可以参考下

1、@Conditional注解介绍

@Conditional是Spring4版本新提供的一种注解

它的作用是按照设定的条件进行判断,把满足判断条件的bean注册到Spring容器。

2、@Conditional的源码

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Conditional {
    /**
     * All {@link Condition Conditions} that must {@linkplain Condition#matches match}
     * in order for the component to be registered.
     */
    Class<? extends Condition>[] value();
}

说明:

3、用法示例

3.1 新建Book.java 文件

package com.spring.bean;
 
 
public class Book {
    private String bookName;
    private String bookType;
    private double price;
 
 
    public Book(String bookName, String bookType, double price) {
        this.bookName = bookName;
        this.bookType = bookType;
        this.price = price;
    }
 
 
    public String getBookName() {
        return bookName;
    }
 
 
    public void setBookName(String bookName) {
        this.bookName = bookName;
    }
 
 
    public String getBookType() {
        return bookType;
    }
 
 
    public void setBookType(String bookType) {
        this.bookType = bookType;
    }
 
 
    public double getPrice() {
        return price;
    }
 
 
    public void setPrice(double price) {
        this.price = price;
    }
 
 
    @Override
    public String toString() {
        return "Book{" +
                "bookName='" + bookName + '\'' +
                ", bookType='" + bookType + '\'' +
                ", price=" + price +
                '}';
    }
}

3.2 CNCondition.java 文件

package com.spring.condition;
 
 
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
 
 
public class CNCondition implements Condition {
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        // user.language=CN 表示中文返回 true
        return conditionContext.getEnvironment().getProperty("user.language").contains("cn");
    }
}

3.3 ZHCondition.java 文件

package com.spring.condition;
 
 
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
 
 
public class ZHCondition implements Condition {
    public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {
        // user.language=ZH Java默认语言 表示中文返回 true
        return conditionContext.getEnvironment()
        .getProperty("user.language").contains("zh");
    }
}

3.4 新建TestConditionalConfig.java 配置文件

package com.spring.config;
 
 
import com.spring.bean.Book;
import com.spring.condition.CNCondition;
import com.spring.condition.ZHCondition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
 
 
@Configuration
@Conditional(ZHCondition.class)
 
 
public class TestConditionalConfig {
    @Bean
    @Conditional(ZHCondition.class)//中文
    public Book bookZH()    {
        return new Book("红楼梦","中国古典书籍",99.9);
    }
 
 
    @Bean
    @Conditional(CNCondition.class)//英文
    public Book bookCN()    {
        return new Book("哈姆雷特","外国经典名著",66.6);
    }
}

3.5 新建TestConditional.java 测试文件

package com.spring.test;
 
 
import com.spring.bean.Book;
import com.spring.config.TestConditionalConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
 
import java.util.Map;
 
 
public class TestConditional {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext annotationContext = new AnnotationConfigApplicationContext(TestConditionalConfig.class);
        Map<String, Book> map = annotationContext.getBeansOfType(Book.class);
        System.out.println(map);
    }
}

4、@Conditional应用场景

在一些需要条件满足才是实例化的类中,使用此注解

比如项目中需要根据不同的场景使用不同的消息中间件,在消息队列的实例化bean上,使用@Conditional注解,根据配置文件的不同,来判断哪个bean需要加载到Spring容器中。

5、@Conditional的扩展注解

到此这篇关于Spring中@Conditional注解用法详解的文章就介绍到这了,更多相关@Conditional注解用法内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文