好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Spring Boot 条件注解详情

前言:

SpringBoot条件注解@Conditional,可用于根据某个特定的条件来判断是否需要创建某个特定的Bean。SpringBoot自动配置功能里面就大量的使用了条件注解。接下来我们就对@Conditional的使用做一个简单的介绍。

@Conditional注解需要和Condition接口搭配一起使用。通过对应Condition接口来告知是否满足匹配条件。

?

1

2

3

4

5

6

7

8

9

10

@Target ({ElementType.TYPE, ElementType.METHOD})

@Retention (RetentionPolicy.RUNTIME)

@Documented

public @interface Conditional {

 

     /**

      * 所有用于匹配的Condition接口(实现该接口的类),只有这些类都返回true才认为是满足条件

      */

     Class<? extends Condition>[] value();

}

@Conditional注解可以添加在@Configuration、@Component、@Service等修饰的类上用于控制对应的Bean是否需要创建,或者添加在@Bean修饰的方法上用于控制方法对应的Bean是否需要创建。

@Conditional添加在@Configuration修饰的类上,用于控制该类和该类里面所有添加的@Bean方法对应的Bean是否需要创建。

一 @Conditional扩展注解

为了方便我们的使用Spring Boot对@Conditional条件注解做了一些扩展,提供了一些很实用的扩展性条件注解。

上面的扩展注解我们可以简单的分为以下几类:

Bean作为条件: @ConditionalOnBean、@ConditionalOnMissingBean、@ConditionalOnSingleCandidate。 类作为条件: @ConditionalOnClass、@ConditionalOnMissingClass。 SpEL表达式作为条件: @ConditionalOnExpression。 JAVA版本作为条件: @ConditionalOnJava 配置属性作为条件: @ConditionalOnProperty。 资源文件作为条件: @ConditionalOnResource。 是否Web应用作为判断条件: @ConditionalOnWebApplication、@ConditionalOnNotWebApplication。

1.1 Bean作为条件

1.1.1 @ConditionalOnBean

 @ConditionalOnBean对应的Condition处理类是OnBeanCondition。如果Spring容器里面存在指定的Bean则生效。

@ConditionalOnBean配置参数

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

@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 {};

}

1.1.2 @ConditionalOnMissingBean

@ConditionalOnMissingBean对应的Condition实现类是OnBeanCondition。如果Spring容器里面不存在指定的Bean则生效。

@ConditionalOnMissingBean配置参数

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

@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 {};

}

比如如下的实例,当容器里面不存在redisTemplate对应的Bean的时候,就会创建一个RedisTemplate添加到容器里面去。

?

1

2

3

4

5

6

7

8

@Bean

@ConditionalOnMissingBean (name = "redisTemplate" )

public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory)

         throws UnknownHostException {

     RedisTemplate<Object, Object> template = new RedisTemplate<>();

     template.setConnectionFactory(redisConnectionFactory);

     return template;

}

1.1.3 @ConditionalOnSingleCandidate

 @ConditionalOnSingleCandidate对应的Condition处理类是OnBeanCondition。如果当指定Bean在容器中只有一个,或者虽然有多个但是指定首选Bean的时候则生效。

@ConditionalOnSingleCandidate配置参数

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

@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;

}

1.2 类作为条件

1.2.1 @ConditionalOnClass

 @ConditionalOnClass对应的Condition处理类是OnClassCondition。如果当前类路径下面有指定的类的时候则生效。

@ConditionalOnClass配置属性介绍

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

@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 {};

}

1.2.2 @ConditionalOnMissingClass

 @ConditionalOnMissingClass对应的Condition处理类是OnClassCondition。如果当前类路径下面没有指定的类的时候则生效。

?

1

2

3

4

5

6

7

8

9

10

@Target ({ ElementType.TYPE, ElementType.METHOD })

@Retention (RetentionPolicy.RUNTIME)

@Documented

@Conditional (OnClassCondition. class )

public @interface ConditionalOnMissingClass {

     /**

      * 需要作为条件的类的Name, Class.getName()

      */

     String[] value() default {};

}

1.3 SpEL表达式作为条件

@ConditionalOnExpression对应的Condition处理类是OnExpressionCondition。只有当SpEL表达式满足条件的时候则生效。

?

1

2

3

4

5

6

7

8

9

10

@Retention (RetentionPolicy.RUNTIME)

@Target ({ ElementType.TYPE, ElementType.METHOD })

@Documented

@Conditional (OnExpressionCondition. class )

public @interface ConditionalOnExpression {

     /**

      * 要作为条件的SpEL表达式

      */

     String value() default "true" ;

}

例如@ConditionalOnExpression("${ test.enabled:true }"),只有当配置文件里面存在test.enabled: true的时候则生效。

更加详细的用法可以去看下SpEL表达式的使用。

1.4 JAVA版本作为判断条件

 @ConditionalOnJava对应的Condition处理类是OnJavaCondition。只有当指定的JAVA版本条件满足的时候,才会创建对应的Bean。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

@Target ({ ElementType.TYPE, ElementType.METHOD })

@Retention (RetentionPolicy.RUNTIME)

@Documented

@Conditional (OnJavaCondition. class )

public @interface ConditionalOnJava {

     /**

      * 比较方式,Range.EQUAL_OR_NEWER:当前版本等于或高于、Range.OLDER_THAN:当前版本老于,越早的版本越老

      */

     Range range() default Range.EQUAL_OR_NEWER;

 

     /**

      * 指定JAVA版本

      */

     JavaVersion value();

     /**

      * Range options.

      */

     enum Range {

         /**

          * Equal to, or newer than the specified {@link JavaVersion}.

          */

         EQUAL_OR_NEWER,

 

         /**

          * Older than the specified {@link JavaVersion}.

          */

         OLDER_THAN

     }

}

1.5 配置属性作为判断条件

@ConditionalOnProperty对应的Condition实现类OnPropertyCondition。只有当对应的配置属性和给定条件的值相等的时候则生效。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

@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 ;

}

 @ConditionalOnProperty(prefix = [spring.aop], name = [auto], havingValue = [true])表示当配置文件里面spring.aop.auto=true的时候才会加载对应的Bean。

1.6 资源文件是否存在作为判断条件

@ConditionalOnResource对应的Condition处理类OnResourceCondition。只有当指定的资源文件出现在classpath中则生效。

@ConditionalOnResource配置属性

?

1

2

3

4

5

6

7

8

9

10

@Target ({ ElementType.TYPE, ElementType.METHOD })

@Retention (RetentionPolicy.RUNTIME)

@Documented

@Conditional (OnResourceCondition. class )

public @interface ConditionalOnResource {

     /**

      * 要作为判断条件的资源文件名称  @ConditionalOnResource(resources=]mybatis.xml])

      */

     String[] resources() default {};

}

1.7 是否Web应用作为判断条件

1.7.1 @ConditionalOnWebApplication

@ConditionalOnWebApplication对应的Condition处理类是OnWebApplicationCondition。只有当当前项目是Web项目的时候则生效。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

@Target ({ ElementType.TYPE, ElementType.METHOD })

@Retention (RetentionPolicy.RUNTIME)

@Documented

@Conditional (OnWebApplicationCondition. class )

public @interface ConditionalOnWebApplication {

     /**

      * 需要作为条件的Web应用程序的必需类型

      */

     Type type() default Type.ANY;

 

     /**

      * Available application types.

      */

     enum Type {

 

         /**

          * 任何web应用都将匹配

          */

         ANY,

 

         /**

          * 仅基于servlet的Web应用程序将匹配

          */

         SERVLET,

 

         /**

          * 仅基于反应式的Web应用程序将匹配

          */

         REACTIVE

     }

}

1.7.2 @ConditionalOnNotWebApplication

@ConditionalOnNotWebApplication对应的Condition处理类是OnWebApplicationCondition。只有当当前项目不是Web项目的时候则生效。

?

1

2

3

4

5

6

@Target ({ ElementType.TYPE, ElementType.METHOD })

@Retention (RetentionPolicy.RUNTIME)

@Documented

@Conditional (OnWebApplicationCondition. class )

public @interface ConditionalOnNotWebApplication {

}

二 @Conditional自定义

上面介绍每个扩展注解的时候都特意提到了每个注解对应的Condition实现类。其实我们可以仿照这些Condition实现类来实现我们自己的@Conditional注解。下面我们同个两个简单的实例来看下怎么实现自己的@Conditional扩展注解。

2.1 判断是否配置指定属性

 注意: 和@ConditionalOnProperty不一样哦,@ConditionalOnProperty是判断是否有属性并且判断值是否等于我们指定的值。我们要实现的注解只判断有没有配置属性,不管属性对应的值。

扩展注解ConditionalOnPropertyExist。指定我们的Condition实现类OnPropertyExistCondition。并且指定两个参数。一个是参数name用于指定属性。另一个参数exist用于指定是判断存在还是不存在。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

@Retention (RetentionPolicy.RUNTIME)

@Target ({ElementType.TYPE, ElementType.METHOD})

@Documented

@Conditional (OnPropertyExistCondition. class )

public @interface ConditionalOnPropertyExist {

 

     /**

      * 配置文件里面对应的key

      */

     String name() default "" ;

 

     /**

      * 是否有配置的时候判断通过

      */

     boolean exist() default true ;

 

}

OnPropertyExistCondition类就是简单的判断下属性存在与否。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

public class OnPropertyExistCondition implements Condition {

     @Override

     public boolean matches(ConditionContext conditionContext, AnnotatedTypeMetadata annotatedTypeMetadata) {

         Map<String, Object> annotationAttributes = annotatedTypeMetadata.getAnnotationAttributes(ConditionalOnPropertyExist. class .getName());

         if (annotationAttributes == null ) {

             return false ;

         }

         String propertyName = (String) annotationAttributes.get( "name" );

         boolean values = ( boolean ) annotationAttributes.get( "exist" );

         String propertyValue = conditionContext.getEnvironment().getProperty(propertyName);

         if (values) {

             return !StringUtils.isEmpty(propertyValue);

         } else {

             return StringUtils.isEmpty(propertyValue);

         }

     }

}

2.1 判断是否配置指定属性

我们简单实现这样一个功能,根据指定的系统加载不同的Bean。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

@Retention (RetentionPolicy.RUNTIME)

@Target ({ElementType.TYPE, ElementType.METHOD})

@Documented

@Conditional (OnSystemCondition. class )

public @interface ConditionalOnSystem {

     /**

      * 指定系统

      */

     SystemType type() default SystemType.WINDOWS;

 

     /**

      * 系统类型

      */

     enum SystemType {

 

         /**

          * windows系统

          */

         WINDOWS,

 

         /**

          * linux系统

          */

         LINUX,

 

         /**

          * mac系统

          */

         MAC

 

     }

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

public class OnSystemCondition implements Condition {

     @Override

     public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {

         Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(ConditionalOnSystem. class .getName());

         if (annotationAttributes == null ) {

             return false ;

         }

         ConditionalOnSystem.SystemType systemType = (ConditionalOnSystem.SystemType) annotationAttributes.get( "type" );

         switch (systemType) {

             case WINDOWS:

                 return context.getEnvironment().getProperty( "os.name" ).contains( "Windows" );

             case LINUX:

                 return context.getEnvironment().getProperty( "os.name" ).contains( "Linux " );

             case MAC:

                 return context.getEnvironment().getProperty( "os.name" ).contains( "Mac " );

         }

         return false ;

     }

}

到此这篇关于Spring Boot 条件注解详情的文章就介绍到这了,更多相关Spring Boot内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://HdhCmsTestjianshu测试数据/p/e21b9079a23c

查看更多关于Spring Boot 条件注解详情的详细内容...

  阅读:20次