spring conditional注解,springbootconfiguration注解
00-1010 I .当指定类对象存在于@ConditionalOnClass() Spring中时,注入指定配置1。先介绍一下pom依赖2。实体类测试对象3。定义@ConditionalOnClass()配置类4。开始课程测试2。注入指定的配置1。先介绍一下pom依赖2。实体类测试对象3。定义@ConditionalOnMissingClass()配置类4。开始类测试3,加载配置1。先介绍一下pom依赖2。实体类2.1的测试对象引入条件判断实体类3。定义@ ConditionalOnBean()配置类4。开始等级测试4,ioc 1中的判断。先介绍一下pom依赖2。实体类测试对象2.1引入条件判断实体类3。定义@ConditionalOnMissingBean()配置类4。开始班级测试5。@ConditionalOnProperty () Load 1。先介绍一下pom依赖2。实体类3的测试对象。定义@ ConditionalOnProperty()来配置类4。配置4.application.yml 5。开始班级测试6。@ ConditionalonExpress()配置文件1。首先介绍一下pom依赖2。实体类3的测试对象。定义@ conditionalOnExpress()配置类4.application.yml配置5。开始课堂测试7。由@ConditionalOnResource()指定的资源文件出现在类路径中,并从1开始生效。首先介绍一下pom依赖2。实体类3的测试对象。定义@ ConditionalOnResource()配置类4。开始班级测试。
00-1010和ConditionalOnBean()的区别在于,ConditionalOnBean()是基于ioc中是否有这个实例对象,而ConditionalOnClass()是指Spring中有这个类。
目录
!-介绍springboot父依赖-父groupIdorg.springframework.boot/groupId Artifactid spring-Boot-Starter-Parent/Artifactid version2.3.2.RELEASE/version/parentdependencies!-介绍启动器依赖-依赖groupIdorg.springframework.boot/groupId Artifactid spring-Boot-Starter-web/Artifactid/dependency!-使用json确定是否创建bean依赖的groupIdcom.alibaba/groupId工件fast Jason/工件ID版本1 . 2 . 71/Version/dependency/dependencies
00-1010表示是否根据条件返回的结果将该对象注入iocTrue:意味着注入ioc。
//根据条件返回的结果,指示是否注入ioc。true:表示注入IOC公共课学生{string name=小白;整数年龄=12;@Override公共字符串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;@Configurationpublic 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,对应该配置才会生效@SpringBootApplicationpublic 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:表示注入iocpublic 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;@Configurationpublic 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,注入指定配置@SpringBootApplicationpublic 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:表示注入iocpublic class Student { String name="小白"; Integer age =12; @Override public String toString() { return "Student{" + "name=" + name + + ", age=" + age + }; }}
2.1 引入条件判断实体类
将此类==注入==ioc容器
importorg.springframework.stereotype.Component;@ComponentPublic 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;@Configurationpublic 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实例对象时,注入指定配置@SpringBootApplicationpublic 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:表示注入iocpublic class Student { String name="小白"; Integer age =12; @Override public String toString() { return "Student{" + "name=" + name + + ", age=" + age + }; }}
2.1 引入条件判断实体类
==不注入==此类到ioc容器
importorg.springframework.stereotype.Component;//@ComponentPublic 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;//@Configurationpublic 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实例对象时,注入指定配置@SpringBootApplicationpublic 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:表示注入iocpublic 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;@Configurationpublic 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()配置文件中配置的指定参数值符合要求时,加载此配置@SpringBootApplicationpublic 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:表示注入iocpublic 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;@Configurationpublic 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时,加载此配置@SpringBootApplicationpublic 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:表示注入iocpublic class Student { String name="小白"; Integer age =12; @Override public String toString() { return "Student{" + "name=" + name + + ", age=" + age + }; }}
3.定义@ConditionalOnResource()配置类
resources = "comitmhh" :编译后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;@Configurationpublic 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没有指定名
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。