zoukankan      html  css  js  c++  java
  • 通过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:xxxxxyyyy" 然后将所有的配置文件和属性文件放到对应的目录中就可以了。

  • 相关阅读:
    hrbust 1840 (树状数组第k大) 删点使用
    20150211--Smarty2-02
    20150211--Smarty2-01
    20150210--Smarty1-02
    20150210--Smarty1-01
    20150209--JS巩固与加强6-02
    20150209--JS巩固与加强6-01
    20150207--JS巩固与加强5
    20150206--JS巩固与加强4-02
    20150206--JS巩固与加强4
  • 原文地址:https://www.cnblogs.com/yudar/p/5255262.html
Copyright © 2011-2022 走看看