返回顶部
首页 > 资讯 > 后端开发 > Python >SpringBoot中的Condition包下常用条件依赖注解案例介绍
  • 121
分享到

SpringBoot中的Condition包下常用条件依赖注解案例介绍

2024-04-02 19:04:59 121人浏览 薄情痞子

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

摘要

目录一、@ConditionalOnClass() spring中存在指定class对象时,注入指定配置1.首先引入pom依赖2.实体类测试对象3.定义@ConditionalOnC

一、@ConditionalOnClass() Spring中存在指定class对象时,注入指定配置

和ConditionalOnBean()的区别在于ConditionalOnBean()是根据ioc里是否有此实例对象,而ConditionalOnClass()表示只要在Spring中有这个类就可以

1.首先引入pom依赖

<!--引入SpringBoot父依赖-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.2.RELEASE</version>
</parent>
<dependencies>
    <!--引入启动器依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-WEB</artifactId>
    </dependency>
<!--用JSON来判断是否创建bean-->
	<dependency>
   		<groupId>com.alibaba</groupId>
   	 	<artifactId>fastjson</artifactId>
   		<version>1.2.71</version>
	</dependency>
</dependencies>

2.实体类测试对象

根据Condition返回结果表示是否将此对象注入ioc中; true:表示注入ioc

//根据Condition返回结果表示是否注入ioc中true:表示注入ioc
public class Student {
    String name="小白";
    Integer age =12;
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
 

3.定义@ConditionalOnClass()配置类

name = "com.alibaba.fastjson.JSON" 表示此spring中是否有这个类,有的话就注入此配置到ioc

import com.it.mhh.entry.Student;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomConditionOnClass {
    @Bean
    @ConditionalOnClass(name = "com.alibaba.fastjson.JSON")
    //这里就是判断是否吧此对象注入IOC,ClassCondition此类matches对象的返回的Boolean[true:创建,false:不创建]
    public Student getStudent() {
        return new Student();
    }
}

4.启动类测试

@ConditionalOnClass() Spring中存在指定class,对应该配置才会生效

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
//@ConditionalOnClass() Spring中存在指定class,对应该配置才会生效
@SpringBootApplication
public class ConditionApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(ConditionApplication.class, args);
        //Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'getStudent' available[表示没有获取到这个bean]
        Object getStudent = run.getBean("getStudent");//这里因为是注入ioc时 @Bean没有指定名字,则就是方法名从ioc中获取此对象
        System.out.println(getStudent); //Student{name='小白', age=12}
    }
}

二、注入指定配置

@ConditionalOnMissingClass() Spring中不存在指定class对象时,注入指定配置

和ConditionalOnMissingBean()的区别在于ConditionalOnMissingBean()是根据ioc里没有此实例对象,而ConditionalOnClass()表示只要在Spring中没有这个类就可以

1.首先引入pom依赖

<!--引入springboot父依赖-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.2.RELEASE</version>
</parent>
<dependencies>
    <!--引入启动器依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
<!--用json来判断是否创建bean-->
<!--<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.71</version>
</dependency>-->
</dependencies>

2.实体类测试对象

根据Condition返回结果表示是否将此对象注入ioc中; true:表示注入ioc

//根据Condition返回结果表示是否注入ioc中true:表示注入ioc
public class Student {
    String name="小白";
    Integer age =12;
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3.定义@ConditionalOnMissingClass()配置类

name = "com.alibaba.fastjson.JSON" 表示此spring中是否有这个类,有的话就==不==注入此配置到ioc

import com.it.mhh.entry.Student;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomConditionOnMissingClass {
    @Bean
    @ConditionalOnMissingClass("com.alibaba.fastjson.JSON")
    //这里就是判断是否吧此对象注入IOC,ClassCondition此类matches对象的返回的Boolean[true:创建,false:不创建]
    public Student getStudent() {
        return new Student();
    }
}

4.启动类测试

@ConditionalOnMissingClass Spring容器中==不存在==指定class,注入指定配置

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
//@ConditionalOnMissingClass Spring容器中不存在指定class,注入指定配置
@SpringBootApplication
public class ConditionApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(ConditionApplication.class, args);
        //Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'getStudent' available[表示没有获取到这个bean]
        Object getStudent = run.getBean("getStudent");//这里因为是注入ioc时 @Bean没有指定名字,则就是方法名从ioc中获取此对象
        System.out.println(getStudent); //Student{name='小白', age=12}
    }
}

三、加载指定配置

@ConditionalOnBean() 根据ioc中判断有没有此实例对象,有则加载指定配置

和ConditionalOnClass()的区别在于ConditionalOnClass()是根据Spring中是否有此类,而ConditionalOnBean()表示在ioc中是否由此实例对象;

1.首先引入pom依赖

<!--引入springboot父依赖-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.2.RELEASE</version>
</parent>
<dependencies>
    <!--引入启动器依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2.实体类测试对象

根据Condition返回结果表示是否将此对象注入ioc中; true:表示注入ioc

//根据Condition返回结果表示是否注入ioc中true:表示注入ioc
public class Student {
    String name="小白";
    Integer age =12;
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

2.1 引入条件判断实体类

将此类==注入==ioc容器

importorg.springframework.stereotype.Component;
@Component
Public class BeanServer{
}

3.定义@ConditionalOnBean()配置类

BeanServer.class :判断ioc中是否有此类对象,==有==的话就加载此配置

import com.it.mhh.customConditionOnBeanOrOnMissingBean.server.TestServer;
import com.it.mhh.entry.Student;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomConditionOnBean {
    @Bean
    @ConditionalOnBean(BeanServer.class)
    //这里就是判断是否吧此对象注入IOC,ClassCondition此类matches对象的返回的Boolean[true:创建,false:不创建]
    public Student getStudent() {
        return new Student();
    }
}

4.启动类测试

@ConditionalOnBean() Spring容器中==存在==指定class实例对象时,注入指定配置

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
//@ConditionalOnBean() Spring容器中存在指定class实例对象时,注入指定配置
@SpringBootApplication
public class ConditionApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(ConditionApplication.class, args);
        //Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'getStudent' available[表示没有获取到这个bean]
        Object getStudent = run.getBean("getStudent");//这里因为是注入ioc时 @Bean没有指定名字,则就是方法名从ioc中获取此对象
        System.out.println(getStudent); //Student{name='小白', age=12}
    }
}

四、ioc中判断

ConditionalOnMissingBean() 根据ioc中判断有没有此实例对象,没有则加载指定配置

和ConditionalOnMissingClass()的区别在于ConditionalOnMissingClass()是根据Spring中是否有此类,而ConditionalOMissingnBean()表示在ioc中是否由此实例对象;

1.首先引入pom依赖

<!--引入springboot父依赖-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.2.RELEASE</version>
</parent>
<dependencies>
    <!--引入启动器依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2.实体类测试对象

根据Condition返回结果表示是否将此对象注入ioc中; true:表示注入ioc

//根据Condition返回结果表示是否注入ioc中true:表示注入ioc
public class Student {
    String name="小白";
    Integer age =12;
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
 

2.1 引入条件判断实体类

==不注入==此类到ioc容器

importorg.springframework.stereotype.Component;
//@Component
Public class BeanServer{
}

3.定义@ConditionalOnMissingBean()配置类

BeanServer.class :判断ioc中是否有此类对象,==没有==的话就加载此配置

import com.it.mhh.customConditionOnBeanOrOnMissingBean.server.TestServer;
import com.it.mhh.entry.Student;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
//@Configuration
public class CustomConditionOnMissingBean {

    @Bean
    @ConditionalOnMissingBean(BeanServer.class)
    //这里就是判断是否吧此对象注入IOC,ClassCondition此类matches对象的返回的Boolean[true:创建,false:不创建]
    public Student getStudent() {
        return new Student();
    }
}

4.启动类测试

@ConditionalOnMissingBean() Spring ioc中不存在指定class实例对象时,注入指定配置

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
//@ConditionalOnMissingBean() Spring容器中不存在指定class实例对象时,注入指定配置
@SpringBootApplication
public class ConditionApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(ConditionApplication.class, args);
        //Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'getStudent' available[表示没有获取到这个bean]
        Object getStudent = run.getBean("getStudent");//这里因为是注入ioc时 @Bean没有指定名字,则就是方法名从ioc中获取此对象
        System.out.println(getStudent); //Student{name='小白', age=12}
    }
}

五、@ConditionalOnProperty()加载配置

@ConditionalOnProperty() 配置文件中配置的指定参数值符合要求时,加载此配置

与@ConditionalOnExpression()的区别在于@ConditionalOnProperty() 注解使用的是通过注解中的属性赋值作为参照物和yml里的配置进行匹配是否为相同;

1.首先引入pom依赖

<!--引入springboot父依赖-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.2.RELEASE</version>
</parent>
<dependencies>
    <!--引入启动器依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2.实体类测试对象

根据Condition返回结果表示是否将此对象注入ioc中; true:表示注入ioc

//根据Condition返回结果表示是否注入ioc中true:表示注入ioc
public class Student {
    String name="小白";
    Integer age =12;
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3.定义@ConditionalOnProperty() 配置类

prefix = "class",name = "teacher",matchIfMissing=false, havingValue = "小黑"
==@ConditionalOnProperty()中属性含义==
public @interface ConditionalOnProperty {String[] value() default {}; // 数组,获取对应yml名称的key,与name含义一样,不能同时使用value和name;

  String prefix() default ""; //配置文件中yml名称的前缀;String[] name() default {};// 数组,获取对应yml名称的key,与value含义一样,不能同时使用value和name;String havingValue() default ""; //配置文件yml的value值boolean matchIfMissing() default false; //配置文件yml中没有与之匹配到的数值,是否加载,true:表示正常加载此配置,false表示如果yml中没有此数值就不加载;}

import com.it.mhh.entry.Student;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomConditionOnProperty {
    @Bean
    @ConditionalOnProperty(prefix = "class",name = "teacher",matchIfMissing=false, havingValue = "小黑")
    //这里就是判断是否吧此对象注入IOC,ClassCondition此类matches对象的返回的Boolean[true:创建,false:不创建]
    public Student getStudent() {
        return new Student();
    }
}

4.application.yml配置

@ConditionalOnProperty()注解中的 prefix 属性就是此yml配的前缀 yml,name就是 teacher,而havingValue 则就是'小黑',如果application中有此配置,则加载相应配置

class:
  teacher: 小黑

5.启动类测试

@ConditionalOnProperty()配置文件中配置的指定参数值符合要求时,加载此配置

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
// @ConditionalOnProperty()配置文件中配置的指定参数值符合要求时,加载此配置
@SpringBootApplication
public class ConditionApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(ConditionApplication.class, args);
        //Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'getStudent' available[表示没有获取到这个bean]
        Object getStudent = run.getBean("getStudent");//这里因为是注入ioc时 @Bean没有指定名字,则就是方法名从ioc中获取此对象
        System.out.println(getStudent); //Student{name='小白', age=12}
    }
}

六、@ConditionalOnExpression() 配置文件

@ConditionalOnExpression() 配置文件中配置的指定参数值与我给的值比较为true时,加载此配置

与@ConditionalOnProperty() 的区别在于@ConditionalOnExpression()注解使用的是SpringEL表达式与我给的值进行匹配,为true时加载配置,而@ConditionalOnProperty() 注解使用的是通过注解中的属性赋值作为参照物和yml里的配置进行匹配是否为相同;

1.首先引入pom依赖

<!--引入springboot父依赖-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.2.RELEASE</version>
</parent>
<dependencies>
    <!--引入启动器依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2.实体类测试对象

根据Condition返回结果表示是否将此对象注入ioc中; true:表示注入ioc

//根据Condition返回结果表示是否注入ioc中true:表示注入ioc
public class Student {
    String name="小白";
    Integer age =12;
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3.定义@ConditionalOnExpression() 配置类

"'${class.teacher}'.equals('小黑')" :Spring EL表达式 ,配置文件class.teacher里对应的值去比较我输入的’小黑‘是否相同,true:加载此配置;

import com.it.mhh.entry.Student;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomConditionOnExpression {
    @Bean
    //配置文件中配置的指定参数值与我要做比较的值为true时,加载此配置
    @ConditionalOnExpression("'${class.teacher}'.equals('小黑')")
    //这里就是判断是否吧此对象注入IOC,ClassCondition此类matches对象的返回的Boolean[true:创建,false:不创建]
    public Student getStudent() {
        return new Student();
    }
}

4.application.yml配置

@ConditionalOnExpression()注解 value属性就是此配置的键 class.teacherg 而获取到的值就是’小黑‘,然后进行比较, true则加载相应配置

class:
  teacher: 小黑

5.启动类测试

@ConditionalOnExpression()//配置文件中配置的指定参数值与我传的参数进行比较,如果相同则为true,加载此配置,否则不加载

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
// @ConditionalOnExpression()//配置文件中配置的指定参数值与我传的参数比较,相同为true时,加载此配置
@SpringBootApplication
public class ConditionApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(ConditionApplication.class, args);
        //Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'getStudent' available[表示没有获取到这个bean]
        Object getStudent = run.getBean("getStudent");//这里因为是注入ioc时 @Bean没有指定名字,则就是方法名从ioc中获取此对象
        System.out.println(getStudent);
    }
}

七、@ConditionalOnResource() 指定的资源文件出现在classpath中生效

(就是编译后target里classes里的路径名是否存在)

指定文件路径名存在时,对应配置生效

1.首先引入pom依赖

<!--引入springboot父依赖-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.2.RELEASE</version>
</parent>
<dependencies>
    <!--引入启动器依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

2.实体类测试对象

根据Condition返回结果表示是否将此对象注入ioc中; true:表示注入ioc

//根据Condition返回结果表示是否注入ioc中true:表示注入ioc
public class Student {
    String name="小白";
    Integer age =12;
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

3.定义@ConditionalOnResource()配置类

resources = "com\it\mhh" :编译后taget->classes里的路径名是否存在,存在为true,加载配置

import com.it.mhh.entry.Student;
import org.springframework.boot.autoconfigure.condition.ConditionalOnResource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CustomConditionOnResource {
    @Bean
    //指定的资源文件出现在classpath中生效(就数编译后target里classes里的包路径)
    @ConditionalOnResource(resources = "com\\it\\mhh")
    //这里就是判断是否吧此对象注入IOC,ClassCondition此类matches对象的返回的Boolean[true:创建,false:不创建]
    public Student getStudent() {
        return new Student();
    }
}

4.启动类测试

@ConditionalOnResource()//指定的资源文件出现在classpath中生效(就数编译后target里classes里的包路径)

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
//@ConditionalOnResource()//指定的资源文件出现在classpath中生效(就数编译后target里classes里的包路径)
public class ConditionApplication {
    public static void main(String[] args) {
        ConfigurableApplicationContext run = SpringApplication.run(ConditionApplication.class, args);
        //Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'getStudent' available[表示没有获取到这个bean]
        Object getStudent = run.getBean("getStudent");//这里因为是注入ioc时 @Bean没有指定名字,则就是方法名从ioc中获取此对象
        System.out.println(getStudent);
    }
}

到此这篇关于SpringBoot中的Condition包下常用条件依赖注解案例介绍的文章就介绍到这了,更多相关SpringBoot条件依赖注解内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: SpringBoot中的Condition包下常用条件依赖注解案例介绍

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

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

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

  • 微信公众号

  • 商务合作