您的位置:首页 > 编程语言 > Java开发

在spring中读取properties配置文件里面的信息

2015-12-16 16:11 666 查看
properties文件的读取与配置

一般我们在创建项目的时候会把一些经常用到和经常变动的信息写到配置文件里,以便于以后跨平台和移植只需要修改配置文件,不用修改项目代码,这样起到 可很好的解耦合作用。那么,我们spring mvc 是如何读取配置文件的呢?

一般是这样的顺序:

新建properties-->配置文件读取-->Controller依赖注入,放入Session-->前端页面el表达式取到,访问

1、新建:

report.properties里面存放着固定的报表连接地址

例如:report.url=http://192.168.1.1/reportJsp/showReport.jsp?raq=zjzcjdb.raq

2、读取properties文件:

在applicationContext.xml,总的配置文件里面,定义一个专门读取properties文件的类,启动项目时自动加载信息

<!-- 用来解析Java Properties属性文件值(注意class指定的类)-->
<bean id="placeholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:report.properties</value>
</list>
</property>
</bean>

把properties里面的信息读进来:

<bean id="report" class="java.lang.String">
<constructor-arg value="${report.url}"/>
</bean>

3、Controller中注入

@Autowired
private String report;

这样report就可以在Controller里面使用了,例如我们把它加入到Session里面,随时带着以便于用的时候取出

session.setAttribute("reportUrl", report);

4、我们用到这个properties信息的时候在前端是如何取得?

点击事件

<a onclick="javascript:window.open('${reportUrl}')" type="button" class="btn btn-success btn-sm">生成报表</a>

这样我们就可以访问这个链接了,如果链接信息改变的话,只需要修改properties文件里面的url信息就好。

要是链接后面需要跟参数的话:

<a onclick="javascript:window.open('${reportUrl}&year=2015')" type="button" class="btn btn-success btn-sm">

生成报表</a>


下面用两个例子在进一步说明与理解

例1:

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

        <property name="locations">

            <list>

                <value>classpath*:jdbc.properties</value>

                <!--要是有多个配置文件,只需在这里继续添加即可 -->

            </list>

        </property>

    </bean>

这里为什么用locations(还有一个location)

是因为。一般来说。我们的项目里面。配置文件可能存在多个。

就算是只有一个。那将来新添加的话。只需在下面再加一个value标签即可。

而不必再重新改动太多。(当然。性能上是否有影响,这个以当前这种服务器的配置来说。是基科可以忽略不计的)。

然后我们就可以在jdbc.properties文件中填写具体的配置信息了。

    <!-- 配置C3P0数据源 -->

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">

        <property name="driverClass">

            <value>${jdbc.driverClassName}</value>

        </property>

        <property name="jdbcUrl">

            <value>${jdbc.url}</value>

        </property>

        <property name="user">

            <value>${jdbc.username}</value>

        </property>

        <property name="password">

            <value>${jdbc.password}</value>

        </property>

    </bean>

jdbc.properties文件写的信息。

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root

例2:

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

  <property name="locations">

   <list>

     <value>file:/data/pc-config/passport.properties</value>

    <value>classpath:memcached.properties</value>

   </list>

  </property>

 </bean>

classpath:是指的当前类文件的目录下。

file:在window下是指的当前分区(比如你的项目是放在d盘,则是在d:/data/pc-config/passport.properties)

      在linux下,则是当前路径下的文件/data/pc-config/passport.properties
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息