@ConditionalOnProperty注解的作用和用法

1.遇到的问题

自定义参数有的模块不需要用到自定义的线程池,怎么屏蔽呢?这时想到了@ConditionalOnProperty这个注解。

@ConditionalOnProperty注解;

@ConditionalOnProperty注解可以通过配置文件中的属性值,来判定configuration是否被注入;

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.boot.autoconfigure.condition;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Conditional;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE, ElementType.METHOD})
@Documented
@Conditional({OnPropertyCondition.class})
public @interface ConditionalOnProperty {
    String[] value() default {};

    String prefix() default "";

    String[] name() default {};

    String havingValue() default "";

    boolean matchIfMissing() default false;
}

2.使用方法 

@ConditionalOnProperty(prefix = "async.executor.thread", name = "enabled", havingValue = "true")
public class AsyncConfig implements AsyncConfigurer {

}

你可能感兴趣的:(java,开发语言)