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).

  • 相关阅读:
    20155209林虹宇虚拟机的安装及一点Linux的学习
    预备作业2林虹宇20155209
    预备作业01:你期望的师生关系是什么?
    20155203
    我眼中的师生关系以及对于专业学习的展望
    20155319的第一次随笔
    20155336 2016-2017-2《JAVA程序设计》第二周学习总结
    20155336 2016-2017-2《JAVA程序设计》第一周学习总结
    虎光元的第三次随笔
    20155336虎光元
  • 原文地址:https://www.cnblogs.com/shamgod/p/5235454.html
Copyright © 2011-2022 走看看