SpringInAction-- 配置Profile Bean
Profile Bean 使用场景描述:
在开发软件的时候,在数据库方面,往往不是一个库就能解决的,一般分为开发库、测试库、生产库,在这些库设置链接的时候,也会配置其对应的数据。
现有一种方式,就是单独的配置类,或者在xml中配置bean,然后在构建的时候确定哪一个配置编译到部署的应用。这种方式是可行的,但是也是有问题存在的,即当从开发阶段迁移到QA阶段的时候,重新部署是没有问题的,但是从QA阶段迁移到生产阶段的时候,重建有可能会引入bug并且会使得QA团队成员中带来不安的情绪。
这个时候我们就可以引入profile了
profile bean的好处
Spring提供的profile bean 配置,其原理是根上面的解决方式没啥区别,两种方法的区别就是在于spirng在运行的时候,会根据环境决定哪些bean应该被创建,哪些不被创建。因为不是在部署前去判断的,而是等运行的时候来判断的,这样一来,一个部署单元(可能是war包)就能够适合所有的环境,这样一来就没有必要重构了。
接下里就让我们从例子中来领会它的含义
假设我们有三种坐骑,当我们在陆地的时候可以选择 跳跳蛙、在天空的时候选择小黑龙、在海洋中的时候选择 皮皮虾。。
这样一来我们先创造一个 宠物坐骑接口类 PetMounts 然后分别创造自己的坐骑。 FlyingMounts 、LandMounts 、SeaMounts
PeTMounts
package com.bean.profile; /**
* Created by IntelliJ IDEA.
* Author XueYuan
* Data 2017/02/22
* Time 16:46
*/ public interface PetMounts { void letsGo();
}
FlyingMounts
package com.bean.profile; /**
* Created by IntelliJ IDEA.
* Author XueYuan
* Data 2017/02/22
* Time 16:52
*/ public class FlyingMounts implements PetMounts { public void letsGo() {
System.out.println("我是飞行坐骑 小黑龙,我们走……!");
}
}
LandMounts
package com.bean.profile; /**
* Created by IntelliJ IDEA.
* Author XueYuan
* Data 2017/02/22
* Time 16:53
*/ public class LandMounts implements PetMounts {
public void letsGo() { System.out.println("我是陆地坐骑 跳跳蛙,我们走……!");
}
}
SeaMounts
package com.bean.profile; import org.springframework.stereotype.Component; /**
* Created by IntelliJ IDEA.
* Author XueYuan
* Data 2017/02/22
* Time 16:54
*/ @Component
public class SeaMounts implements PetMounts {
public void letsGo() { System.out.println("我是海洋坐骑 皮皮虾,我们走……!");
}
}
好了坐骑我们创造好了,下面就开始根据环境来选择什么坐骑。
首先先来看 java文件的配置方法
package com.bean.profile; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile; /**
* Created by IntelliJ IDEA.
* Author XueYuan
* Data 2017/02/22
* Time 16:44
*/ @Configuration
public class PetMountsConfig { @Bean
@Profile("sky")
public PetMounts sky() {
return new FlyingMounts();
} @Bean
@Profile("land")
public PetMounts land() {
return new LandMounts();
} @Bean
@Profile("sea")
public PetMounts sea() {
return new SeaMounts();
} }
我们要分别创建Bean 然后在对应的方法上面添加profile
小贴士:在Spring3.1之前只能在类级别上面添加@Profile注解;从Spring3.2后开始就支持在方法上面添加注释
xml中配置Profile
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <beans profile="sky">
<bean id="flyingMounts" class="com.bean.profile.FlyingMounts"/>
</beans> <beans profile="land">
<bean id="landMounts" class="com.bean.profile.LandMounts"/>
</beans> <beans profile="sea">
<bean id="seaMounts" class="com.bean.profile.SeaMounts"/>
</beans> </beans>
在xml中配置,主要是利<beans>中的Profile 属性
好了配置也配置好了,那么下面要怎么用,怎么激活呢?
Spring在确定哪个profile处于激活状态时,需要依赖两个独立的属性:spring.profiles.active和spring.profiles.default。如果设置了spring.profiles.active属性的话,那么它的值就会用来确定哪个profile是激活的。但如果没有设置spring.profiles.active属性的话,那Spring将会查找spring.profiles.default的值。如果spring.profiles.active和spring.profiles.default均没有设置的话,那就没有激活的profile,因此只会创建那些没有定义在profile中的bean。
有多种方式来设置这两个属性:
- 作为DispatcherServlet的初始化参数;
- 作为Web应用的上下文参数;
- 作为JNDI条目;
- 作为环境变量;
- 作为JVM的系统属性;
- 在集成测试类上,使用@ActiveProfiles注解设置。
下面就是在web中配置 profile (偷懒了下 直接书上的图片)

在测试中使用Profile的时候 方法如下:
@RunWith(SpringJUnit4ClassRunner.class)
/*@ContextConfiguration(classes = PetMountsConfig.class)*/
@ContextConfiguration(value = "config.xml")
@ActiveProfiles("sea")
public class LetsGo { ……
}
激活方法我们也知道了,现在就让我们选择一个坐骑,去把妹吧!
package com.bean.profile; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /**
* Created by IntelliJ IDEA.
* Author XueYuan
* Data 2017/02/22
* Time 20:42
*/
@RunWith(SpringJUnit4ClassRunner.class)
/*@ContextConfiguration(classes = PetMountsConfig.class)*/
@ContextConfiguration(value = "config.xml")
@ActiveProfiles("sea")
public class LetsGo { @Autowired
PetMounts petMounts; @Test
public void LetsGoLog(){
petMounts.letsGo();
}
}
以上就是peofile bean简单小例子,如有错误,请指出,谢谢~
代码:https://github.com/eoooxy/springinaction test下 的com.bean.profile中
SpringInAction-- 配置Profile Bean的更多相关文章
- 第3章—高级装配—配置profile bean
配置profile bean 3.1.@profile注解是spring提供的一个用来标明当前运行环境的注解. 我们正常开发的过程中经常遇到的问题是,开发环境是一套环境,qa测试是一套环境,线上部署又 ...
- Spring Boot实战笔记(三)-- Spring常用配置(Bean的初始化和销毁、Profile)
一.Bean的初始化和销毁 在我们的实际开发的时候,经常会遇到Bean在使用之前或之后做些必要的操作,Spring对Bean的生命周期操作提供了支持.在使用Java配置和注解配置下提供如下两种方式: ...
- spring中bean配置和bean注入
1 bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean ...
- 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法
Java提供了Class类,可以通过编程方式获取类别的字段和方法,包括构造方法 获取Class类实例的方法: 类名.class 实例名.getClass() Class.forNam ...
- Spring 通过XML配置装配Bean
使用XML装配Bean需要定义对于的XML,需要引入对应的XML模式(XSD)文件,这些文件会定义配置Spring Bean的一些元素,简单的配置如下: <?xml version=" ...
- Spring 自定义配置类bean
<!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.bea ...
- (D)spring boot使用注解类代替xml配置实例化bean
bean经常需要被实例化,最常见的就是new一个呗,Bean bean = new Bean(),方便好用还快捷. 然而在我们刚开始学习写i项目的时候却发现,new不好用哦,并且也不报错,根本不知道怎 ...
- Spring 常用配置、Bean
spring模块 Spring-Core: Core包是框架的最基础部分,并提供依赖注入(Dependency Injection)管理Bean容器功能. Spring-Context:(Spring ...
- 五 Spring的配置:Bean的配置,生命周期和作用范围
Bean相关的配置: <bean>标签的id和name的配置: id:使用了约束中的唯一约束,里面不能出现特殊字符 name:没有使用唯一约束,理论上可以重复,实际上开发不行,里面可以出现 ...
随机推荐
- RocEDU.阅读.写作《苏菲的世界》书摘(四)
亚理斯多德认为,快乐有三种形式.一种是过着享乐的生活,一种是做一个自由而负责的公民,另一种则是做一个思想家与哲学家.接着,他强调,人要同时达到这三个标准才能找到幸福与满足. 亚理斯多德提倡所谓的&qu ...
- c#结构体和字节流之间的相互转换
结构体转byte数组 1 首先要明白 ,是 在那个命名空间下 System.Runtime.InteropServices; 2 首先得到结构体的大小 2 开辟相应的内存空间 3 将结构体填 ...
- 解读:MultipleOutputs类
//MultipleOutputs类用于简化多文件输出The MultipleOutputs class simplifies writing output data to multiple outp ...
- 如何使一个openwrt下的软件开机自启动
条件有三: 1.需要在软件包的Makefile中添加宏定义Package/$(package-name)/preinst和Package/$(package-name)/prerm define Pa ...
- 何为K-邻近算法
答:K-邻近算法,英文为K-nearest neighbor(KNN),就是计算要测试对象与k个样本对象之间的距离,通过距离的大小来对测试对象进行分类
- MQ的前世今生
1983年孟买26岁的工程师Vivek Ranadive设想一种软件总线,同年Teknekron诞生了. 最初用于高盛,用于解决金融交易.于是发布订阅的MQ The Information B ...
- Codeforces Round #358 (Div. 2) C. Alyona and the Tree dfs
C. Alyona and the Tree time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 探索C++虚函数
探索C++虚函数 1 测试环境 各个编译器对虚函数的实现有各自区别,但原理大致相同.本文基于VS2008探索虚函数 2 测试代码 #pragma once #include <iostream& ...
- vs 2017 保存文件 utf8
vs 2017 保存文件 utf8 转自:https://blog.csdn.net/jiegemena/article/details/79369650
- 重新学习MySQL数据库7:详解MyIsam与InnoDB引擎的锁实现
重新学习Mysql数据库7:详解MyIsam与InnoDB引擎的锁实现 说到锁机制之前,先来看看Mysql的存储引擎,毕竟不同的引擎的锁机制也随着不同. 三类常见引擎: MyIsam :不支持事务,不 ...