原创文章,转载请注明出处

分离的好处就不说了。说下分离的思路。通过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 应用的二进制代码与配置分离。的更多相关文章

  1. spring cloud深入学习(七)-----配置中心git示例

    随着线上项目变的日益庞大,每个项目都散落着各种配置文件,如果采用分布式的开发模式,需要的配置文件随着服务增加而不断增多.某一个基础服务信息变更,都会引起一系列的更新和重启,运维苦不堪言也容易出错.配置 ...

  2. 明白生产环境中的jvm参数

    明白生产环境中的jvm参数 写代码的时候,程序写完了,发到线上去运行,跑一段时间后,程序变慢了,cpu负载高了--一堆问题出来了,所以了解一下生产环境的机器上的jvm配置是有必要的.比如说: JDK版 ...

  3. 【JVM】3、jvm参数和main方法参数

    在实际应用中,我们经常会使用一些额外的参数定义不通的环境下jvm的启动设置 特别是springCloud的项目,因为yml配置文件的问题,如果我们要做负载的话,会同时启动一个jar当做2个服务 那么这 ...

  4. JVM参数优化(基础篇)

    原文:https://www.howardliu.cn/java/jvm-tuning-basic/ 这几天压测预生产环境,发现TPS各种不稳.因为是重构的系统,据说原来的系统在高并发的时候一点问题没 ...

  5. JVM中的GC算法,JVM参数,垃圾收集器分类

    一.在JVM中什么是垃圾?如何判断一个对象是否可被回收?哪些对象可以作为GC Roots的根 垃圾就是在内存中已经不再被使用到的空间就是垃圾. 1.引用计数法: 内部使用一个计数器,当有对象被引用+1 ...

  6. 转载:把你的精力专注在java,jvm原理,spring原理,mysql锁,事务,多线程,大并发,分布式架构,微服务,以及相关的项目管理等等,这样你的核心竞争力才会越来越高

    https://developer.51cto.com/art/202001/608984.htm 把你的精力专注在java,jvm原理,spring原理,mysql锁,事务,多线程,大并发,分布式架 ...

  7. JVM 参数的设置及解析

    JVM 参数的设置及解析 1.关于JVM配置: 2.Linux JVM设置: 1.关于JVM配置: 设置jvm内存的参数有四个: -Xmx 设置堆(Java Heap)最大值,默认值为物理内存的1/4 ...

  8. Java 6 JVM参数选项大全(中文版)

    原文来自: http://kenwublog.com/docs/java6-jvm-options-chinese-edition.htm 本文是基于最新的SUN官方文档Java SE 6 Hotsp ...

  9. jinfo_动态调整JVM参数(无需重启)(实践)

    ​本文演示在JVM进程运行过程中动态开启/关闭 GC输出,无需重启JVM进程 jinfo使用介绍 可以用来查看正在运行的Java应用程序的扩展参数,甚至支持在运行时,修改部分参数 -flag < ...

随机推荐

  1. TXB0108 TXS0108E 8-Bit Bidirectional Voltage-Level Translator for Open-Drain and Push-Pull Applications

    TXS(开漏优化设计),如I2C TXB(上拉优化设计),如SPI TXS0108 has integrated pull-up resistors to save board space and c ...

  2. GO开发环境搭建

    GO开发环境搭建 1)下载GO SDK,并安装 https://golang.google.cn/dl/ 2)下载GO IDE:GOLAND,并安装 3)设置GOROOT和GOPATH 4)新建一个工 ...

  3. PL/SQL中复制中文再粘贴出现乱码问题的解决【转】

    前不久!我对我的windowsxp做了一番大规模的设置:包括区域.系统.网络等方面的,结果当我设置完成以后,发现如果我从一些软件上复制内容到记事本里面会出现乱码,而且如果复制到word里面也不能够正常 ...

  4. 深入理解Git (一) - 元数据

    三分钟教你学Git仅仅是教我们会用Git了,但是内部的一些实现原理假设我们也知道一些的话会使我们使用起来更加顺手.这个系列就是在你用了Git一段时间之后,并想继续到Git的内部看一下到底而准备的. G ...

  5. 解决netaddr不能安装0.7.6以上版本

    今天在Jenkins上添加了两个新jobs,但是同事提交代码后的自动测试运行出现错误.查看原因是tox命令在安装包依赖的时候,找不到大于0.7.6以上的版本,但可是我们明明安装过0.7.10了呀. 后 ...

  6. RPC简述

    在某种意义上,WebService.REST均是RPC的实现,那么RPC的发展过程如何呢?本文参考了wikipedia,对RPC做一下简要摘记. RPC(RemoteProcedureCall),是进 ...

  7. Linux内核编译与安装

    2013-04-16    Linux内核介绍  Linux内核是一个用C语言写成的,符合POSIX标准的类Unix操作系统.内核是操作系统中最基本的一部分,提供了众多应用程序访问计算机硬件的机制.L ...

  8. Ubuntu 安装nginx

    https://www.nginx.com/resources/admin-guide/load-balancer/ https://github.com/gplessis/dotdeb-nginx/ ...

  9. curl 调用jenkins的api

    jenkins提供了rest api,通过调用接口,可以执行一些job的操作,如构建job ,新建job,启用禁用等操作骑车 其次curl是什么? cURL是一个利用URL语法在命令行下工作的文件传输 ...

  10. 使用Docker-Docker for Web Developers(2)

    1. 使用镜像 1.1 在Docker Hub上查找镜像 我们查找一下之前博客里面,推送到Docker Hub里面的bage88/docker-demo,能看到有2个仓库,第一个就是我们上次上传的镜像 ...