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. toast js

    参考别人的,自己改写了下,很好用. <!DOCTYPE html> <html lang="zh-CN"> <head> <meta ch ...

  2. PCI总线目标接口状态机设计

    module state_machine (devsel_l, trdy_l, stop_l, pci_ad_oe,      dts_oe, par_oe, bk_oe, pci_ad_en, hi ...

  3. unigui回车代替TAB

    unigui回车代替TAB 在业务系统中常常使用回车键(Enter)替代Tab键完成焦点跳转,在uniGUI下,可以不用代码,直接使用TUniForm的NavigateKeys进行设置: 其中Next ...

  4. Http站点转Https站点教程

    https://blog.csdn.net/tanga842428/article/details/79273226 Http站点转Https站点教程 2018年02月28日 12:04:35 坦GA ...

  5. Spring 使用javaconfig配置

    除了使用xml,spring提供javaconfig配置,下面是简单的例子: 1.声明接口 /** * */ package com.junge.demo.spring.service; /** * ...

  6. WebApi使用JWT认证

    https://www.cnblogs.com/wangyulong/p/8727683.html https://blog.csdn.net/kebi007/article/details/7286 ...

  7. 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 准 ...

  8. 解决C#项目出现“此项目引用这台计算机上缺少的 NuGet 程序包。使用 NuGet 程序包还原可下载这些程序包。有关详细信息,请参阅 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 ..\packages\Microsoft.Net.Compilers.1.0.0\build\Microsoft.Net.Compilers.props”

    1.打开项目的.csproj文件 2.删掉如下选中的内容: 3.右键项目-->管理NuGet程序包(N) 找到Microsoft.CodeDom.Providers.DotNetCompiler ...

  9. hash的安全性

    在区块链中,我们面临着两个问题: 隐私问题 快速对账问题 由于区块链中,每个人都存在着一个账本,当一个人有收入的时候,将会进行广播到所有人的账本,例如张三收入xxx钱,这样子所有的账本才能同步更新.但 ...

  10. 23_pikle/shevel/json

    一.序列化       存储数据或者传输数据时,需要把对象进行处理,把对象处理成方便存储和传输的数据格式.不同的序列化,结果也不同.     序列化方式:         (1) pickle 可以将 ...