SPRING IN ACTION 第4版笔记-第三章ADVANCING WIRING-003-@Conditional根据条件生成bean及处理profile
2016-03-02 16:25
751 查看
一、用@Conditional根据条件决定是否要注入bean
1.
2.
3.
4.
二、用@Conditional处理profile
1.
2.
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.
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.
package com.habuma.restfun; import static org.junit.Assert.*; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes=MagicConfig.class) public class MagicExistsTest { @Autowired private ApplicationContext context; /* * This test will fail until you set a "magic" property. * You can set this property as an environment variable, a JVM system property, by adding a @BeforeClass * method and calling System.setProperty() or one of several other options. */ @Test public void shouldNotBeNull() { assertTrue(context.containsBean("magicBean")); } }
二、用@Conditional处理profile
1.
@Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) @Documented @Conditional(ProfileCondition.class) public @interface Profile { String[] value(); }
2.
class ProfileCondition implements Condition { public boolean matches( ConditionContext context, AnnotatedTypeMetadata metadata) { if (context.getEnvironment() != null) { MultiValueMap < String, Object > attrs = metadata.getAllAnnotationAttributes(Profile.class.getName()); if (attrs != null) { for (Object value: attrs.get("value")) { if (context.getEnvironment() .acceptsProfiles(((String[]) value))) { return true; } } return false; } } return true; } }
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).
相关文章推荐
- java7 AIO / NIO 2 小记
- Java学习笔记---多线程同步的五种方法
- java String.split 传入参数为正则表达式
- 『java.util』 list 接口研究
- SpringMVC与Struts2的比较
- Java 项目JDBC 链接数据库中会出现的错误
- Java,定义类
- java中set的交集、差集、并集的简单实现
- java线程停止问题
- java内存泄露问题详解
- eclipse android使用Maven deploy构建到Nexus上
- java验证码功能
- Java中控制程序流转
- eclipse 插件编写(三)
- Java通过反射创建内部类
- 3.2 java中堆栈(stack)和堆(heap)(还在问静态变量放哪里,局部变量放哪里,静态区在哪里.....进来)
- Java代码规范
- Spring定时任务的几种实现
- Myeclipse安装使用MAVEN
- java 读写JSON(一)