日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2025/3/14 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。