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:没有使用唯一约束,理论上可以重复,实际上开发不行,里面可以出现 ...
随机推荐
- Ubuntu server 安装的mysql数据库忘记密码的解决方法
客户端连接时报错MySQL数据库出现:Error 1045错误时,就表明输入的用户名或密码错误被拒绝访问了. 解决办法可以分为以下几步: 1.修改mysql配置文件,使得可以无密码登录mysql su ...
- servlet类与Spring Controller类的关系
以前的java web项目,需要在web.xml中定义servlet,对应不同的请求,而在spring项目中,我们用controller定义了各种各样的servlet(当然不包括DispatcherS ...
- shell进阶教程
背景:就自己常用的shell脚本写作风格,总结了一些知识点.也是作为交接工作的一部分文档.部分内容单独写 #!/bin/sh # shell脚本进阶教程 # 1.常用知识点:变量设置/日期设置/格式化 ...
- 20145302张薇 《Java程序设计》第二周学习总结
20145302张薇 <Java程序设计>第一周学习总结 教材学习内容总结 第三章 第三章讲的是基本类型,变量,运算符和基本条件语句. 基本类型分为: 整数:short(2 byte),i ...
- linux启动过程⭐
启动第一步--加载BIOS当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬盘 ...
- Spring笔记1——Spring起源及其核心技术
Spring的作用 当我们使用一种技术时,需要思考为什么要使用这门技术.而我们为什么要使用Spring呢?从表面上面SSH这三大框架中,Struts是负责MVC责任的分离,并且提供为Web层提供诸如控 ...
- iOS开发进阶 - 项目的本地化处理(多语言开发)
移动端访问不佳,请访问我的个人博客 最近项目本地化,需要支持多国语言,下面将本地化的步骤记录下来,方便查找使用,步骤很简单,有些地方也有坑,希望大家看后少走弯路~~ 什么是本地化 本地化说直白点就是多 ...
- git 总结命令
git 命令 创建git版本库:git init 查看状态:git status 把文件添加到暂存区:git add 把文件提交到版本库:git commit -m "提交说明" ...
- vue集成ueditor
相关代码见github 1.引入ueditor相关的文件,具体目录见下图如下 我将下载的文件放在static下面,这里专门用来放置相关的静态文件 (在ueditor.config.js需要配置一下路径 ...
- 【转】chrome 67版本后无法拖拽离线安装CRX格式插件的解决方法
第一种:开启开发者模式即可 (推荐) chrome 的设置 -> 更多工具 -> 扩展程序,开启开发者模式即可! 第二种方法:修改参数 首先打开下面地址:chrome://flags/# ...