springboot中的condition包下常用条件依赖注解案例介绍-mile米乐体育
目录
- 一、@conditionalonclass() spring中存在指定class对象时,注入指定配置
- 1.首先引入pom依赖
- 2.实体类测试对象
- 3.定义@conditionalonclass()配置类
- 4.启动类测试
- 二、注入指定配置
- 1.首先引入pom依赖
- 2.实体类测试对象
- 3.定义@conditionalonmissingclass()配置类
- 4.启动类测试
- 三、加载指定配置
- 1.首先引入pom依赖
- 2.实体类测试对象
- 2.1 引入条件判断实体类
- 3.定义@conditionalonbean()配置类
- 4.启动类测试
- 四、ioc中判断
- 1.首先引入pom依赖
- 2.实体类测试对象
- 2.1 引入条件判断实体类
- 3.定义@conditionalonmissingbean()配置类
- 4.启动类测试
- 五、@conditionalonproperty()加载配置
- 1.首先引入pom依赖
- 2.实体类测试对象
- 3.定义@conditionalonproperty() 配置类
- 4.application.yml配置
- 5.启动类测试
- 六、@conditionalonexpression() 配置文件
- 1.首先引入pom依赖
- 2.实体类测试对象
- 3.定义@conditionalonexpression() 配置类
- 4.application.yml配置
- 5.启动类测试
- 七、@conditionalonresource() 指定的资源文件出现在classpath中生效
- 1.首先引入pom依赖
- 2.实体类测试对象
- 3.定义@conditionalonresource()配置类
- 4.启动类测试
一、@conditionalonclass() spring中存在指定class对象时,注入指定配置
和conditionalonbean()的区别在于conditionalonbean()是根据ioc里是否有此实例对象,而conditionalonclass()表示只要在spring中有这个类就可以
1.首先引入pom依赖
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");//这里因为是注入iocpkqgcjvq时 @bean没有指定名字,则就是方法名从ioc中获取此对象 system.out.println(getstudent); //student{name='小白', age=12} } }
二、注入指定配置
@conditionalonmissingclass() spring中不存在指定class对象时,注入指定配置
和conditionalonmissingbean()的区别在于conditionalonmissingbean()是根据ioc里没有此实例对象,而conditionalonclass()表示只要在spring中没有这个类就可以
1.首先引入pom依赖
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依赖
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(); } }>
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依赖
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依赖
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条件依赖注解内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!