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

分离的好处就不说了。说下分离的思路。通过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. ZwQuerySystemInformation枚举内核模块及简单应用

    简单说,即调用第11号功能,枚举一下内核中已加载的模块.部分代码如下://功能号为11,先获取所需的缓冲区大小ZwQuerySystemInformation(SystemModuleInformat ...

  2. C调用lua的table里面的函数

    网上搜索C.C++调用lua函数,有一大堆复制粘贴的. 但是搜索<C调用lua的table里面的函数> 怎么就没几个呢? 经过探索,发现其实逻辑是这样的: 1.根据name获取table ...

  3. boost python3依赖安装

    之前写过python dlib依赖的安装,当时是使用了系统默认的python2.7安装的boost.Python,如果自己编译好python3,想安装支持python3的boost.Python也是可 ...

  4. 原创:vsphere概念深入系列二:vSphere交换机命令行查看排错

    1.如何查看VM的IP Addresses, MAC Addresses, Uplink ports, Port ID,VSS/VDS,portgroup,DVPort Group,vmnic Upl ...

  5. OpenCV学习代码记录——人脸检测

    很久之前学习过一段时间的OpenCV,当时没有做什么笔记,但是代码都还在,这里把它贴出来做个记录. 代码放在码云上,地址在这里https://gitee.com/solym/OpenCVTest/tr ...

  6. 11G新特性 -- Multicolumn Statistics (Column groups)

    默认oracle会收集表中各个列的统计信息,但是会忽略列之间的关联关系.在大多情况下,优化器假设在复杂查询中的列之间是独立的.当where子句后指定了一个表的多个列条件时,优化器通常会将多个列的选择性 ...

  7. 11g新特性-自动sql调优(Automatic SQL Tuning)

    11g新特性-自动sql调优(Automatic SQL Tuning) 在Oracle 10g中,引进了自动sql调优特性.此外,ADDM也会监控捕获高负载的sql语句. 在Oracle 11g中, ...

  8. A Tale of Three Apache Spark APIs: RDDs, DataFrames, and Datasets(中英双语)

    文章标题 A Tale of Three Apache Spark APIs: RDDs, DataFrames, and Datasets 且谈Apache Spark的API三剑客:RDD.Dat ...

  9. [ci] jenkins kubernetes插件配置(容器模式)-通过jnlp

    有个小伙用sh结合jenkins搞的k8s cicd还不错 jenkins kubernetes插件 首先插件管理,搜索kubernetes plugin安装 配置kubernetes云 配置项目 执 ...

  10. linux 重启和关机

    重启命令有:reboot,  shutdown -ry 0关机命令有:poweroff, shutdown -hy 0