首页 技术 正文
技术 2022年11月18日
0 收藏 999 点赞 5,082 浏览 2485 个字

【Java Web开发学习】Spring 环境profile

转载:http://www.cnblogs.com/yangchongxing/p/8890702.html

开发、测试、生产环境往往是不同的,我们需要将应用从一个环境迁移到另外一个环境,这时候就牵扯到不同环境配置是不同的。

Spring提供了@Profile注解来指定bean属于哪一个profile

1、配置@Profile注解,该注解可用于类也可用于方法

用于类上只有当该profile是激活状态时,这个类的bean才能被创建

package cn.ycx.web.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;import cn.ycx.web.model.Painter;
import cn.ycx.web.model.Player;@Configuration
@Profile("dev")
public class TestConfig {
@Bean
public Player player() {
return new Player();
}
@Bean
public Painter painter() {
return new Painter();
}
}

用于方法上只有当该profile是激活状态时,标注@Bean的方法才能被创建bean

package cn.ycx.web.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;import cn.ycx.web.model.Painter;
import cn.ycx.web.model.Player;
@Configuration
public class TestConfig {
@Bean
@Profile("prod")
public Player player() {
return new Player();
}
@Bean
@Profile("dev")
public Painter painter() {
return new Painter();
}
}

2、激活profile

Spring根据两个独立的属性来确定激活那个profile。spring.profiles.active和spring.profiles.default。spring.profiles.active比spring.profiles.default优先级高,当设置了spring.profiles.active属性值那么就用它的值来确定激活那个profile;只有当spring.profiles.active没有设置的时候才会去查找spring.profiles.default属性值并用它的值来确定激活那个profile。当两个值都没设置时就没有激活的profile。

spring.profiles.active和spring.profiles.default可以为复数,多个值用逗号隔开。例如:spring.profiles.active=dev,play

有多种方式来设置这两个值:

1.作为DispatcherServlet的初始化参数

2.作为Web应用的上下文参数

3.作为JNDI条目

4.作为环境变量

5.作为JVM的系统属性

6.在集成测试环境上使用@ActiveProfiles注解

对于web应用个人喜欢在DispatcherServlet初始化参数和应用上下文参数指定spring.profiles.default值,通过环境变量指定spring.profiles.active值,这样就能在需要改变的地方轻松改变

  <context-param>
<param-name>spring.profiles.default</param-name>
<param-value>prod</param-value>
</context-param>
<servlet>
<servlet-name>ds</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 也可通过classpath指定路径 -->
<param-value>\WEB-INF\ds-servlet.xml</param-value>
</init-param>
<init-param>
<param-name>spring.profiles.default</param-name>
<param-value>prod</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

默认激活prod,通过下面的方式激活dev。

选用环境变量

windows系统

注意:我的机器重启系统之后才生效了

spring.profiles.active=dev

ubuntu系统

当前用户

$ vim ~/.bashrc
追加 export spring.profiles.active=dev
$ source ~/.bashrc

所有用户

# vim /etc/profile
追加 export spring.profiles.active=dev
# source /etc/profile

标注@Profile(“dev”)的Bean就会创建

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,076
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,552
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,400
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,176
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,812
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,894