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的更多相关文章

  1. 第3章—高级装配—配置profile bean

    配置profile bean 3.1.@profile注解是spring提供的一个用来标明当前运行环境的注解. 我们正常开发的过程中经常遇到的问题是,开发环境是一套环境,qa测试是一套环境,线上部署又 ...

  2. Spring Boot实战笔记(三)-- Spring常用配置(Bean的初始化和销毁、Profile)

    一.Bean的初始化和销毁 在我们的实际开发的时候,经常会遇到Bean在使用之前或之后做些必要的操作,Spring对Bean的生命周期操作提供了支持.在使用Java配置和注解配置下提供如下两种方式: ...

  3. spring中bean配置和bean注入

    1 bean与spring容器的关系 Bean配置信息定义了Bean的实现及依赖关系,Spring容器根据各种形式的Bean配置信息在容器内部建立Bean定义注册表,然后根据注册表加载.实例化Bean ...

  4. 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法

    Java提供了Class类,可以通过编程方式获取类别的字段和方法,包括构造方法    获取Class类实例的方法:   类名.class   实例名.getClass()   Class.forNam ...

  5. Spring 通过XML配置装配Bean

    使用XML装配Bean需要定义对于的XML,需要引入对应的XML模式(XSD)文件,这些文件会定义配置Spring Bean的一些元素,简单的配置如下: <?xml version=" ...

  6. Spring 自定义配置类bean

    <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.bea ...

  7. (D)spring boot使用注解类代替xml配置实例化bean

    bean经常需要被实例化,最常见的就是new一个呗,Bean bean = new Bean(),方便好用还快捷. 然而在我们刚开始学习写i项目的时候却发现,new不好用哦,并且也不报错,根本不知道怎 ...

  8. Spring 常用配置、Bean

    spring模块 Spring-Core: Core包是框架的最基础部分,并提供依赖注入(Dependency Injection)管理Bean容器功能. Spring-Context:(Spring ...

  9. 五 Spring的配置:Bean的配置,生命周期和作用范围

    Bean相关的配置: <bean>标签的id和name的配置: id:使用了约束中的唯一约束,里面不能出现特殊字符 name:没有使用唯一约束,理论上可以重复,实际上开发不行,里面可以出现 ...

随机推荐

  1. oracle中job定时器任务

    对于DBA来说,经常要数据库定时的自动执行一些脚本,或做数据库备份,或做数据的提炼,或做数据库的性能优化,包括重建索引等等的工作.但是,Oracle定时器Job时间的处理上,千变万化,今天我把比较常用 ...

  2. Python面试题目之(针对dict或者set数据类型)边遍历 边修改 报错dictionary changed size during iteration

    # result 是一个字典, 把里面属性值是None的属性删除 for key in result: if not result[key]: del result[key] continue 但是报 ...

  3. poj-2259 team queue(数据结构)

    第一遍看的时候立即想到了哈希表. 再想时觉得两个队列,一个用来排队伍之间的顺序,一个用来排队伍内部成员的顺序即足够了. DEQUE的时候先判断哪只队伍排在队首,之后再让该队伍中的首队员出列. 整体没有 ...

  4. windows批量删除ip

    cmd下输入如下命令第一步:netsh -c int ip dump >c:\ip.txt在C盘根目录看到一个ip.txt的文件,内容为当前网卡的设置信息,为了能更直观的看清楚IP的设置信息. ...

  5. Nodejs V8引擎 fast property lookup

    前言 之所以会研究这个东西,是我在网上找了一下各个语言的执行效率比较.好吧,我承认这是个无聊的东西,不过看看总是无妨,然而我惊讶的发现,有些测试声称Java,C,Nodejs是处在同一个效率级别参见链 ...

  6. 02_Storm集群部署

    1. 部署前的硬件及软件检查 硬件要求 1)storm集群部署包括zookeeper部署,而zookeeper集群最小为3台机器2)storm的计算过程都在内存中完成,因此内存要尽量大3)storm少 ...

  7. 外国人专门写了一篇文章,来分析为什么go在中国如此火

    外国人专门写了一篇文章,来分析为什么go在中国如此火: <Why is Golang popular in China?> http://herman.asia/why-is-go-pop ...

  8. 关于javascript以及jquery如何打开文件

    其实很简单, <input type="file" id="file" mce_style="display:none"> 这个 ...

  9. Testng中注释简介

    1. @Before和@After 注释 这两个就比较多,一般用于在测试构件上.关于测试构件以后详细介绍,测试构件一般有测试之前的操作和测试运行之后的清除数据的操作. Before分类有@Before ...

  10. iQuery移动端手势事件插件-jGestures

    jGestures下载 jGestures事件简介 orientationchange 代表设备顺时针或者逆时针旋转.此事件可以被设备触发,可能使用的是重力传感器. pinch 缩放手势(两个手指在屏 ...