spring+jidi读取property的配置文件

本文介绍如何在Spring项目中通过PropertyPlaceholderConfigurer加载多个配置文件,并实现灵活的配置管理。

假如你现在还在为自己的技术担忧,假如你现在想提升自己的工资,假如你想在职场上获得更多的话语权,假如你想顺利的度过35岁这个魔咒,假如你想体验BAT的工作环境,那么现在请我们一起开启提升技术之旅吧,详情请点击http://106.12.206.16:8080/qingruihappy/index.html

 

 

 在Spring项目中,你可能需要从properties文件中读入配置注入到bean中,例如数据库连接信息,memcached server的地址端口信息等,这些配置信息最好独立于jar包或者war包,这样便于修改配置。Spring提供了PropertyPlaceholderConfigurer类来处理这件事情。

一个系统中通常会存在如下一些以Properties形式存在的配置文件
1.数据库配置文件demo-db.properties:
Properties代码  
database.url=jdbc:mysql://localhost/smaple  
database.driver=com.mysql.jdbc.Driver  
database.user=root  
database.password=123  
 
2.消息服务配置文件demo-mq.properties:
Properties代码  
mq.java.naming.factory.initial=org.apache.activemq.jndi.ActiveMQInitialContextFactory  
mq.java.naming.provider.url=failover:(tcp://localhost:61616?soTimeout=30000&connectionTimeout=30000)?jms.useAsyncSend=true&timeout=30000  
mq.java.naming.security.principal= jjktyjydjd 
mq.java.naming.security.credentials= jytjtyjjt
jms.MailNotifyQueue.consumer=5  
 
3.远程调用的配置文件demo-remote.properties:
Properties代码  
remote.ip=localhost  
remote.port=16800  
remote.serviceName=test  

 

 

 

一、系统中需要加载多个Properties配置文件
应用场景:Properties配置文件不止一个,需要在系统启动时同时加载多个Properties文件。
配置方式:
Xml代码  
 
  (1)<!-- 将多个配置文件读取到容器中,交给Spring管理 -->  
   <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
      <property name="locations">  
         <list>  
            <!-- 这里支持多种寻址方式:classpath和file -->  
            <value>classpath:/opt/demo/config/demo-db.properties</value>  
            <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->  
            <value>file:/opt/demo/config/demo-mq.properties</value>  
            <value>file:/opt/demo/config/demo-remote.properties</value>  
         </list>  
      </property>  
   </bean>   
 
   <!-- 使用MQ中的配置 -->  
   <bean id="MQJndiTemplate" class="org.springframework.jndi.JndiTemplate">  
     <property name="environment">  
       <props>  
         <prop key="java.naming.factory.initial">${mq.java.naming.factory.initial}</prop>  
         <prop key="java.naming.provider.url">${mq.java.naming.provider.url}</prop>  
         <prop key="java.naming.security.principal">${mq.java.naming.security.principal}</prop>  
         <prop key="java.naming.security.credentials">${mq.java.naming.security.credentials}</prop>  
         <prop key="userName">${mq.java.naming.security.principal}</prop>  
         <prop key="password">${mq.java.naming.security.credentials}</prop>  
       </props>  
      </property>  
   </bean>

      (2) 

 
<!-- 将多个配置文件读取到容器中(配置文件放在了tomcat中的),交给Spring管理 -->

 

      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

 

        <property name="locations">
            <list>
                <value>file:${catalina.home}/etc/domain/domain.properties</value>
                <value>file:${catalina.home}/etc/dcas/dcas-client.properties</value>
<value>file:${catalina.home}/etc/fruadmetrix/fruadmetrix.properties</value>
            </list>
        </property>
    </bean>

   <!--使用配置的另一种写法>

 
   <bean id="freightConfig" class="com.olymtech.dzg.freight.enquiry.rest.config.FreightConfig">

 

     <property name="fraudmetrixUrl" value="${fraudmetrix.fraudmetrixUrl}"></property>
     <property name="partner_code" value="${fraudmetrix.partner_code}"></property>
     <property name="secret_key" value="${fraudmetrix.secret_key}"></property>
     <property name="event_id" value="${fraudmetrix.event_id}"></property>
      </bean>

 (3)也可以将list抽取出来

            <!-- 将多个配置文件位置放到列表中 -->  

 

 

    <bean id="propertyResources" class="java.util.ArrayList">  
     <constructor-arg>  
       <list>  
        <!-- 这里支持多种寻址方式:classpath和file -->  
        <value>classpath:/opt/demo/config/demo-db.properties</value>  
        <!-- 推荐使用file的方式引入,这样可以将配置和代码分离 -->  
        <value>file:/opt/demo/config/demo-mq.properties</value>  
        <value>file:/opt/demo/config/demo-remote.properties</value>  
       </list>  
     </constructor-arg>  
    </bean>   
 
    <!-- 将配置文件读取到容器中,交给Spring管理 -->  
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations" ref="propertyResources" />  
    </bean>  

 

 

 

 

class Tongbao: def __init__(self,sortname,money,percent): self.danwei = "单位" self.file_name = Load.read_excel("./a站点通报.xlsx", "py通报") self.sortname = sortname self.money = money self.percent = percent def paiming(self,unit): b = [1,2,3,8,9,10] self.danwei = [] Money = [] Precent = [] for i in b: self.danwei.append(self.file_name.loc[self.file_name[self.sortname] == i, self.danwei].values) Money.append(self.file_name.loc[self.file_name[self.sortname] == i, self.money].values) Precent.append(self.file_name.loc[self.file_name[self.sortname] == i, self.percent].values) # print(top_3['单位'][0]) res = f"{self.danwei[0][0]}({round(Money[0][0],2)}{unit},完成{round(Precent[0][0]*100,2)}%),{self.danwei[1][0]}({round(Money[1][0],2)}{unit},完成{round(Precent[1][0]*100,2)}%),{self.danwei[2][0]}({round(Money[2][0],2)}{unit},完成{round(Precent[2][0]*100,2)}%)" res1 = f"{self.danwei[3][0]}({round(Money[3][0],2)}{unit},完成{round(Precent[3][0]*100,2)}%),{self.danwei[4][0]}({round(Money[4][0],2)}{unit},完成{round(Precent[4][0]*100,2)}%),{self.danwei[5][0]}({round(Money[5][0],2)}{unit},完成{round(Precent[5][0]*100,2)}%)" return res,res1 xundian_res,xundian_res1 = Tongbao(sortname="巡店排名",money = "当月巡店2次以上的站点数",percent = "当月巡店率").paiming("个") res_jidi,res_jidi1 = Tongbao(sortname="寄递排名",money = "寄递叠加累计",percent = "寄递叠加完成率").paiming("个") res_jinrong,res_jinrong1 = Tongbao(sortname="金融排名",money = "叠加金融累计",percent = "金融叠加完成率").paiming("个") res_youzhi,res_youzhi1 = Tongbao(sortname="站点排名",money = "已优质站点完成",percent = "优质站点完成率").paiming("个") 代码报错Index data must be 1-dimensional
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值