广告
返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot自动装配之Condition深入讲解
  • 739
分享到

SpringBoot自动装配之Condition深入讲解

SpringBoot ConditionSpringBoot自动装配Condition 2023-01-16 09:01:32 739人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录案例一案例二案例三总结Condition是在spring 4.0 增加的条件判断功能,通过这个可以功能可以实现选择性的创建 Bean操作。 思考: SpringBoot是如何知道

Condition是在spring 4.0 增加的条件判断功能,通过这个可以功能可以实现选择性的创建 Bean操作。

思考:

SpringBoot是如何知道要创建哪个Bean的?比如SpringBoot是如何知道要创建RedisTemplate的?

看一个例子:

当我们没导入redis-start时,会报错

引出问题

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'redisTemplate' available
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:874)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1358)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:309)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154)
	at com.example.condition.SpringbootDemo01Application.main(SpringbootDemo01Application.java:13)

当导入redis起步依赖后

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
org.springframework.data.redis.core.RedisTemplate@5a6d5a8f

问题:

SpringBoot是怎么知道我们导入redis坐标的呢?

案例一

需求:

在 Spring 的 ioc 容器中有一个 User 的 Bean,现要求:

通过condition设置加载或者不加载。

新建实体类:

package com.example.condition.entity;
public class User {
}

新建配置文件:

package com.example.condition.config;
import com.example.condition.condition.ClassCondition;
import com.example.condition.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
    @Bean
    @Conditional(ClassCondition.class)
    public User user(){
        return new User();
    }
}

新建condition:

如果为true则加载,如果为false则不加载

package com.example.condition.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class ClassCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        return true;
    }
}

测试

package com.example.condition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringbootDemo01Application {
    public static void main(String[] args) {
        //启动SpringBoot应用,返回Spring的IOC容器
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootDemo01Application.class, args);
        Object user = context.getBean("user");
        System.out.println(user);
    }
}

案例二

需求:

在 Spring 的 IOC 容器中有一个 User 的 Bean,现要求:

导入Jedis坐标后,加载该Bean,没导入,则不加载。

新建User实体类:

package com.example.condition.entity;
public class User {
}

新建配置文件:

package com.example.condition.config;
import com.example.condition.condition.ClassCondition;
import com.example.condition.entity.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
@Configuration
public class UserConfig {
    @Bean
    @Conditional(ClassCondition.class)
    public User user(){
        return new User();
    }
}

condition通过反射判断jedis是否已经加载完毕

package com.example.condition.condition;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
public class ClassCondition implements Condition {
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        boolean flag =true;
        try {
            Class<?> cls = Class.forName("redis.clients.jedis.Jedis");
        }catch (ClassNotFoundException e){
            flag =false;
        }
        return flag;
    }
}

测试类:

package com.example.condition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringbootDemo01Application {
    public static void main(String[] args) {
        //启动SpringBoot应用,返回Spring的IOC容器
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootDemo01Application.class, args);
        Object user = context.getBean("user");
        System.out.println(user);
    }
}

引入jedis进行测试判断:

        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>3.6.0</version>
        </dependency>

案例三

需求:

在 Spring 的 IOC 容器中有一个 User 的 Bean,现要求:

  • 导入Jedis坐标后,加载该Bean,没导入,则不加载。
  • 将类的判断定义为动态的。判断哪个字节码文件存在可以动态指定。

实体类:

package com.example.condition.entity;
public class User {
}

自定义注解:

package com.example.condition.condition;import org.springframework.context.annotation.Conditional;import java.lang.annotation.*;@Target({ElementType.TYPE, ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)@Documented@Conditional(ClassCondition.class)public @interface ConditionOnClass {    String[] value();}

Condition类:

package com.example.condition.condition;
import org.springframework.context.annotation.Conditional;
import java.lang.annotation.*;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(ClassCondition.class)
public @interface ConditionOnClass {
    String[] value();
}

配置类:

package com.example.condition.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;
public class ClassCondition implements Condition {
    
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Environment environment = context.getEnvironment();
        //1.需求:导入指定坐标后创建Bean
        //思路:判断指定坐标文件是否存在
        //获取注解属性值 value
        Map<String, Object> map = metadata.getAnnotationAttributes(ConditionOnClass.class.getName());
        String[] value = (String[]) map.get("value");
        boolean flag = true;
        try {
            for (String className : value) {
                Class<?> cls = Class.forName(className);
            }
        } catch (ClassNotFoundException e) {
            flag = false;
        }
        return flag;
    }
}

测试类:

package com.example.condition;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class SpringbootDemo01Application {
    public static void main(String[] args) {
        //启动SpringBoot应用,返回Spring的IOC容器
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootDemo01Application.class, args);
        Object user = context.getBean("user");
        System.out.println(user);
    }
}

总结

自定义条件:

① 定义条件类:自定义类实现Condition接口,重写 matches 方法,在 matches 方法中进行逻辑判断,返回 boolean值 。 matches 方法两个参数:

  • context:上下文对象,可以获取属性值,获取类加载器,获取BeanFactory等。
  • metadata:元数据对象,用于获取注解属性。

② 判断条件:在初始化Bean时,使用 @Conditional(条件类.class)注解

SpringBoot 提供的常用条件注解:

  • ConditionalOnProperty:判断配置文件中是否有对应属性和值才初始化Bean
@Bean
@ConditionalOnProperty(name = "com",havingValue = "example")
public User user1(){
    return new User();
}

配置文件添加一下属性:

com = example

  • ConditionalOnClass:判断环境中是否有对应字节码文件才初始化Bean
  • ConditionalOnMissingBean:判断环境中没有对应Bean才初始化Bean

到此这篇关于SpringBoot自动装配之Condition深入讲解的文章就介绍到这了,更多相关SpringBoot Condition内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot自动装配之Condition深入讲解

本文链接: https://www.lsjlt.com/news/177829.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作