欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot自动装配Condition的实现方式

程序员文章站 2022-06-25 10:04:34
目录2.1 @conditional2.2 condition4.2 修改usercondition5. spring内置条件注解1. 简介@conditional注解在spring4.0中引入,其主...

1. 简介

@conditional注解在spring4.0中引入,其主要作用就是判断条件是否满足,从而决定是否初始化并向容器注册bean。

2. 定义

2.1 @conditional

@conditional注解定义如下:其内部只有一个参数为class对象数组,且必须继承自condition接口,通过重写condition接口的matches方法来判断是否需要加载bean

@target({elementtype.type, elementtype.method})
@retention(retentionpolicy.runtime)
@documented
public @interface conditional {
  class<? extends condition>[] value();
}

2.2 condition

condition接口定义如下:该接口为一个函数式接口,只有一个matches接口,形参为conditioncontext context, annotatedtypemetadata metadataconditioncontext定义如2.2.1,annotatedtypemetadata见名知意,就是用来获取注解的元信息的

@functionalinterface
public interface condition {
  boolean matches(conditioncontext context, annotatedtypemetadata metadata);
}

2.2.1 conditioncontext

conditioncontext接口定义如下:通过查看源码可以知道,从这个类中可以获取很多有用的信息

public interface conditioncontext {
  /**
   * 返回bean定义信息
   * return the {@link beandefinitionregistry} that will hold the bean definition
   * should the condition match.
   * @throws illegalstateexception if no registry is available (which is unusual:
   * only the case with a plain {@link classpathscanningcandidatecomponentprovider})
   */
  beandefinitionregistry getregistry();

  /**
   * 返回bean工厂
   * return the {@link configurablelistablebeanfactory} that will hold the bean
   * definition should the condition match, or {@code null} if the bean factory is
   * not available (or not downcastable to {@code configurablelistablebeanfactory}).
   */
  @nullable
  configurablelistablebeanfactory getbeanfactory();

  /**
   * 返回环境变量 比如在application.yaml中定义的信息
   * return the {@link environment} for which the current application is running.
   */
  environment getenvironment();

  /**
   * 返回资源加载器
   * return the {@link resourceloader} currently being used.
   */
  resourceloader getresourceloader();

  /**
   * 返回类加载器
   * return the {@link classloader} that should be used to load additional classes
   * (only {@code null} if even the system classloader isn't accessible).
   * @see org.springframework.util.classutils#forname(string, classloader)
   */
  @nullable
  classloader getclassloader();
}

3. 使用说明

通过一个简单的小例子测试一下@conditional是不是真的能实现bean的条件化注入。

3.1 创建项目

SpringBoot自动装配Condition的实现方式

首先我们创建一个springboot项目

3.1.1 导入依赖

这里我们除了springboot依赖,再添加个lombok依赖

<?xml version="1.0" encoding="utf-8"?>
<project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"
         xsi:schemalocation="http://maven.apache.org/pom/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelversion>4.0.0</modelversion>
    <parent>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-starter-parent</artifactid>
        <version>2.5.3</version>
        <relativepath/> <!-- lookup parent from repository -->
    </parent>
    <groupid>com.ldx</groupid>
    <artifactid>condition</artifactid>
    <version>0.0.1-snapshot</version>
    <name>condition</name>
    <description>demo project for spring boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupid>org.springframework.boot</groupid>
            <artifactid>spring-boot-starter</artifactid>
        </dependency>
        <dependency>
            <groupid>org.projectlombok</groupid>
            <artifactid>lombok</artifactid>
            <version>1.18.12</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupid>org.springframework.boot</groupid>
                <artifactid>spring-boot-maven-plugin</artifactid>
            </plugin>
        </plugins>
    </build>
</project>

3.1.2 添加配置信息

在application.yaml 中加入配置信息

user:
  enable: false

3.1.3 创建user类

package com.ldx.condition;

import lombok.allargsconstructor;
import lombok.data;

/**
 * 用户信息
 * @author ludangxin
 * @date 2021/8/1
 */
@data
@allargsconstructor
public class user {
   private string name;
   private integer age;
}

3.1.4 创建条件实现类

package com.ldx.condition;

import org.springframework.context.annotation.condition;
import org.springframework.context.annotation.conditioncontext;
import org.springframework.core.env.environment;
import org.springframework.core.type.annotatedtypemetadata;

/**
 * 用户bean条件判断
 * @author ludangxin
 * @date 2021/8/1
 */
public class usercondition implements condition {
   @override
   public boolean matches(conditioncontext conditioncontext, annotatedtypemetadata annotatedtypemetadata) {
      environment environment = conditioncontext.getenvironment();
      // 获取property user.enable
      string property = environment.getproperty("user.enable");
      // 如果user.enable的值等于true 那么返回值为true,反之为false
      return "true".equals(property);
   }
}

3.1.5 修改启动类

package com.ldx.condition;

import lombok.extern.slf4j.slf4j;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.context.configurableapplicationcontext;
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.conditional;

@slf4j
@springbootapplication
public class conditionapplication {

   public static void main(string[] args) {
      configurableapplicationcontext applicationcontext = springapplication.run(conditionapplication.class, args);
      // 获取类型为user类的bean
      user user = applicationcontext.getbean(user.class);
      log.info("user bean === {}", user);
   }

  /**
   * 注入user类型的bean
   */
   @bean
   @conditional(usercondition.class)
   public user getuser(){
      return new user("张三",18);
   }

}

3.2 测试

3.2.1 当user.enable=false

报错找不到可用的user类型的bean

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: spring boot ::                (v2.5.3)

2021-08-01 17:07:51.994  info 47036 --- [           main] com.ldx.condition.conditionapplication   : starting conditionapplication using java 1.8.0_261 on ludangxindemacbook-pro.local with pid 47036 (/users/ludangxin/workspace/idea/condition/target/classes started by ludangxin in /users/ludangxin/workspace/idea/condition)
2021-08-01 17:07:51.997  info 47036 --- [           main] com.ldx.condition.conditionapplication   : no active profile set, falling back to default profiles: default
2021-08-01 17:07:52.461  info 47036 --- [           main] com.ldx.condition.conditionapplication   : started conditionapplication in 0.791 seconds (jvm running for 1.371)
exception in thread "main" org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type 'com.ldx.condition.user' available
	at org.springframework.beans.factory.support.defaultlistablebeanfactory.getbean(defaultlistablebeanfactory.java:351)
	at org.springframework.beans.factory.support.defaultlistablebeanfactory.getbean(defaultlistablebeanfactory.java:342)
	at org.springframework.context.support.abstractapplicationcontext.getbean(abstractapplicationcontext.java:1172)
	at com.ldx.condition.conditionapplication.main(conditionapplication.java:16)

process finished with exit code 1

3.2.2 当user.enable=true

正常输出userbean实例信息

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: spring boot ::                (v2.5.3)

2021-08-01 17:13:38.022  info 47129 --- [           main] com.ldx.condition.conditionapplication   : starting conditionapplication using java 1.8.0_261 on ludangxindemacbook-pro.local with pid 47129 (/users/ludangxin/workspace/idea/condition/target/classes started by ludangxin in /users/ludangxin/workspace/idea/condition)
2021-08-01 17:13:38.024  info 47129 --- [           main] com.ldx.condition.conditionapplication   : no active profile set, falling back to default profiles: default
2021-08-01 17:13:38.434  info 47129 --- [           main] com.ldx.condition.conditionapplication   : started conditionapplication in 0.711 seconds (jvm running for 1.166)
2021-08-01 17:13:38.438  info 47129 --- [           main] com.ldx.condition.conditionapplication   : user bean === user(name=张三, age=18)

3.3 小结

上面的例子通过使用@conditionalcondition接口,实现了spring bean的条件化注入。

好处:

  • 可以实现某些配置的开关功能,如上面的例子,我们可以将userbean换成开启缓存的配置,当property的值为true时,我们才开启缓存的配置。
  • 当有多个同名的bean时,如何抉择的问题。
  • 实现自动化的装载。如判断当前classpath中有mysql的驱动类时(说明我们当前的系统需要使用mysql),我们就自动的读取application.yaml中的mysql配置,实现自动装载;当没有驱动时,就不加载。

4. 改进

从上面的使用说明中我们了解到了条件注解的大概使用方法,但是代码中还是有很多硬编码的问题。比如:usercondition中的property的key包括value都是硬编码,其实我们可以通过再扩展一个注解来实现动态的判断和绑定。

4.1 创建注解

import org.springframework.context.annotation.conditional;
import java.lang.annotation.*;

/**
 * 自定义条件属性注解
 * <p>
 *   当配置的property name对应的值 与设置的 value值相等时,则注入bean
 * @author ludangxin
 * @date 2021/8/1
 */
@target({elementtype.type, elementtype.method})
@retention(retentionpolicy.runtime)
@documented
// 指定condition的实现类
@conditional({usercondition.class})
public @interface myconditiononproperty {
   // 配置信息的key
   string name();
   // 配置信息key对应的值
   string value();
}

4.2 修改usercondition

package com.ldx.condition;

import org.springframework.context.annotation.condition;
import org.springframework.context.annotation.conditioncontext;
import org.springframework.core.env.environment;
import org.springframework.core.type.annotatedtypemetadata;

import java.util.map;

/**
 * 用户bean条件判断
 * @author ludangxin
 * @date 2021/8/1
 */
public class usercondition implements condition {
   @override
   public boolean matches(conditioncontext conditioncontext, annotatedtypemetadata annotatedtypemetadata) {
      environment environment = conditioncontext.getenvironment();
      // 获取自定义的注解
      map<string, object> annotationattributes = annotatedtypemetadata.getannotationattributes("com.ldx.condition.myconditiononproperty");
      // 获取在注解中指定的name的property的值 如:user.enable的值
      string property = environment.getproperty(annotationattributes.get("name").tostring());
      // 获取预期的值
      string value = annotationattributes.get("value").tostring();
      return value.equals(property);
   }
}

测试后,结果符合预期。

其实在spring中已经内置了许多常用的条件注解,其中我们刚实现的就在内置的注解中已经实现了,如下。

5. spring内置条件注解

注解 说明
@conditionalonsinglecandidate 当给定类型的bean存在并且指定为primary的给定类型存在时,返回true
@conditionalonmissingbean 当给定的类型、类名、注解、昵称在beanfactory中不存在时返回true.各类型间是or的关系
@conditionalonbean 与上面相反,要求bean存在
@conditionalonmissingclass 当给定的类名在类路径上不存在时返回true,各类型间是and的关系
@conditionalonclass 与上面相反,要求类存在
@conditionaloncloudplatform 当所配置的cloudplatform为激活时返回true
@conditionalonexpression spel表达式执行为true
@conditionalonjava 运行时的java版本号是否包含给定的版本号.如果包含,返回匹配,否则,返回不匹配
@conditionalonproperty 要求配置属性匹配条件
@conditionalonjndi 给定的jndi的location 必须存在一个.否则,返回不匹配
@conditionalonnotwebapplication web环境不存在时
@conditionalonwebapplication web环境存在时
@conditionalonresource 要求制定的资源存在

到此这篇关于springboot自动装配condition的实现方式的文章就介绍到这了,更多相关springboot自动装配内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!