• SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile


    一、用@Conditional根据条件决定是否要注入bean

    1.

    package com.habuma.restfun;
    
    public class MagicBean {
    
    }

    2.

    package com.habuma.restfun;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Conditional;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class MagicConfig {
    
      @Bean
      @Conditional(MagicExistsCondition.class)
      public MagicBean magicBean() {
        return new MagicBean();
      }
      
    }

    3.

    package com.habuma.restfun;
    
    import org.springframework.context.annotation.Condition;
    import org.springframework.context.annotation.ConditionContext;
    import org.springframework.core.env.Environment;
    import org.springframework.core.type.AnnotatedTypeMetadata;
    
    public class MagicExistsCondition implements Condition {
    
      @Override
      public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        Environment env = context.getEnvironment();
        return env.containsProperty("magic");
      }
      
    }

    4.

     1 package com.habuma.restfun;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import org.junit.Test;
     6 import org.junit.runner.RunWith;
     7 import org.springframework.beans.factory.annotation.Autowired;
     8 import org.springframework.context.ApplicationContext;
     9 import org.springframework.test.context.ContextConfiguration;
    10 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    11 
    12 @RunWith(SpringJUnit4ClassRunner.class)
    13 @ContextConfiguration(classes=MagicConfig.class)
    14 public class MagicExistsTest {
    15 
    16   @Autowired
    17   private ApplicationContext context;
    18   
    19   /*
    20    * This test will fail until you set a "magic" property.
    21    * You can set this property as an environment variable, a JVM system property, by adding a @BeforeClass
    22    * method and calling System.setProperty() or one of several other options.
    23    */
    24   @Test
    25   public void shouldNotBeNull() {
    26     assertTrue(context.containsBean("magicBean"));
    27   }
    28   
    29 }

    二、用@Conditional处理profile

    1.

    1 @Retention(RetentionPolicy.RUNTIME)
    2 @Target({ElementType.TYPE, ElementType.METHOD})
    3 @Documented
    4 @Conditional(ProfileCondition.class)
    5 public @interface Profile {
    6     String[] value();
    7 }

    2.

     1 class ProfileCondition implements Condition {
     2     public boolean matches(
     3         ConditionContext context, AnnotatedTypeMetadata metadata) {
     4         if (context.getEnvironment() != null) {
     5             MultiValueMap < String, Object > attrs =
     6                 metadata.getAllAnnotationAttributes(Profile.class.getName());
     7             if (attrs != null) {
     8                 for (Object value: attrs.get("value")) {
     9                     if (context.getEnvironment()
    10                         .acceptsProfiles(((String[]) value))) {
    11                         return true;
    12                     }
    13                 }
    14                 return false;
    15             }
    16         }
    17         return true;
    18     }
    19 }

    As you can see, ProfileCondition fetches all the annotation attributes for the
    @Profile annotation from AnnotatedTypeMetadata . With that, it checks explicitly for
    the value attribute, which contains the name of the bean’s profile. It then consults
    with the Environment retrieved from the ConditionContext to see whether the pro-
    file is active (by calling the acceptsProfiles() method).

  • 相关阅读:
    1. 首次运行 Git 的配置
    本地git连接github
    Cygwin(linux)中vim配置
    ibatis入门
    XML(可扩展性标记语言)学习汇总一
    第一只小啊小爬虫(纪念下)
    Android SQLite DB的封装
    关于做项目的一些感想
    Git 初学三(git对象与git重置)
    Git 初学二(暂存区)
  • 原文地址:https://www.cnblogs.com/shamgod/p/5235454.html
Copyright © 2020-2023  润新知