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

分离的好处就不说了。说下分离的思路。通过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. Android-ContentProvider使用

    Android-ContentProvider使用 一 建立ContentProviderserver端 1 建立一个继承自ContentProvider的类并重写接口方法(这里仅打一些log做代表) ...

  2. VMWare Station 问题汇总

    1.开机黑屏,不启动系统 解决方法: 命令行窗口cmd—输入下面代码,然后重启计算机. netsh winsock reset 2.提示磁盘被锁无法打开 解决方法: 虚拟机目录下面的.lck文件都删了

  3. solr中重建索引(转)

    Stop your application server Change your schema.xml file Start your application server Delete the in ...

  4. mipmap和drawable文件夹的区别

    在Android上创建工程,会默认创建mipmap文件夹.之前在Eclipse上创建的是drawable的文件夹.那么这两个有什么区别呢? 问题: I'm working with android s ...

  5. 如何唯一的标识一台Android设备?

    UUID : (Universally Unique Identifier)全局唯一标识符,是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的.由以下几部分的组合:当前日期和时间(U ...

  6. 阿里云服务器CentOS7怎么分区格式化/挂载硬盘

    一.在阿里云上购买了服务器的硬盘后就可以操作了,先看看硬盘情况: 硬盘vda是系统盘:vdb是在阿里云后台购买的另一块硬盘. 第一次使用要分区:fdisk /dev/vdb1 在提示符下依次输入:n+ ...

  7. ceph 底层代码分享

    一.底层工作队列 二.对象操作 三.上下文(Context)代码分析:

  8. 创建一个可拖动的DIV

    var drag = function(){ var obj = document.getElementById("id"); var s = obj.style; var b = ...

  9. oracle 12c common user与local user

    12c的多租户架构,引入CDB和PDB概念,使得用户也分为两种:common用户和local用户. 1.common用户就是数据库的用户,这个用户在root和每个已存在的或以后要创建的PDB都是相同的 ...

  10. 12C配置EM Express的https端口

    1.启动监听并查看监听信息 $ lsnrctl stat ora12 LSNRCTL for Linux: Version 12.1.0.2.0 - Production on 07-FEB-2017 ...