zoukankan      html  css  js  c++  java
  • 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).

  • 相关阅读:
    2017年6月笔记
    2017年5月笔记
    2017年4月笔记
    转发:i p _ f o r w a r d函数
    IP分组
    IP协议首部结构介绍
    IP:网际协议
    sed工具使用
    正则表达式匹配
    TCP的半连接
  • 原文地址:https://www.cnblogs.com/shamgod/p/5235454.html
Copyright © 2011-2022 走看看