Chapter 3: Advanced Assembly


3.1:
The bean that the profile needs to load is different with the change of the software operating environment. For example, the database used by the development environment and the database used by the runtime environment are different. Spring provides profiles that can determine which bean needs to be used. When using it, you only need to specify different Profiles for different beans, and then activate the required Profiles. In Spring 4.0, @profile was refactored to
declare Profile using java configuration based on @Conditional
//Profile used during development
@configuration
@Profile("dev") //All beans in this configuration class are Belongs to the Profile named dev (with Spring 3.1).
java configuration class{}

//Profile used during testing
@configuration
@Profile("qc")
java configuration class{}

@Configuration
java configuration class{
@Profile("dev") //Specify that the current bean belongs to devProfile (Spring3. 2 Yes)
@Bean
Bean configuration method {}
@Profile("qc"

Bean configuration method {}
}
uses xml configuration declaration Profile
<beans xmlns="http://...."
profile = "dev"> //All beans contained in this <beans> belong to devProfile
<bean>.. ..</bean>
</beans>

//Use the nested <beans> method to configure the beans individually
<beans xmlns="http://...."> //All the items contained in this <beans> All beans belong to devProfile
<beans profile="dev">
<bean>...</bean>
</beans>
<beans profile="qc">
<bean>...</bean>
</beans>
</ beans>
Activation profile:
The two properties that need to be configured to activate the profile are: spring.profiles.active and spring.profiles.default.
The profile configured with active is active. If active is not configured, the default will take effect. If there is no configuration, it will not take effect.
There are many configuration locations (check information)
3.2: Conditional Beans
If there is a Bean, it needs to be created after another specific Bean is declared. @Conditional is provided in Spring 4.0. It can be used in the @Bean configuration method, the bean will be created only when the specified conditions are met, otherwise ignored.
For example: the judgment condition is that there is "JAVA_HOME" in the environment variable.
@Configuration
java configuration class {
@Bean
@Conditional(javaHomeExistsCondition.class) //Configuration comparison class, this class only needs to implement the matches method of the Condition interface.
javaBean configuration method(){...}
}
public class javaHomeExistsCondition implements Condition{ //The matches method in the Condition interface returns a Boolean value to determine whether the verification is passed or not. The two parameters of matches are very useful
public boolean matches(ConditionContext context , AnnotatedTypeMetadata metadata) {
Environment emv = context.getEnvironment();
return env.containsProperty("JAVA_HOME");
}
}
上面的matches方法非常简单,但是功能却非常强大。
借助ConditionContext接口可以实现:
getRegistry() 返回BeanDefinitionRegistry检查bean定义。
getBeanFactory() 返回ConfigurableListableListableBeanFactory检查bean是否存在,探测bean的属性
getEnvironment() 返回Environment检查环境变量
getResourceLoader() 返回ResourceLoader读取并探测加载的资源
getClassLoader() 返回ClassLoader检查类是否存在
借助AnnotatedTypeMetadata接口可以检查带有@Bean注解的方法上还有什么其他注解
3.3:处理自动转配的歧义性
如果使用组件扫描自动装配Bean的话,Spring会寻找符合条件的Bean作为依赖注入,是当有多个Bean同时符合条件时,Spring不知道使用哪个Bean好。抛出异常:NoUniqueBeanDefinitionException。
解决这个问题可以通过:1、标识首选的Bean 2、限定自动装配的Bean (使用方法查资料)
3.4:Bean的作用域
Spring应用上下文中的bean默认都是单例的,但是单例的bean是非常理想的,有时候使用的bean时易变得,会保持某一状态的。Spring还为Bean指定了别的作用域
单例(Singleton) 在整个应用中只创建一个bean的实例。
原型(Prototype) 每次注入或者在上下文中获取的时候都创建一个新的。
会话(Session) Web应用中为每一个会话创建一个Bean实例
请求(Request) Web应用中为每一个请求创建一个Bean实例

配置bean的作用域
使用组件扫描发现bean
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class xxx{...}
在java配置中将bean
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public 类型 方法名(){...}
使用xml配置bean
<bean id = "xxx" class = "xxx.yyy" scope = "prototype"> </bean>
使用会话和请求作用域
将Bean声明为会话和请求作用域时有个问题:如果某些单例的bean依赖了这些bean,单例的bean创建的时候会话bean还不存在。Spring在这个问题上使用代理模式解决。
因此声明会话或者请求作用域时需要加一个参数:
@Scope(WebApplicationContext.SCOPE_SESSION , proxyMode = ScopedProxyMode.INTERFACES)
使用方法???????????????????????
3.5:运行时注入值
????????????????????????





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325305319&siteId=291194637