一、条件注解介绍
1、介绍
常见的@ConditionalOnxxx
开头的注解我们称之为条件注解,常见的条件注解有
- class条件注解:@ConditionalOnClass
- bean条件注解:@ConditionalOnBean
- 属性条件注解:@ConditionalOnProperty
- …
2、常见的条件注解
@ConditionalOnProperty:如果有指定的配置,条件生效;是最为常见的一种,尤其用在自动配置类中。
@ConditionalOnBean:如果有指定的Bean,条件生效;
@ConditionalOnMissingBean:如果没有指定的Bean,条件生效;
@ConditionalOnMissingClass:如果没有指定的Class,条件生效;
@ConditionalOnWebApplication:在Web环境中条件生效;
@ConditionalOnExpression:根据表达式判断条件是否生效。
二、@ConditionalOnProperty
1、源码
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperty {
//name的别名,和value不可同时使用
String[] value() default {};
/**配置项的前缀,例如完整的配置是config.person.enable=true
* 那 prefix=“config.person”
*/
String prefix() default "";
/**
配置项的属性,例如完整的配置是config.person.enable=true
在前面已经设置 prefix=“config.person”
那么 name=“enable”;
如果prefix没有设置,那么name可以是整个配置项,例如下面:
name=“config.person.enable”,效果和上面的一样
*/
String[] name() default {};
/**
* 会将配置文件中的值和havingValue的值对比,如果一样则加载Bean,例如:
*
* config.person.enable=true,havingValue=“true”,加载Bean
*
* config.person.enable=false,havingValue=“false”,加载Bean
*
* config.person.enable=ok,havingValue=“ok”,加载Bean
*
* config.person.enable=false,havingValue=“true”,不加载Bean
*
* config.person.enable=ok,havingValue=“no”,不加载Bean
*
* 当然havingValue也可以不设置,只要配置项的值不是false或“false”,都加载Bean,例如:
*
* config.person.enable=ok,不设置havingValue,加载Bean
*
* config.person.enable=false,不设置havingValue,不加载Bean
*/
String havingValue() default "";
/**
* 如果在配置文件中没有该配置项,是否加载Bean
*/
boolean matchIfMissing() default false;
}
2、语法
简单来讲,一般是在配置类上或者是@Bean修饰的方法上,添加此注解表示一个类是否要被Spring上下文加载,若满足条件则加载,若不满足条件则不加载。有以下几个属性:
prefix:配置文件中的前缀。
name:配置的名字。
havingValue:它的值与配置文件的值对比,当两个值相同,类会被加载到spring的IOC容器中。
matchIfMissing:缺少该配置时是否可以加载,如果为true,没有该配置属性时也会正常加载,反之则不会生效。
(1) matchIfMissing:
用来指定如果配置文件中未进行对应属性配置时的默认处理:默认情况下matchIfMissing为false,也就是说如果未进行属性配置,则自动配置不生效。如果matchIfMissing为true,则表示如果没有对应的属性配置,则自动配置默认生效。
(2)name
3、作用
@ConditionalOnProperty注解是控制被注解的类中其他注解(指@Component和@Configuration这两个注解)是否生效。
4、使用场景
4.1、在配置类中控制@Configuration是否生效
在spring boot中有时候需要控制配置类中的Bean是否生效,可以使用@ConditionalOnProperty注解来控制@Configuration是否生效.
@Configuration被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。
filter.loginFilter=true
@Configuration
@ConditionalOnProperty(prefix = "filter",name = "loginFilter",havingValue = "true")
public class FilterConfig {
@Bean
public FilterRegistrationBean getFilterRegistration() {
FilterRegistrationBean filterRegistration = new FilterRegistrationBean(new LoginFilter());
filterRegistration.addUrlPatterns("/*");
return filterRegistration;
}
}
4.2、在切面类中控制@Component是否生效
@Slf4j
@Aspect
@Component
@ConditionalOnProperty(prefix = "system.log", name = "enabled", havingValue = "true")
public class LogAspect {
//成功的标志
private final static boolean SUCCESS = true;
@Resource
private HttpServletRequest request;
@Autowired
private JWTService jwtService;
@Autowired
private SystemLogService systemLogService;
/**
* 配置切点,拦截Controller中添加@Log注解的方法
*/
@Pointcut("@annotation(com.ljxx.common.annotation.Log)")
public void logPointCut() {
}
/**
* 前置通知
*
* @param joinPoint
*/
@Before("logPointCut()")
public void doBeforeAdvice(JoinPoint joinPoint) {
}
/**
* 后置通知
*
* @param obj
* @throws IOException
*/
@AfterReturning(returning = "obj", pointcut = "logPointCut()")
public void doAfterReturning(JoinPoint joinPoint, Object obj) throws Exception {
handleLog(joinPoint, obj, null);
}
/**
* 异常通知
*
* @param e
*/
@AfterThrowing(value = "logPointCut()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Exception e) throws Exception {
handleLog(joinPoint, null, e);
}
/**
* 日志处理
*
* @param result
* @param e
*/
private void handleLog(JoinPoint joinPoint, Object result, Exception e) throws Exception {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
...
systemLogService.addLog(systemLog);
}
/**
* 截取字符串
*
* @param val
* @return
*/
private String getText(String val) {
return StrUtil.sub(val, 0, 4000);
}
}
配置中的代码:
#开启日志存储
system.log.enabled=true
4.3、在配置类中控制@Component是否生效
(1)配置文件配置
zs.name=张三
zs.age=14
config.isActive=1
(2)配置类
@Component
@ConfigurationProperties(prefix = "zs")
@ConditionalOnProperty(prefix = "config",name = "isActive",havingValue = "1")
@Data
public class ConfigurationBean {
private String name;
private Integer age;
}
@ConditionOnPropertiy注解的意思是:如果配置文件中config.isActive=1,则该@Component注解生效。通过注解@ConfigurationProperties(prefix="配置文件中的key的前缀")可以将配置文件中的配置自动与实体进行映射
三、demo
1、
import lombok.Builder;
import lombok.Data;
@Data
@Builder
public class MenuDTO {
private String name;
private String url;
}
import lombok.extern.slf4j.Slf4j;
import org.demo.annotation.dto.MenuDTO;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Slf4j
public class MyBeanConfig {
@Bean
public MenuDTO menuDTO(){
log.info("===MenuDTO init start===");
return MenuDTO.builder().name("初始化name").url("初始化url").build();
}
}
启动时会自动注入Bean:
2、现在加入开关
import lombok.extern.slf4j.Slf4j;
import org.demo.annotation.dto.MenuDTO;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@Slf4j
public class MyBeanConfig {
@Bean
@ConditionalOnProperty(name = "config.switch",matchIfMissing = true)
public MenuDTO menuDTO(){
log.info("===MenuDTO init start===");
return MenuDTO.builder().name("初始化name").url("初始化url").build();
}
}
(1)设置为true
config.switch = true
启动项目后台打印同上。
(2)不设置
server.port=3333
server.servlet.context-path=/annotationDemo
#config.switch = false
启动项目后台打印同上,因为设置了matchIfMissing = true
(3)config.switch 改为false,则启动项目不打印,说明bean未执行初始化