Spring高级装配bean
目录
spring profile
条件化的bean声明
自动装配与歧义性
bean的作用域
Spring表达式语言
一、环境与profile
配置profile bean
在软件开发的时候,有一个很大的挑战就是将应用程序从一个环境迁移到另外一个环境。数据库配置、加密算法以及与外部系统的集成是夸环境部署时会发生变化的几个典型例子。
以下是两个不同环境的DataSource bean。
package springdemo.test1.entity.datasource; import javax.sql.DataSource; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; @Configuration
@Profile("dev")
public class DevelopmentProfileConfig { @Bean(destroyMethod="shutdown")
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:schema.sql")
.addScript("classpath:test-data.sql")
.build();
}
}
package springdemo.test1.entity.datasource; import javax.sql.DataSource; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jndi.JndiObjectFactoryBean; @Configuration
@Profile("prod")
public class ProductionProfileConfig { @Bean
public DataSource dataSource() {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName("jdbc/myDs");
jndiObjectFactoryBean.setResourceRef(true);
jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
return (DataSource)jndiObjectFactoryBean.getObject();
}
}
在该两个配置类中,使用了注解@Profile,它会告诉spring这个配置类中的bean只有在dev profile/prod profile激活时才会创建,如果对应profile没有激活,那么带有@Bean注解的方法都会被忽略掉。
在spring3.2开始,@Profile注解可以使用到方法级别上了,可以与@Bean注解一同使用,这样可以将两个bean的声明放在一个配置类中。
package springdemo.test1.entity.datasource; import javax.sql.DataSource; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.jndi.JndiObjectFactoryBean; @Configuration
public class DataSourceConfig { @Bean(destroyMethod="shutdown")
@Profile("dev")
public DataSource devDataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.H2)
.addScript("classpath:schema.sql")
.addScript("classpath:test-data.sql")
.build();
} @Bean
@Profile("prod")
public DataSource proDataSource() {
JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
jndiObjectFactoryBean.setJndiName("jdbc/myDs");
jndiObjectFactoryBean.setResourceRef(true);
jndiObjectFactoryBean.setProxyInterface(javax.sql.DataSource.class);
return (DataSource)jndiObjectFactoryBean.getObject();
}
}
以上是使用javaConfig配置
也可使用xml配置Profile
使用<beans>元素的profile属性,在xml中配置profile bean
可以创建用于不同环境的xml,但只有profile属性与当前激活profile相匹配的配置文件才会被用到。
另外可以在跟<beans>元素中嵌套定义<beans>元素,而不是为每个环境都创建一个profile xml文件
激活profile
spring在确定哪个profile处于激活状态时,需要依赖两个独立的属性:spring.profiles.active 和 spring.rofiles.default。如果设置了active属性的话,那么它的值就用来确定哪个profile是激活的。如果没有设置active的值,spring会查找default的值。如果两个值都没有设置的话,那就是没有激活的profile,因此只会创建哪些没有定义在profile中的bean。
有多种方式来设置这两个属性:
- 作为DispatcherServlet的初始化参数
- 作为web应用的上下文参数
- 作为JNDI条目
- 作为环境变量
- 作为jvm的系统属性
- 在集成测试类上,使用ActiveProfiles注解设置
二、条件化的bean
spring4引入了一个新的@Conditional注解,它可以用到带有@Bean注解的方法上。如果给定的条件计算结果为true,就会创建这个bean,否则,这个bean会被忽略。
@Bean
@Conditional(EngineCondition.class)
public IEngine setIEngine()
{
return new AudiEngine();
}
@Conditional注解中给定了一个class,它指明了条件。@Conditional将会通过Condition接口进行条件对比:
public abstract interface Condition
{
public abstract boolean matches(ConditionContext paramConditionContext,
AnnotatedTypeMetadata paramAnnotatedTypeMetadata);
}
设置给@Conditional的类可以是任意实现了Condition接口的类型。如果matches()方法返回true,那么就会创建带有@Conditional注解的bean,如果返回为false,将不会创建这些bean。
matches()方法会得到ConditionContext和AnnotatedTypeMetadata对象用来做出决策。
ConditionContext是一个接口,大致如下:
public interface ConditionContext {
/**
* Return the {@link BeanDefinitionRegistry} that will hold the bean definition
* should the condition match or {@code null} if the registry is not available.
* @return the registry or {@code null}
*/
BeanDefinitionRegistry getRegistry();
/**
* Return the {@link ConfigurableListableBeanFactory} that will hold the bean
* definition should the condition match or {@code null} if the bean factory
* is not available.
* @return the bean factory or {@code null}
*/
ConfigurableListableBeanFactory getBeanFactory();
/**
* Return the {@link Environment} for which the current application is running
* or {@code null} if no environment is available.
* @return the environment or {@code null}
*/
Environment getEnvironment();
/**
* Return the {@link ResourceLoader} currently being used or {@code null}
* if the resource loader cannot be obtained.
* @return a resource loader or {@code null}
*/
ResourceLoader getResourceLoader();
/**
* Return the {@link ClassLoader} that should be used to load additional
* classes or {@code null} if the default classloader should be used.
* @return the class loader or {@code null}
*/
ClassLoader getClassLoader();
}
AnnotatedTypeMetadata能够让我们检查带有@Bean注解的方法上还有什么其他注解,AnnotatedTypeMetadata也是一个接口:
public interface AnnotatedTypeMetadata {
/**
* Determine whether the underlying element has an annotation or meta-annotation
* of the given type defined.
* <p>If this method returns {@code true}, then
* {@link #getAnnotationAttributes} will return a non-null Map.
* @param annotationType the annotation type to look for
* @return whether a matching annotation is defined
*/
boolean isAnnotated(String annotationType);
/**
* Retrieve the attributes of the annotation of the given type, if any (i.e. if
* defined on the underlying element, as direct annotation or meta-annotation),
* also taking attribute overrides on composed annotations into account.
* @param annotationType the annotation type to look for
* @return a Map of attributes, with the attribute name as key (e.g. "value")
* and the defined attribute value as Map value. This return value will be
* {@code null} if no matching annotation is defined.
*/
Map<String, Object> getAnnotationAttributes(String annotationType);
/**
* Retrieve the attributes of the annotation of the given type, if any (i.e. if
* defined on the underlying element, as direct annotation or meta-annotation),
* also taking attribute overrides on composed annotations into account.
* @param annotationType the annotation type to look for
* @param classValuesAsString whether to convert class references to String
* class names for exposure as values in the returned Map, instead of Class
* references which might potentially have to be loaded first
* @return a Map of attributes, with the attribute name as key (e.g. "value")
* and the defined attribute value as Map value. This return value will be
* {@code null} if no matching annotation is defined.
*/
Map<String, Object> getAnnotationAttributes(String annotationType, boolean classValuesAsString);
/**
* Retrieve all attributes of all annotations of the given type, if any (i.e. if
* defined on the underlying element, as direct annotation or meta-annotation).
* Note that this variant does <i>not</i> take attribute overrides into account.
* @param annotationType the annotation type to look for
* @return a MultiMap of attributes, with the attribute name as key (e.g. "value")
* and a list of the defined attribute values as Map value. This return value will
* be {@code null} if no matching annotation is defined.
* @see #getAllAnnotationAttributes(String, boolean)
*/
MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationType);
/**
* Retrieve all attributes of all annotations of the given type, if any (i.e. if
* defined on the underlying element, as direct annotation or meta-annotation).
* Note that this variant does <i>not</i> take attribute overrides into account.
* @param annotationType the annotation type to look for
* @param classValuesAsString whether to convert class references to String
* @return a MultiMap of attributes, with the attribute name as key (e.g. "value")
* and a list of the defined attribute values as Map value. This return value will
* be {@code null} if no matching annotation is defined.
* @see #getAllAnnotationAttributes(String)
*/
MultiValueMap<String, Object> getAllAnnotationAttributes(String annotationType, boolean classValuesAsString);
}
三、处理自动装配的歧义性
加入我们使用@Autowired 注解标注了setDessert()方法:
@Autowired
public void setDessert(Dessert dessert)
{
this.dessert = dessert;
}
在本例中,Dessert是一个接口,并且有三个实现类,分别是Cake,Cookies和IceCream
@Component
public class Cake implements Dessert{ @Override
public void showDessert() {
// TODO Auto-generated method stub
}
}
@Component
public class Cookies implements Dessert{ @Override
public void showDessert() {
// TODO Auto-generated method stub }
}
@Component
public class IceCream implements Dessert{ @Override
public void showDessert() {
// TODO Auto-generated method stub }
}
因为这三个类使用了@Component注解,在组件扫描的时候,能够发现它们并将其创建为Spring应用上下文里的bean。然后spring在试图自动装配setDessert()中的Dessert参数时,它并没有唯一、无歧义的可选bean。spring会抛出NoUniqueBeanDefinitionException异常。
解决自动装配bean的歧义性问题有两种方法:
- 将可选bean中的某一个设置为首选的bean
- 使用限定符(qualifier)来帮助spring将可选bean的范围缩小到只有一个bean
标识首选的bean
使用@Primary注解来标识首选。@Primary注解可以与@Component组合用在组件扫描的bean上,也可以与@Bean注解组合用在java配置的bean声明中。
@Component
@Primary
public class IceCream implements Dessert{ @Override
public void showDessert() {
// TODO Auto-generated method stub }
}
xml中配置首选bean,可以使用<bean>元素的primary属性来指定。
使用限定符限定自动装配的bean
设置首选bean的局限在于@Primary注解无法将可选的方案的范围限定到唯一一个无歧义性的选项中。它只能标识一个优选的方案,当首选bean的数量超过一个时,没法进一步缩小可选范围。
@Qualifier是使用限定符的主要方式。可以与@Autowired协同使用。在注入的时候指定想要注入进去的是哪个bean。例如:
@Autowired
@Qualifier("iceCream")
public void setDessert(Dessert dessert)
{
this.dessert = dessert;
}
@Qualifier注解所设置的参数就是想要注入的bean的ID。这里的问题在于setDessert()方法上所指定的限定符与要注入的bean的名称是紧耦合的。
我们可以为bean设置自己的限定符,而不是依赖于bean的ID。如下:
@Component
@Qualifier("cold")
public class Cake implements Dessert{ @Override
public void showDessert() {
// TODO Auto-generated method stub
}
}
如此,则code限定符分配给了Cake bean。在注入的地方,只要引用cold限定符就可以了:
@Autowired
@Qualifier("cold")
public void setDessert(Dessert dessert)
{
this.dessert = dessert;
}
四、bean的作用域
spring定义了多种作用域,可以基于这些作用域创建bean,包括:
- 单例(Singleton):在整个应用中,只创建一个实例
- 原型(Prototype):每次注入或者通过spring应用上下文获取的时候,都会创建一个新的bean实例
- 回话(Session):在web应用中,为每个会话创建一个bean实例
- 请求(Request):在web应用中,为每个请求创建一个bean实例
单例是默认的作用域,如果选择其他作用域,要使用@Scope注解,它可以域@Component或@Bean一起使用。
@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class IceCream implements Dessert{ @Override
public void showDessert() {
// TODO Auto-generated method stub }
}
同样,如果使用xml来配置的话,可以使用<bean>元素的scope属性来设置作用域。
会话和请求作用域需要注意作用域代理的问题。
五、运行时注入---spring表达式语言
spring一直支持将属性定义到外部的属性文件中,并使用占位符值将其插入到Spring bean中。在Spring装配中,占位符的形式为使用“${...}”包装的属性名称。
<bean id="iEngine" class="springdemo.test1.entity.impl.AudiEngine"
c:_title="${engine.title}" c:_outValue="${engine.outValue}"/>
为了使用属性占位符,我们必须要配置一个PropertyPlaceholderConfigurer bean 或者PropertySourcesPlaceholderConfigurer bean 。因为他能够基于spring Environment 及属性源来解析占位符。
<context:property-placeholder location=""/>
spring表达式-----SpEL
首先SpEL表达式要放到“#{...}”之中。
示例:
#{T(System).currentTimeMillis()} ---计算表达式的那一刻当前的时间的毫秒数。
#{userinfo.userName}---应用其他bean或其他bean的属性。
SpEL拥有很多特性,包括:
- 使用bean的id来引用bean;
- 调用方法和访问对象的属性;
- 对值进行算术、关系和逻辑运算;
- 正则表达式匹配
- 集合操作。
此篇中不在详述SpEL,具体使用细节可查询spring相关文档或书籍。
Spring高级装配bean的更多相关文章
- (三)Spring 高级装配 bean的作用域@Scope
1.默认情况下,spring通过@Autowared注入的bean是单例的bean,但有些情况是不满足的,例如:购物车,每个会话,或每个用户登录使用的购物车都是独立的 spring的定义的作用域: a ...
- Spring高级装配
Spring高级装配 目录 一.Profile(根据开发环境创建对应的bean) 二.条件化的创建bean(根据条件创建bean) 三.处理自动装配歧义性(指定首选bean.限定符限制bean) 四. ...
- Spring高级装配(一) profile
Spring高级装配要学习的内容包括: Spring profile 条件化的bean声明 自动装配与歧义性 bean的作用域 Spring表达式语言 以上属于高级一点的bean装配技术,如果你没有啥 ...
- Spring 自动装配 Bean
Spring3系列8- Spring 自动装配 Bean 1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiri ...
- Spring自动装配Bean详解
1. Auto-Wiring ‘no’ 2. Auto-Wiring ‘byName’ 3. Auto-Wiring ‘byType 4. Auto-Wirin ...
- Spring实战(四)Spring高级装配中的bean profile
profile的原意为轮廓.剖面等,软件开发中可以译为“配置”. 在3.1版本中,Spring引入了bean profile的功能.要使用profile,首先要将所有不同的bean定义整理到一个或多个 ...
- [spring]03_装配Bean
3.1 JavaBean 3.1.1 JavaBean 是什么 JavaBean 是一种JAVA语言写成的可重用组件. 为写成JavaBean,类必须是具体的和公共的,并且具有无参数的构造器. Jav ...
- spring IOC装配Bean(注解方式)
1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...
- Spring自动化装配bean
1. 场景 用CD(Compact disc)和CD播放器(CDPlayer)阐述DI(依赖注入). 如果不将CD插入(注入)到CDPlayer中,那么CDPlayer其实没有太大的用处,所以,可以这 ...
随机推荐
- RAID磁盘阵列及CentOS7系统启动流程
磁盘阵列(Redundant Arrays of Independent Disks,RAID),有“独立磁盘构成的具有冗余能力的阵列”之意,,数据读取无影响.将数据切割成许多区段,分别存放在各个硬盘 ...
- SOUI开发应用展示2
神速加速器 一款网游加速器,下载:http://js.ispeed.cn/downloadPage.html 一款上位机界面,行业软件,不提供下载 : 光速搜索:自己搜索官网,有广告. ABC看图: ...
- python第七天(字符编码,字符与字节,文件操作)
一.字符编码: 定义:将人识别的字符转换成计算机能识别的0和1,转换的规则就是字符编码表. 常见编码表:ascii.unicode.GBK 编码表: 1.采用的都是unicode编码表 2.unico ...
- 相机标定问题-Matlab & Py-Opencv
一.相机标定基本理论 1.相机成像系统介绍 图中总共有4个坐标系: 图像坐标系:Op 坐标表示方法(u,v) Unit:Dots(个) 成像坐标系:Oi ...
- vue 事件修饰符
1.用.stop来阻止冒泡(点击click按钮之后,先执行clickBtn2方法,然后执行clickBtn1方法,.stop命令阻止了clickBtn1方法的执行) 2.使用.prevent命令来阻止 ...
- yii2 httpClient的用法
yii2 httpClient的用法示例: <?php /* * @Purpose : yii2 httpClient 请求示例 * @Author : Chrdai * @Time : 201 ...
- SVN 问题:None of the environment variables SVN_EDITOR, VISUAL or EDITOR are set, and no 'editor-cmd' run-time
问题原因: 没有设置svn编辑器的环境变量,主要是import.commit中填写comment要用 解决办法: 编辑 /etc/bashrc 文件,加入如下一行: export SVN_EDITOR ...
- 怎么修改kodexplorer网盘下的版权
前言: 要说kodexplorer,可是个好东西,在线web管理服务器文件,着实是网站管理员的好助手.内置的adminer管理数据库,用起来也是很顺手. 这么好的工具,还是免费的.但就是页面底部有ko ...
- [AtCoder 2702]Fountain Walk - LIS
Problem Statement In the city of Nevermore, there are 108 streets and 108 avenues, both numbered fro ...
- Java_流程控制
介绍: java的流程控制结构有三种:顺序.选择.循环 顺序结构,就是从头到尾依次执行每条语句的操作. 选择结构,也称条件控制,是指根据表达式的值有选择的执行. 循环结构,也称回 ...