使用Spring3.1后的的Profile配置使不同环境加载不同配置文件
来源:互联网 发布:用友t3数据保存在哪 编辑:程序博客网 时间:2024/05/19 16:35
需求:项目运行在不同的环境中,每次运行项目的时候,都要重新手动配置配置文件,这样很容易出错,还好Spring3.1以后又出良心之作–profile,看了其他大佬一些做法,感触颇深,但仍然有疑惑,在这里提出来跟大家探讨一下。
配置文件结构:
|---config |---common |---common.properties |---dev |---config-dev.properties |---dpconfig-dev.properties |---production |---config-production.properties |---dpconfig-production.properties |---spring |---applicationContext-profile.xml |---applicationContext-spring.xml |---test |---config-test.properties |---dpconfig-test.properties
applicationContext-profile.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" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- 测试环境配置文件 --> <beans profile="test"> <context:property-placeholder location="classpath:test/*.properties, classpath:common/*.properties" /> </beans> <!-- 生产环境配置文件 --> <beans profile="production"> <context:property-placeholder location="classpath:production/*.properties, classpath:common/*.properties" /> </beans> <!-- 开发环境配置文件 --> <beans profile="development"> <context:property-placeholder location="classpath:dev/*.properties, classpath:common/*.properties" /> </beans></beans>
applicationContext-spring.xml
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <!-- 数据库基本信息配置 --> <property name="url" value="${url}"/> <!--<property name="username" value="${username}"/> <property name="password" value="${password}"/>--> <property name="driverClassName" value="${driverClassName}"/> <property name="filters" value="${filters}"/> <!-- 最大并发连接数 --> <property name="maxActive" value="${maxActive}"/> <!-- 初始化连接数量 --> <property name="initialSize" value="${initialSize}"/> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="${maxWait}"/> <!-- 最小空闲连接数 --> <property name="minIdle" value="${minIdle}"/> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}"/> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}"/> <property name="validationQuery" value="${validationQuery}"/> <property name="testWhileIdle" value="${testWhileIdle}"/> <property name="testOnBorrow" value="${testOnBorrow}"/> <property name="testOnReturn" value="${testOnReturn}"/> <property name="maxOpenPreparedStatements" value="${maxOpenPreparedStatements}"/> <!-- 打开removeAbandoned功能 --> <property name="removeAbandoned" value="${removeAbandoned}"/> <!-- 1800秒,也就是30分钟 --> <property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}"/> <!-- 关闭abanded连接时输出错误日志 --> <property name="logAbandoned" value="${logAbandoned}"/> </bean> <import resource="applicationContext-profile.xml"/>
事务方面省略,在web.xml中别忘了初始化spring容器.
web.xml
<!-- 配置spring的默认profile 可选值:production(生产环境) test(测试环境) development(开发环境) --> <context-param> <param-name>spring.profiles.active</param-name> <param-value>test</param-value> </context-param> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-spring.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
对于激活Profile的四种方式:
第二种和第三种都试过,其他两种有机会再测。
1.ENV方式: ConfigurableEnvironment.setActiveProfiles("test")2.JVM参数方式: -Dspring.profiles.active="test"3.web.xml方式:<init-param> <param-name>spring.profiles.active</param-name> <param-value>test</param-value></init-param>4.标注方式(junit单元测试非常实用):@ActiveProfiles({"test","productprofile"})
可能是我的理解力不行,所以在这我把我的理解和做法详细的说明一下(仅限第二种和第三种):
一、对于JVM参数方式:
①如果在linux系统中运行(项目已经打成war包发布到tomcat),需要修改bin/catalina.sh, vim 打开文件,添加:-Dspring.profiles.active="test"
后再运行tomcat即可。
②如果在windows系统中运行(项目已经打成war包发布到tomcat),需要修改bin/catalina.bat 添加set JAVA_OPTS="-Dspring.profiles.active=test"
即可
③如果是用Eclipse运行项目,需要右键项目run as – run configuration – Arguments – VM arguments加入-Dspring.profiles.active="test"
运行项目即可
④如果是用IDEA运行项目,需要点击上方工具栏Run – Edit Configurations,项目第一次运行的话需要配置:点击左上角+号,选Tomcat Server – Local,在Application server配置本地的tomcat,在JRE处配置JDK,在VM options中添加-Dspring.profiles.active="test"
运行项目即可
二、对于web.xml方式:
在各个环境中修改
<context-param> <param-name>spring.profiles.active</param-name> <param-value>test</param-value> </context-param>
的值即可。
此处,发现一个问题:之前的配置文件为:
url:jdbc:mysql://***.**.**.***/numysql?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8&allowMultiQueries=truedriverClassName:com.mysql.cj.jdbc.Driverusername:numysqlpassword:123456
这样的话,一直连接不上数据库一直报异常:
java.sql.SQLException: Access denied for user ‘root’@’localhost’ (using password: YES)
后经试验,应该是编码问题,改成这种mysql的用户名和密码拼接在url后即可解决问题:
url:jdbc:mysql://***.**.**.***/numysql?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8&allowMultiQueries=true&user=numysql&password=123456driverClassName:com.mysql.cj.jdbc.Driver
- 使用Spring3.1后的的Profile配置使不同环境加载不同配置文件
- spring3.1 profile 配置不同的环境
- spring3.1 profile 配置不同的环境
- spring3.1 profile 配置不同的环境
- spring3.1 profile 配置不同的环境
- Spring3.1 profile 配置不同的数据库环境
- Spring-Boot使用profile来配置不同环境的配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件
- 利用maven的resources、filter和profile实现不同环境使用不同配置文件(POM中)
- profile 配置 切换不同的数据库环境
- 使用Maven的Profile实现根据不同环境选用不同配置
- 使用Maven的Profile实现根据不同环境选用不同配置
- Spring MVC 根据不同环境加载不同的配置文件
- spring profile来用不同的文件,配置不同的环境, deploy不同的war
- maven 的profile 、resource标签应用。不同的环境下编译导入不同配置文件
- Unity播放声音框架
- jupyter使用matplotlib
- 2017年全球大数据产业报告之海外篇(第五集)
- 【软件网每日新闻播报│第9-18期】
- Batch Normalization
- 使用Spring3.1后的的Profile配置使不同环境加载不同配置文件
- 2017年全球大数据产业报告之海外篇(终结篇)
- Apache-Tomcat的server.xml配置文件中Connector节点中URIEncoding的作用
- CSS居中的常用手段
- Part 4 Package与Import 定义一个圆类(Circle),其所在的包为bzu.info.software;定义一个圆柱类Cylinder,其所在的包为bzu.info.com;
- 推出Android原生开发工具包r16
- hiberate的org.hibernate.loader.custom.NonUniqueDiscoveredSqlAliasException异常解决
- 快把Oracle从云的世界弄走!
- 为什么ZigBee和wifi那么好用,还是选择了lora呢?