通过JVM 参数 实现spring 应用的二进制代码与配置分离。
原创文章,转载请注明出处
分离的好处就不说了。说下分离的思路。通过JVM 参数-D 添加 config.path 的property 到系统中。系统通过System.getProperty(config.path)得到配置文件的目录。然后通过spring的filecontext 加载。例子如下
来看下启动类
public class App
{ public static FileSystemXmlApplicationContext fileSystemXmlApplicationContext = null; static {
if(System.getProperty("os.name").toLowerCase().contains("windows")){
fileSystemXmlApplicationContext=new FileSystemXmlApplicationContext(System.getProperty("config.path").toString()+"\\applicationContext_rules.xml");
}else{
fileSystemXmlApplicationContext=new FileSystemXmlApplicationContext(System.getProperty("config.path").toString()+"/applicationContext_rules.xml");
}
} public static void main( String[] args ) throws RuleEngineException, MqttException { EventRuleService eventRuleTransaction = fileSystemXmlApplicationContext.getBean(EventRuleService.class); System.out.println("test");
}
}
System.getProperty("config.path").toString()+"\\applicationContext_rules.xml" 这里就实现了代码与配置分离。接下来,在 applicationContext_rules.xml 文件中,还有诸如数据库,事务,mybatis mapper文件等的外链。都可以通过
取得config.path实现动态配置。
看例子
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd"> <context:property-placeholder
ignore-unresolvable="true" location="file:${config.path}/config.properties"/>
<!--
<context:property-placeholder
ignore-unresolvable="true" location="classpath*:config.properties"/>
--> <context:component-scan base-package="com.iot.aws" use-default-filters="true" annotation-config="true" >
</context:component-scan>
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- db config 加载配置属性文件 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}"/>
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes >
<tx:method name="delete*" propagation="REQUIRED" read-only="false" />
<tx:method name="insert*" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" read-only="false"/>
<tx:method name="find*" propagation="REQUIRED" read-only="true"/>
<tx:method name="select*" propagation="REQUIRED" read-only="true"/>
<tx:method name="get*" propagation="REQUIRED" read-only="true"/>
<tx:method name="make*" propagation="SUPPORTS" read-only="false"/>
</tx:attributes>
</tx:advice> <aop:config proxy-target-class="true">
<aop:pointcut expression="execution(* com.iot.aws.service.*.*(..))" id="serviceCutPoint"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceCutPoint"/>
</aop:config> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="file:${config.path}/mybatis-config.xml"></property> <!--mybatis配置文件自动扫描路径-->
<property name="mapperLocations">
<array>
<value>file:${config.path}/mappers/*.xml</value>
</array>
</property>
</bean> <mybatis:scan base-package="com.iot.aws.common.mapper" /> </beans>
在spring 中,可以通过${config.path} 获取JVM 添加的属性。通过上面的配置,所以的配置文件和mapper 映射文件都定位到相同的目录。
最后在启动应用的时候加入java -Dconfig.path="d:\xxx\xx\yyyy" 然后将所有的配置文件和属性文件放到对应的目录中就可以了。
通过JVM 参数 实现spring 应用的二进制代码与配置分离。的更多相关文章
- spring cloud深入学习(七)-----配置中心git示例
随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多.某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错.配置 ...
- 明白生产环境中的jvm参数
明白生产环境中的jvm参数 写代码的时候,程序写完了,发到线上去运行,跑一段时间后,程序变慢了,cpu负载高了--一堆问题出来了,所以了解一下生产环境的机器上的jvm配置是有必要的.比如说: JDK版 ...
- 【JVM】3、jvm参数和main方法参数
在实际应用中,我们经常会使用一些额外的参数定义不通的环境下jvm的启动设置 特别是springCloud的项目,因为yml配置文件的问题,如果我们要做负载的话,会同时启动一个jar当做2个服务 那么这 ...
- JVM参数优化(基础篇)
原文:https://www.howardliu.cn/java/jvm-tuning-basic/ 这几天压测预生产环境,发现TPS各种不稳.因为是重构的系统,据说原来的系统在高并发的时候一点问题没 ...
- JVM中的GC算法,JVM参数,垃圾收集器分类
一.在JVM中什么是垃圾?如何判断一个对象是否可被回收?哪些对象可以作为GC Roots的根 垃圾就是在内存中已经不再被使用到的空间就是垃圾. 1.引用计数法: 内部使用一个计数器,当有对象被引用+1 ...
- 转载:把你的精力专注在java,jvm原理,spring原理,mysql锁,事务,多线程,大并发,分布式架构,微服务,以及相关的项目管理等等,这样你的核心竞争力才会越来越高
https://developer.51cto.com/art/202001/608984.htm 把你的精力专注在java,jvm原理,spring原理,mysql锁,事务,多线程,大并发,分布式架 ...
- JVM 参数的设置及解析
JVM 参数的设置及解析 1.关于JVM配置: 2.Linux JVM设置: 1.关于JVM配置: 设置jvm内存的参数有四个: -Xmx 设置堆(Java Heap)最大值,默认值为物理内存的1/4 ...
- Java 6 JVM参数选项大全(中文版)
原文来自: http://kenwublog.com/docs/java6-jvm-options-chinese-edition.htm 本文是基于最新的SUN官方文档Java SE 6 Hotsp ...
- jinfo_动态调整JVM参数(无需重启)(实践)
本文演示在JVM进程运行过程中动态开启/关闭 GC输出,无需重启JVM进程 jinfo使用介绍 可以用来查看正在运行的Java应用程序的扩展参数,甚至支持在运行时,修改部分参数 -flag < ...
随机推荐
- ASP.NET WebAPI构建API接口服务实战演练
一.课程介绍 一.王小二和他领导的第一次故事 有一天王小二和往常一下去上早班,刚吃完早餐刚一打开电脑没一会儿.王小二的领导宋大宝走到他的面前,我们现在的系统需要提供服务给其他内部业务系统,我看你平时喜 ...
- springboot自定义http反馈状态码
最近在开发一些http server类型程序,通过spring boot构建一些web程序,这些web程序之间通过http进行数据访问.共享,如下图: 假设现在client发起一次保存数据的请求到se ...
- 开源框架完美组合之Spring.NET + NHibernate + ASP.NET MVC + jQuery + easyUI 中英文双语言小型企业网站Demo(转)
热衷于开源框架探索的我发现ASP.NET MVC与jQuery easyUI的组合很给力.由于原先一直受Ext JS框架的licence所苦恼,于是痛下决心寻找一个完全免费的js框架——easyUI. ...
- SQL Server为字段添加默认值
SQL Server为字段添加默认值 if not exists ( select * from sys.columns as c join sys.objects as o on c.default ...
- SqlDateTime overflow / SqlDateTime 溢出
Error - SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM SqlDat ...
- 基于Centos搭建Python Web 环境搭建教程
CentOS 7.2 64 位操作系统 安装 setuptools 工具 安装 因为之后我们需要安装 Django ,而 Django 需要用这个工具,所以我们需要先安装 setuptools 工具. ...
- openssl - 怎么打开POD格式的帮助文件
原文链接: http://zhidao.baidu.com/link?url=47I6A0YGA9FnK6galKZ5sxPSZzFGRdng2qhACb4ToBuhuyMhdrwcYpZmNI28y ...
- [转]Java NIO 系列教程
Java NIO提供了与标准IO不同的IO工作方式: Channels and Buffers(通道和缓冲区):标准的IO基于字节流和字符流进行操作的,而NIO是基于通道(Channel)和缓冲区(B ...
- Atitit easyui翻页组件与vue的集成解决方案attilax总结
Atitit easyui翻页组件与vue的集成解决方案attilax总结 ===============使用1 ===========\paggingUtil_easyui_vue.js2 C:\U ...
- Atitti mybatis的单元测试attilax总结
Atitti mybatis的单元测试attilax总结 版本mybatis 3.2.4 /palmWin/src/main/java/com/attilax/dao/mybatisTest.java ...