spring 配置文件使用

来源:互联网 发布:java 节假日判断 编辑:程序博客网 时间:2024/05/17 19:18

配置文件主要是为了方便项目上线后配置信息随时改变,比较灵活。公司使用的paoding rose,和spring大体差不多。
1. 配置文件bean

package com.xx.gamecenter.dashboard.bean;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;/** * @author zexi * @time 2016年10月20日 下午1:41:30 * 用户信息判定文件 */@Componentpublic class FilePathConfig {    @Value("#{propertyConfigurer['woman.packagename.file.path']}")    private  String womanPackageNameFilePath;    @Value("#{propertyConfigurer['man.packagename.file.path']}")    private  String manPackageNameFilePath;    @Value("#{propertyConfigurer['student.packagename.file.path']}")    private  String studentPackageNameFilePath;    @Value("#{propertyConfigurer['internetuser.packagename.file.path']}")    private  String internetuserPackageNameFilePath;    public String getWomanPackageNameFilePath() {        return womanPackageNameFilePath;    }    public void setWomanPackageNameFilePath(String womanPackageNameFilePath) {        this.womanPackageNameFilePath = womanPackageNameFilePath;    }    public String getManPackageNameFilePath() {        return manPackageNameFilePath;    }    public void setManPackageNameFilePath(String manPackageNameFilePath) {        this.manPackageNameFilePath = manPackageNameFilePath;    }    public String getStudentPackageNameFilePath() {        return studentPackageNameFilePath;    }    public void setStudentPackageNameFilePath(String studentPackageNameFilePath) {        this.studentPackageNameFilePath = studentPackageNameFilePath;    }    public String getInternetuserPackageNameFilePath() {        return internetuserPackageNameFilePath;    }    public void setInternetuserPackageNameFilePath(String internetuserPackageNameFilePath) {        this.internetuserPackageNameFilePath = internetuserPackageNameFilePath;    }}
  1. 配置文件app.properties
woman.packagename.file.path=/data0/test/userInfo_confirm/woman_packageName.txtman.packagename.file.path=/data0/test/userInfo_confirm/man_packageName.txtstudent.packagename.file.path=/data0/test/userInfo_confirm/student_packageName.txtinternetuser.packagename.file.path=/data0/test/userInfo_confirm/internetuser_packageName.txt
  1. applicationContext.xml设置服务器启动加载配置文件
#单个配置文件写法    <bean id="propertyConfigurer"        class="org.springframework.beans.factory.config.PropertiesFactoryBean">        <property name="location">            <value>classpath:server.properties</value>        </property>        <property name="fileEncoding" value="utf-8" />    </bean>
#多个配置文件写法<bean id="propertyConfigurer"        class="org.springframework.beans.factory.config.PropertiesFactoryBean">        <property name="locations">            <list>                <value>classpath:server.properties</value>                <value>classpath:app.properties</value>            </list>        </property>        <property name="fileEncoding" value="utf-8" />    </bean>
  1. 调用配置文件属性
@Autowiredprivate FilePathConfig filePathConfig;filePathConfig.getXXX()调用;

再接再厉!

0 0
原创粉丝点击