SpringBoot之开启Profile
来源:互联网 发布:python程序员工资 编辑:程序博客网 时间:2024/05/17 00:07
我们在做项目开发的时候,生产环境和测试环境的一些配置可能会不一样,有时候一些功能也可能会不一样,所以我们可能会在上线的时候手工修改这些配置信息。但是Spring中为我们提供了Profile这个功能。我们只需要在启动的时候添加一个虚拟机参数,激活自己环境所要用的Profile就可以了。下面举个例子来说明一下:
首先我们先创建一个bean,用来测试是不是从不同的配置文件中取的值。代码如下(省略getter/setter):
@ConfigurationProperties("profile.test")@Componentpublic class ProfileDomain implements Serializable { private static final long serialVersionUID = 2092752356451204202L; /** * 用户名 */ private String userName; /** * 密码 */ private String passWord;}接着我们在resources下面建立两个properties文件。
application-dev.properties用来存放的时候开发环境的配置信息,application-prod.properties用来存放的是生产环境的配置信息。内容分别如下:
application-dev.properties:
profile.test.userName=\u6211\u662f\u5f00\u53d1\u73af\u5883profile.test.passWord=this is development environmentapplication-prod.properties:
profile.test.userName=\u6211\u662f\u751f\u4ea7\u73af\u5883profile.test.passWord=this is production environment我们写个Controller来测试一下:
@RestControllerpublic class ProfileController { @Autowired private ProfileDomain profileDomain; @RequestMapping("testProfile") public ProfileDomain testProfile() { return profileDomain; }}我们用命令行启动一下(注意我们这里加了个参数 --spring.profiles.active=dev):
java -jar LearnSpringBoot-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev下面我们访问一下:http://localhost:8003/testProfile,输出结果如下:
从输出结果中我们可以看出,输出的内容是application-dev.properties中的配置信息。下面我们用另外一个命令参数启动一下:
java -jar LearnSpringBoot-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod我们还是用上面的请求访问一下:
http://localhost:8003/testProfile,输出结果如下:
从上面的输出结果中我们可以看到我们在启动SpringBoot的时候所启用的profile,它就会从不同的配置文件中加载配置项。另外还可以增加虚拟机参数:-Dspring.profiles.active=prod的方式来激活不同的profile。也可以在SpringBoot默认的或者指定的配置文件中增加profile的激活项。如:spring.profiles.active=dev。
有时候我们可能还会有这样的需求,生产环境或者开发环境我们所看到的功能是不同的,也需要我们根据配置项来激活不同的功能。
首先我们先定义一个Service的接口:
public interface ProfileService { String getProfileDomain();}如果spring.profiles.active的值是prod的时,它的实现类如下:
@Profile(value = "prod")@Servicepublic class ProfileProdServiceImpl implements ProfileService { public ProfileProdServiceImpl() { System.out.println("我是生产环境。。。。。"); } @Override public String getProfileDomain() { StringBuilder sb = new StringBuilder(); sb.append("我在生产环境,").append("我可以吃鸡鸭鱼牛羊肉。。。。"); return sb.toString(); }}如果spring.profiles.active的值是dev的时,它的实现类如下:
@Profile(value = "dev")@Servicepublic class ProfileDevServiceImpl implements ProfileService { public ProfileDevServiceImpl() { System.out.println("我是开发环境。。。。。"); } @Override public String getProfileDomain() { StringBuilder sb = new StringBuilder(); sb.append("我在开发环境,").append("我只能吃加班餐:大米饭。。。。"); return sb.toString(); }}下面我们来测试一下,在刚才的Controller类中注入ProfileService,代码如下:
@RestControllerpublic class ProfileController { @Autowired private ProfileDomain profileDomain; @Autowired private ProfileService profileService; @RequestMapping("testProfile") public ProfileDomain testProfile() { return profileDomain; } @RequestMapping("testProfile2") public String testProfile2() { return profileService.getProfileDomain(); }}当我们的spring.profiles.active为prod的时候:请求如下:http://localhost:8003/testProfile2,输出如下:
当我们的spring.profiles.active为dev的时候:请求如下:http://localhost:8003/testProfile2,输出如下:
我们在根据环境进行功能切换的类上加上@Profile注解,这样就可以根据所激活的不同的环境,注入不同的实现类。
这里需要注意一下:如果用了@Profile这个注解,在配置项中找不到@Profile中的值的话,在系统启动的时候是会报错的。
- SpringBoot之开启Profile
- SpringBoot--Profile
- SpringBoot学习笔记之profile使用
- springboot开启热部署之Idea&Gradle
- SpringBoot构建微服务实战 之 Profile决策
- springboot 多profile配置
- SpringBoot之开启数据库迁移的FlyWay使用
- Springboot学习记录4------profile
- springboot开启druid监控
- springboot开启定时任务教程
- SpringBoot集成MyBatis开启事务
- SpringBoot集成MyBatis开启事务
- SpringBoot集成MyBatis开启事务
- springboot开启声明式事务
- Gradle中给SpringBoot指定active profile
- springboot 读取maven profile 变量 通配符
- 19-SpringBoot——核心-Profile配置
- mysql开启慢查询日志和profile
- mac下XAMPP安装php redis扩展
- 8核、6核、4核、双核CPU是什么意思
- HTML基础学习四
- swagger注解 详细说明
- hdu 1027 Ignatius and the Princess II (dfs+剪枝)?
- SpringBoot之开启Profile
- python里怎么实现多个协程一起执行,只要完成一个就返回一个协程
- (Basic algorithm学习笔记)《基础算法四》- 深度优先搜索(Depth First Search,DFS )
- ES6变量的解构赋值
- 最小费用最大流--uva1658 Admiral
- 禁用,绕过代理
- linux下使用 du查看某个文件或目录占用磁盘空间的大小
- solr包结构介绍,solrCore的安装配置,solr部署到Tomcat,多solrCore配置,配置中文分词器,界面功能介绍,Schema.xml的基本使用,数据导入
- 查看已安装Tomcat的版本号