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中

原文地址:https://www.cnblogs.com/eoooxy/p/6431195.html