企业建站个人建站源码给我免费观看片在线
- 作者: 五速梦信息网
- 时间: 2026年03月21日 10:13
当前位置: 首页 > news >正文
企业建站个人建站源码,给我免费观看片在线,网站删除代码,网站快照Spring Conditional Spring 4.0提供的注解。作用是给需要装载的Bean增加一个条件判断。只有满足条件才会装在到IoC容器中。而这个条件可以由自己去完成的#xff0c;可以通过重写Condition接口重写matches()方法去实现自定义的逻辑。所以说这个注解增加了对Bean装载的灵活性。…Spring Conditional Spring 4.0提供的注解。作用是给需要装载的Bean增加一个条件判断。只有满足条件才会装在到IoC容器中。而这个条件可以由自己去完成的可以通过重写Condition接口重写matches()方法去实现自定义的逻辑。所以说这个注解增加了对Bean装载的灵活性。 源码 可以看出来首先可以修饰在类、接口、枚举以及方法上。并且可以接收一个或多个实现Condition接口的类。 那么在Condition接口中只有一个返回布尔类型的matches()方法。从这个单词也看得出来这是匹配的意思所以就是匹配校验Bean是否可以被加载进IoC容器中。Determine if the condition matches确定条件是否匹配。 实战代码 以下先建两个Bean类、一个条件类、一个配置类、以及测试Main类。需要注意的是条件类中的参数并不是Spring的上下文ApplicationContext所以其内容需要设置在-vm options中。至于这个-vm [options]中的options可以通过DOS窗口输入Java就可以看到有什么选项了。 Data NoArgsConstructor AllArgsConstructor public class Animal {private String name;private String sex; } Data NoArgsConstructor AllArgsConstructor public class Person {private String name;private Integer age; } public class PersonCondition implements Condition {/*** param context 上下文* param metadata 注解元信息/Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {// 通过条件上下文获取环境中的配置文件信息String property context.getEnvironment().getProperty(spring.createBean);if(null property) {return false;}return property.contains(person);} } public class AnimalCondition implements Condition {/** param context 上下文* param metadata 注解元信息/Overridepublic boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {// 通过条件上下文获取环境中的配置文件信息String property context.getEnvironment().getProperty(spring.createBean);if(null property) {return false;}return property.contains(animal);} } public class ConditionalTest {public static void main(String[] args) {// 通过Spring上下文ApplicationContext传入配置类获取其中的Bean描述并输出ApplicationContext applicationContext new AnnotationConfigApplicationContext(ConditionalConfig.class);Arrays.stream(applicationContext.getBeanDefinitionNames()).forEach(System.out::println);} } 测试结果 SpringBoot 关于ConditionalOnXxx注解是在SpringBoot中拓展出来的是原先Spring框架中没有存在的注解。那么以下就逐一去了解每个注解的作用。需要说的是这些注解全部都可以注解在类、接口、枚举和方法上。 从上图可以发现有十三种是ConditionalOnXxx。其中就不了解ConditionalOnCloudPlatform与ConditionOnJndi这两个注解了。 上面的扩展注解我们可以简单的分为以下几类 Bean作为条件ConditionalOnBean、ConditionalOnMissingBean、ConditionalOnSingleCandidate。类作为条件ConditionalOnClass、ConditionalOnMissingClass。SpEL表达式作为条件ConditionalOnExpression。Java版本作为条件: ConditionalOnJava配置属性作为条件ConditionalOnProperty。资源文件作为条件ConditionalOnResource。是否Web应用作为判断条件ConditionalOnWebApplication、ConditionalOnNotWebApplication。 条件为Bean的情况 ConditionalOnBean Target({ElementType.TYPE, ElementType.METHOD}) Retention(RetentionPolicy.RUNTIME) Documented Conditional({OnBeanCondition.class}) public interface ConditionalOnBean {/** 需要作为条件的类的Class对象数组/Class?[] value() default {};/** 需要作为条件的类的Name, Class.getName()/String[] type() default {};/** (用于指定注解修饰的Bean)条件所需的注解类/Class? extends Annotation[] annotation() default {};/** Spring容器中Bean的名字/String[] name() default {};/** 搜索容器层级当前容器父容器/SearchStrategy search() default SearchStrategy.ALL;/** 可能在其泛型参数中包含指定Bean类型的其他类/Class?[] parameterizedContainer() default {}; } 源码中的属性就不一一展示测试了这里就测试value于name即可value传入的是Class类型。而这个注解的含义很简单如果IoC容器中存在该注解中value属性对应的Bean那么就加载被该注解注解的Bean。否则不加载。测试代码采用上面Spring目录下的测试结果中的代码。这里主要展示配置类中的逻辑。 Configuration public class ConditionalConfig {Beanpublic Person person() {return new Person();}BeanConditionalOnBean(Person.class)//ConditionalOnBean(name com.gok.entity.Person)public Animal animal() {return new Animal();} } 这里需要注意的是Spring加载Bean是在配置类中自上而下加载的所以说如果person()与animal()两个方法换位置的话Animal是不会被加载到IoC容器中的因为在它加载时Person还没被加载入IoC容器。 ConditionalOnMissingBean Target({ ElementType.TYPE, ElementType.METHOD }) Retention(RetentionPolicy.RUNTIME) Documented Conditional(OnBeanCondition.class) public interface ConditionalOnMissingBean {/** 需要作为条件的类的Class对象数组/Class?[] value() default {};/** 需要作为条件的类的Name, Class.getName()/String[] type() default {};/** 匹配Bean的时候需要忽视的Class对象数组一般是父类* ConditionalOnMissingBean(value JdbcFactory.class, ignored MySqlDefaultFactory.class)/Class?[] ignored() default {};/** 匹配Bean的时候需要忽视的类的Name, Class.getName()/String[] ignoredType() default {};/** (用于指定注解修饰的Bean)条件所需的注解类/Class? extends Annotation[] annotation() default {};/** Spring容器中Bean的名字/String[] name() default {};/** 搜索容器层级当前容器父容器/SearchStrategy search() default SearchStrategy.ALL;/** 可能在其泛型参数中包含指定Bean类型的其他类/Class?[] parameterizedContainer() default {}; } 理解了上面注解的作用那这个注解就游刃有余了miss单词意为错过、没有的意思。所以这个注解的作用就是如果IoC容器中不存在该注解中value属性对应的Bean那么就加载被该注解注解的Bean。否则不加载。 Configuration public class ConditionalConfig {Beanpublic Person person() {return new Person();}BeanConditionalOnMissingBean(Person.class)//ConditionalOnMissingBean(name com.gok.entity.Person)public Animal animal() {return new Animal();} } ConditionalOnSingleCandidate Target({ ElementType.TYPE, ElementType.METHOD }) Retention(RetentionPolicy.RUNTIME) Documented Conditional(OnBeanCondition.class) public interface ConditionalOnSingleCandidate {/** 需要作为条件的类的Class对象/Class? value() default Object.class;/** 需要作为条件的类的Name, Class.getName()/String type() default ;/** 搜索容器层级当前容器父容器/SearchStrategy search() default SearchStrategy.ALL; } 此注解从单词single与candidate可以得出是单个候选人的意思。大致可以猜测是存在相同类型的Bean的话只会对单个有效。我尝试将其放到person02()上还是一样将这两个Bean加载到了IoC当中但是放在第一个person01()上导致person01没有被加载到IoC容器当中。所以此Bean的作用就是如果当指定Bean在容器中只有一个或者虽然有多个但是指定首选Bean的时候则生效。即同类型的Bean中首选Bean无法被加载入IoC容器中。 Configuration public class ConditionalConfig {BeanConditionalOnSingleCandidatepublic Person person01() {return new Person();}Beanpublic Person person02() {return new Person();} } 条件为类的情况 ConditionalOnClass Target({ ElementType.TYPE, ElementType.METHOD }) Retention(RetentionPolicy.RUNTIME) Documented Conditional(OnClassCondition.class) public interface ConditionalOnClass {/** 需要作为条件的类的Class对象数组/Class?[] value() default {};/** 需要作为条件的类的Name, Class.getName()/String[] name() default {}; } 这个其实和ConditionalOnBean类似但是那个注解是在IoC容器中或者是类全限定名找是否存在该Spring Bean。而ConditionalOnClas是在IoC容器中或者是类全限定名找到是否存在该类。如果存在就加载不存在就不加载到IoC容器中。 ConditionalMissingClass Target({ElementType.TYPE, ElementType.METHOD}) Retention(RetentionPolicy.RUNTIME) Documented Conditional({OnClassCondition.class}) public interface ConditionalOnMissingClass {/** 需要作为条件的类的Name, Class.getName()/String[] value() default {}; } 与ConditionalOnClass相反。会在这里一起展示代码以及测试的结果。Plant类是真实存在的所以说person01被加载到IoC容器中而person02没有被加载到IoC当中。 Configuration public class ConditionalConfig {BeanConditionalOnClass(Animal.class)public Person person01() {return new Person();}BeanConditionalOnMissingClass(com.gok.entity.Animal)public Person person02() {return new Person();} } 条件为SpEL表达式的情况 ConditionalOnExpression Retention(RetentionPolicy.RUNTIME) Target({ ElementType.TYPE, ElementType.METHOD }) Documented Conditional(OnExpressionCondition.class) public interface ConditionalOnExpression {/** 要作为条件的SpEL表达式/String value() default true; } 这个注解就是用来判断该Bean是否符合SpEL表达式至于什么是SpEL表达式就自行百度学习了就不多放篇幅去详细说明了。这里我设置person01为true而person02为false。 Configuration public class ConditionalConfig {BeanConditionalOnExpression(true)public Person person01() {return new Person();}BeanConditionalOnExpression(false)public Person person02() {return new Person();} } 条件为Java的情况 ConditionalOnJava Target({ ElementType.TYPE, ElementType.METHOD }) Retention(RetentionPolicy.RUNTIME) Documented Conditional(OnJavaCondition.class) public interface ConditionalOnJava {/** 比较方式Range.EQUAL_OR_NEWER:当前版本等于或高于、Range.OLDER_THAN:当前版本老于越早的版本越老/ConditionalOnJava.Range range() default ConditionalOnJava.Range.EQUAL_OR_NEWER;/** 指定JAVA版本/JavaVersion value();/** Range options./public static enum Range {/** Equal to, or newer than the specified {link JavaVersion}./EQUAL_OR_NEWER,/** Older than the specified {link JavaVersion}./OLDER_THANprivate Range() {}} }此注解用来判断当前运行环境的Java版本是多少。符合范围内的条件才会加载Bean。 Configuration public class ConditionalConfig {BeanConditionalOnJava(JavaVersion.EIGHT)public Person person01() {return new Person();}BeanConditionalOnJava(JavaVersion.NINE)public Person person02() {return new Person();} } 条件为配置条件的情况 ConditionalOnProperty Retention(RetentionPolicy.RUNTIME) Target({ ElementType.TYPE, ElementType.METHOD }) Documented Conditional(OnPropertyCondition.class) public interface ConditionalOnProperty {/** 对应property名称的值/String[] value() default {};String[] name() default {};/** property名称的前缀可有可无/String prefix() default ;/** 与name组合使用比较获取到的属性值与havingValue给定的值是否相同相同才加载配置/String havingValue() default ;/** 缺少该property时是否可以加载。如果为true没有该property也会正常加载反之报错*/boolean matchIfMissing() default false; } 此注解用于条件配置中读取peoperties文件中的信息。本人测试读取yml无效需要在配置类上多添加个PropertySource注解读取文件才能够使用配置条件注解。
application.properties中的内容
com.gok.testtrue com.gok.password123456 Configuration // 读取properties文件的方式 可以配合Value注解读取详细信息 PropertySource(value classpath:application.properties, encoding UTF-8) //PropertySources({PropertySource(value classpath:application.properties, encoding UTF-8)}) public class ConditionalConfig {BeanConditionalOnProperty(com.gok.test)public Person person01() {return new Person();}BeanConditionalOnProperty(name com.gok.test)public Person person02() {return new Person();}BeanConditionalOnProperty(com.gok.password)public Person person03() {return new Person();}BeanConditionalOnProperty(name com.gok.password, havingValue 123456)public Person person04() {return new Person();}BeanConditionalOnProperty(name com.gok.password, havingValue 123456789)public Person person05() {return new Person();}BeanConditionalOnProperty(value com.gok.password123456, matchIfMissing true)public Person person06() {return new Person();}Bean// 这里要注意如果要使用prefix前缀的话 必须带上name或者value// 或者会报错The name or value attribute of ConditionalOnProperty must be specified// 以下拼接即为是否存在com.gok.password这个属性ConditionalOnProperty(prefix com.gok, name password)public Person person07() {return new Person();} } 条件为资源条件的情况 ConditionalOnResource Target({ ElementType.TYPE, ElementType.METHOD }) Retention(RetentionPolicy.RUNTIME) Documented Conditional(OnResourceCondition.class) public interface ConditionalOnResource {/*** 要作为判断条件的资源文件名称 ConditionalOnResource(resources ”mybatis.xml”)/String[] resources() default {}; } 查询指定的资源不仅仅可以查找classpath下的文件还可以用来查找外部资源是否存在。 Configuration public class ConditionalConfig {BeanConditionalOnResource(resources https://www.baidu.com)public Person person01() {return new Person();}BeanConditionalOnResource(resources classpath:application.properties)public Person person02() {return new Person();}BeanConditionalOnResource(resources https://www.baiduhaha.com)public Person person03() {return new Person();} }条件为Web应用的情况 ConditionalOnWebApplication Target({ ElementType.TYPE, ElementType.METHOD }) Retention(RetentionPolicy.RUNTIME) Documented Conditional(OnWebApplicationCondition.class) public interface ConditionalOnWebApplication {/** 需要作为条件的Web应用程序的必需类型/ConditionalOnWebApplication.Type type() default ConditionalOnWebApplication.Type.ANY;/** Available application types./public static enum Type {/** 任何web应用都将匹配/ANY,/** 仅基于servlet的Web应用程序将匹配/SERVLET,/** 仅基于反应式的Web应用程序将匹配*/REACTIVE;private Type() {}} } 判断当前是否为Web项目/Web环境。主要就是从是否有导入Web的依赖。这里简单介绍以下三种不同情况的依赖引入情况。 !– 无Web容器 – dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId /dependency !– 使用Tomcat/Servlet Web容器 – dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId /dependency !– 使用Netty 响应式的Web容器 – dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-webflux/artifactId /dependency ConditionalOnNotWebApplication Target({ElementType.TYPE, ElementType.METHOD}) Retention(RetentionPolicy.RUNTIME) Documented Conditional({OnWebApplicationCondition.class}) public interface ConditionalOnNotWebApplication { } 参考文章 https://www.cnblogs.com/dusucyy/p/16609736.html ConditionalOnBean详解_你就像甜甜的益达的博客-CSDN博客a
- 上一篇: 企业建站的作用是什么石家庄 网站 科技
- 下一篇: 企业建站公司方案推广方法策略
相关文章
-
企业建站的作用是什么石家庄 网站 科技
企业建站的作用是什么石家庄 网站 科技
- 技术栈
- 2026年03月21日
-
企业建站搭建wordpress 快讯模板
企业建站搭建wordpress 快讯模板
- 技术栈
- 2026年03月21日
-
企业建站 wordpress网站建设服务器有哪些
企业建站 wordpress网站建设服务器有哪些
- 技术栈
- 2026年03月21日
-
企业建站公司方案推广方法策略
企业建站公司方案推广方法策略
- 技术栈
- 2026年03月21日
-
企业建站免费模板长沙做暑假实践活动网站
企业建站免费模板长沙做暑假实践活动网站
- 技术栈
- 2026年03月21日
-
企业建站系统官网免费seo软件推荐
企业建站系统官网免费seo软件推荐
- 技术栈
- 2026年03月21日






