SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile...
生活随笔
收集整理的這篇文章主要介紹了
SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile...
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一、用@Conditional根據(jù)條件決定是否要注入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 {@Overridepublic 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).
轉(zhuǎn)載于:https://www.cnblogs.com/shamgod/p/5235454.html
總結(jié)
以上是生活随笔為你收集整理的SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python小程序:备份文件
- 下一篇: AOE网与AOV网