1. 简述

Spring profile用例,分3个场景(Test, Dev, Prod)相对Spring 环境与profile(一)——超简用例多了根据具体的profile获取对应的Properties

2. 代码结构

3. 各模块介绍

接口

GenericEnv

public interface GenericEnv {

}

#com.env模块

DevEnv.java

@Component
public class DevEnv implements GenericEnv { private String envName = "dev"; @Value("${profile.name}")
private String profileName; public String getEnvName() {
return envName;
} public void setEnvName(String envName) {
this.envName = envName;
} public String getProfileName() {
return profileName;
} public void setProfileName(String profileName) {
this.profileName = profileName;
} @Override
public String toString() {
return "DevEnv [envName=" + envName + ", profileName=" + profileName
+ "]";
}
}

ProdEnv

@Component
public class ProdEnv implements GenericEnv { private String envName = "prod"; @Value("${profile.name}")
private String profileName; public String getEnvName() {
return envName;
} public void setEnvName(String envName) {
this.envName = envName;
} public String getProfileName() {
return profileName;
} public void setProfileName(String profileName) {
this.profileName = profileName;
} @Override
public String toString() {
return "ProdEnv [envName=" + envName + ", profileName=" + profileName
+ "]";
}
}

TestEnv.java

@Component
public class TestEnv implements GenericEnv { private String envName = "test"; @Value("${profile.name}")
private String profileName; public String getEnvName() {
return envName;
} public void setEnvName(String envName) {
this.envName = envName;
} public String getProfileName() {
return profileName;
} public void setProfileName(String profileName) {
this.profileName = profileName;
} @Override
public String toString() {
return "TestEnv [envName=" + envName + ", profileName=" + profileName
+ "]";
}
}

#resources.properties文件

application-default.properties

# Application Common Properties
profile.name=spring.profile

application-dev.properties

profile.name=dev.profiles

# Database Properties
db.driverClass=com.mysql.jdbc.Driver
db.connectionURL=jdbc:mysql://localhost:3306/emp
db.username=dev_usr
db.password=dev_pss # JMS Properties
jms.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory
jms.provider.url=tcp://localhost:61616
jms.queue=dev.queue

application-prod.properties

profile.name=prod.profiles

# Database Properties
db.driverClass=com.mysql.jdbc.Driver
db.connectionURL=jdbc:mysql://192.168.1.1:3306/emp
db.username=prod_usr
db.password=prod_pss # JMS Properties
jms.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory
jms.provider.url=tcp://192.168.1.1:61616
jms.queue=prod.queue

application-test.properties

profile.name=test.profiles

# Database Properties
db.driverClass=com.mysql.jdbc.Driver
db.connectionURL=jdbc:mysql://192.168.1.2:3306/emp
db.username=test_usr
db.password=test_pss # JMS Properties
jms.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory
jms.provider.url=tcp://192.168.1.2:61616
jms.queue=test.queue

#需解析properties的2个类

DatabaseProperties.java

package com.jcg.prop;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class DatabaseProperties { @Value("${db.driverClass}")
private String driverClass; @Value("${db.connectionURL}")
private String connectionURL; @Value("${db.username}")
private String username; @Value("${db.password}")
private String password; public String getDriverClass() {
return driverClass;
} public String getConnectionURL() {
return connectionURL;
} public String getUsername() {
return username;
} public String getPassword() {
return password;
} @Override
public String toString() {
return "DatabaseProperties [driverClass=" + driverClass
+ ", connectionURL=" + connectionURL + ", username=" + username
+ ", password=" + password + "]";
}
}

JmsProperties

package com.jcg.prop;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class JmsProperties {
/*
JMS即Java消息服务(Java Message Service)应用程序接口
*/ @Value("${jms.factory.initial}")
private String factoryInitial; @Value("${jms.provider.url}")
private String providerUrl; @Value("${jms.queue}")
private String queue; public String getFactoryInitial() {
return factoryInitial;
} public String getProviderUrl() {
return providerUrl;
} public String getQueue() {
return queue;
} @Override
public String toString() {
return "JmsProperties [factoryInitial=" + factoryInitial
+ ", providerUrl=" + providerUrl + ", queue=" + queue + "]";
} }

#resources.spring模块

xml-config-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- scans for annotated classes in the com.company package -->
<context:component-scan base-package="com.jcg" /> <!-- enables annotation based configuration -->
<context:annotation-config /> <beans profile="dev">
<!-- allows for ${} replacement in the spring xml configuration from the
application-default.properties, application-dev files on the classpath -->
<context:property-placeholder
location="classpath:properties/application-default.properties, classpath:properties/application-dev.properties"
ignore-unresolvable="true" /> <!-- scans for annotated classes in the com.env.dev package -->
<context:component-scan base-package="com.env.dev" />
</beans> <beans profile="test">
<context:property-placeholder
location="classpath:properties/application-default.properties, classpath:properties/application-test.properties"
ignore-unresolvable="true" /> <context:component-scan base-package="com.env.test" />
</beans> <beans profile="prod">
<context:property-placeholder
location="classpath:properties/application-default.properties, classpath:properties/application-prod.properties"
ignore-unresolvable="true" /> <context:component-scan base-package="com.env.prod" />
</beans> </beans>

4. 测试

package com.jcg.test;

import junit.framework.TestCase;

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; import com.jcg.prop.DatabaseProperties;
import com.jcg.prop.GenericEnv;
import com.jcg.prop.JmsProperties; @RunWith(SpringJUnit4ClassRunner.class)
//Change it to your desired profile
@ActiveProfiles(profiles = "dev")
@ContextConfiguration("classpath:spring/xml-config-context.xml")
public class SpringPropertiesTest extends TestCase { @Autowired
private GenericEnv env; @Autowired
private DatabaseProperties dbProp; @Autowired
private JmsProperties jmsProp; @Test
public void testAppProperties() { System.out.println("Running DatabasePropertiesTest ..."); System.out.println("Environment : " + env.toString()); System.out.println("Database Properties: " + dbProp.toString()); System.out.println("JMS Properties : " + jmsProp.toString());
} }

输出

Running DatabasePropertiesTest ...
Environment : DevEnv [envName=dev, profileName=dev.profiles]
Database Properties: DatabaseProperties [driverClass=com.mysql.jdbc.Driver, connectionURL=jdbc:mysql://localhost:3306/emp, username=dev_usr, password=dev_pss]
JMS Properties : JmsProperties [factoryInitial=org.apache.activemq.jndi.ActiveMQInitialContextFactory, providerUrl=tcp://localhost:61616, queue=dev.queue]

5. 代码

路径

Spring 环境与profile(二)——Properties with Spring的更多相关文章

  1. Spring Boot2 系列教程(二十三)理解 Spring Data Jpa

    有很多读者留言希望松哥能好好聊聊 Spring Data Jpa! 其实这个话题松哥以前零零散散的介绍过,在我的书里也有介绍过,但是在公众号中还没和大伙聊过,因此本文就和大家来仔细聊聊 Spring ...

  2. Spring Boot2 系列教程(二十五)Spring Boot 整合 Jpa 多数据源

    本文是 Spring Boot 整合数据持久化方案的最后一篇,主要和大伙来聊聊 Spring Boot 整合 Jpa 多数据源问题.在 Spring Boot 整合JbdcTemplate 多数据源. ...

  3. Spring Boot2 系列教程(二十六)Spring Boot 整合 Redis

    在 Redis 出现之前,我们的缓存框架各种各样,有了 Redis ,缓存方案基本上都统一了,关于 Redis,松哥之前有一个系列教程,尚不了解 Redis 的小伙伴可以参考这个教程: Redis 教 ...

  4. Spring Boot系列(二):Spring Boot自动装配原理解析

    一.Spring Boot整合第三方组件(Redis为例) 1.加依赖 <!--redis--> <dependency> <groupId>org.springf ...

  5. Spring Boot2 系列教程(二十四)Spring Boot 整合 Jpa

    Spring Boot 中的数据持久化方案前面给大伙介绍了两种了,一个是 JdbcTemplate,还有一个 MyBatis,JdbcTemplate 配置简单,使用也简单,但是功能也非常有限,MyB ...

  6. Spring boot入门(二):Spring boot集成MySql,Mybatis和PageHelper插件

    上一篇文章,写了如何搭建一个简单的Spring boot项目,本篇是接着上一篇文章写得:Spring boot入门:快速搭建Spring boot项目(一),主要是spring boot集成mybat ...

  7. Spring学习笔记(二) 初探Spring

    版权声明 笔记出自<Spring 开发指南>一书. Spring 初探 前面我们简单介绍了 Spring 的基本组件和功能,现在我们来看一个简单示例: Person接口Person接口定义 ...

  8. Spring Boot (十二): Spring Boot 邮件服务

    最早我们发邮件的时候是使用 JavaMail 来发送邮件,而在 Spring Boot 中, Spring Boot 帮我们将 JavaMail 封装好了,是可以直接拿来使用的. 1. 依赖文件 po ...

  9. Spring Boot2 系列教程(二十八)Spring Boot 整合 Session 共享

    这篇文章是松哥的原创,但是在第一次发布的时候,忘了标记原创,结果被好多号转发,导致我后来整理的时候自己没法标记原创了.写了几百篇原创技术干货了,有一两篇忘记标记原创进而造成的一点点小小损失也能接受,不 ...

随机推荐

  1. [ 9.9 ]CF每日一题系列—— 259A黑白棋盘检查问题

    http://codeforces.com/problemset/problem/259/A PS9.8日做了但是忘了发博客,所以坚持3天了呦~ 终于读懂了这个题了,心累 Describe: 给你8 ...

  2. django创建分页

    前台html代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  3. Excel函数vlookup

    最近整理业务文档,需要用到excel,按照教程,操作了20来分钟,却得不到结果. 看了视频,才知道,vlookup仅限关联选中区域的第一列关联,把要关联的行拷贝到第一列,解决. https://www ...

  4. Android-WebView加载网络图片&网页

    加载网络图片: 链接地址:  http://bcs.link-us.com.cn/directBank/newHX149/directBank/h5/www/dist/img/e113.jpg 确保链 ...

  5. Caffe + Ubuntu 14.04 64bit + CUDA 6.5 配置说明2

    1. 安装build-essentials 安装开发所需要的一些基本包 sudo apt-get install build-essential 2. 安装NVIDIA驱动 (3.4.0) 2.1 准 ...

  6. java异步线程

    使用一个ExecutorService,增加两个不可取消的子线程任务,并且获取他们的返回值. ​ @org.junit.Test public void testFuture() throws Int ...

  7. Web应用安全之Response Header里的敏感信息

    Web应用安全之Response Header 文/玄魂 目录 Web应用安全之Response Header 前言 1.1  那些敏感的header 1.2 删除敏感的header 1.2.1 删除 ...

  8. css中“~”和“>”是什么意思

    p~ul选择器 p之后出现的所有ul. 两种元素必须拥有相同的父元素,但是 ul不必直接紧随 p. css中“>”是: css3特有的选择器,A>B 表示选择A元素的所有子B元素. 与A ...

  9. Asp .Net core 2 学习笔记(2) —— 中间件

    这个系列的初衷是便于自己总结与回顾,把笔记本上面的东西转移到这里,态度不由得谨慎许多,下面是我参考的资源: ASP.NET Core 中文文档目录 官方文档 记在这里的东西我会不断的完善丰满,对于文章 ...

  10. .net图表之ECharts随笔03-热力地图

    基于01和02 要得到如图所示的热力地图(我从NuGet上下载的包没有heatmap.js文件,没法直接搞热力图,只好暂时先搞着地图.后面尽量搞一下),一般要设置四个参数——title.tooltip ...