spring配置中,properties文件以及xml文件配置问题

来源:互联网 发布:中铁物资西南公司知乎 编辑:程序博客网 时间:2024/05/19 05:34

一、在springMVC配置读取properties文件

1.第一种方式

<?xml version="1.0" encoding="UTF-8"?>   <beans xmlns="http://www.springframework.org/schema/beans"   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"   xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:schemaLocation="            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">      <context:annotation-config />    <context:component-scan base-package="com.mvc" />  <!-- 自动扫描所有注解该路径 -->      <context:property-placeholder location="classpath:/hibernate.properties" />  
2.第二种方式

spring方便我们的项目快速搭建,功能强大,自然也会是体系复杂!

这里说下配置文件properties管理的问题。

 

一些不涉及到代码逻辑,仅仅只是配置数据,可以放在xxxx.properties文件里面,项目功能复杂的时候,往往properties文件很多,这时,就比较容易让人困惑,有些properties的文件内容总是加载不起来,应用启动时,就不断爆出错误,说某某参数加载失败,这个是什么原因呢?

 

其实,这个是spring配置的时候,org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer这个bean只会被加载一次。在spring的xml配置文件中,多处出现这个bean的配置,spring加载的时候,只会加载第一个,后面的就加载不了。

如何解决呢?其实很简单,比如我的一个应用中,就有internal.properties和jdbc.properties两个文件。按下面这种方式就可以解决:

复制代码
 1     <bean id="configRealm" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 2          <property name="locations"> 3             <list> 4                 <value>classpath:conf/internal.properties</value> 5                 <value>classpath:conf/jdbc.properties</value> 6             </list> 7         </property> 8     </bean> 9      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">10          <property name="properties" ref="configRealm"/>11     </bean>
复制代码

若有多个,可以在list中类似红色部分添加就可以了!

 

在这里,再多说一点,xml的配置问题,当然,不是功能问题,而是习惯问题!

大的基于spring的项目,往往有很多xml配置文件,比如DAO的,比如权限shiro的配置,比如redis的配置等等,在web.xml中的context-param字段,可以如下配置:

1    <context-param>2         <param-name>contextConfigLocation</param-name>3         <param-value>classpath:conf/spring-dao.xml,classpath:conf/spring-servlet.xml</param-value>4     </context-param>

但是,更好的办法,是将一些应用模块的xml配置通过import resource的方式放在一个xml文件中,例如applicationContext.xml。比如,我的应用applicationContext.xml文件的内容:

复制代码
 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3        xmlns:context="http://www.springframework.org/schema/context" 4        xmlns:aop="http://www.springframework.org/schema/aop" 5        xsi:schemaLocation=" 6    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 7    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 8      9     <bean id="configRealm" class="org.springframework.beans.factory.config.PropertiesFactoryBean">10          <property name="locations">11             <list>12                 <value>classpath:conf/internal.properties</value>13                 <value>classpath:conf/jdbc.properties</value>14             </list>15         </property>16     </bean>17      <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">18          <property name="properties" ref="configRealm"/>19     </bean>20 21     <import resource="applicationContext-springdata.xml"/>22     <import resource="applicationContext-security.xml"/>23     <import resource="applicationContext-ehcache.xml"/>24     <import resource="applicationContext-task.xml"/>25     <import resource="applicationContext-external.xml"/>26     <import resource="applicationContext-message.xml"/> -->27 </beans>
复制代码

这个applicationContext.xml可以在web.xml中的context-param配置部分加载,如下:

1     <context-param>2         <param-name>contextConfigLocation</param-name>3         <param-value>classpath:conf/applicationContext.xml</param-value>4     </context-param>

这样子,就会使得项目的配置文件结构非常的清晰!

3.Java中properties

 在我们平时写程序的时候,有些参数是经常改变的,而这种改变不是我们预知的。比如说我们开发了一个操作数据库的模块,开发时我们连接本地的数据库IP、数据库名称、表名称,这些信息都是本地的,要使得这个操作数据的模块具有通用性,那么以上信息就不能写死在程序里,通常我们的做法是用配置文件来解决。 
      各种语言都有自己所支持的配置文件类型。比如 Python支持.ini 文件。因为它内部有一个ConfigParser 类来支持 .ini 文件的读写,根据该类提供的方法程序员可以自由的来操作 .ini 文件。而在Java 中,Java支持的是 .properties 文件的读写,JDK 内置的Java.util.Properties 类为我们操作 .properties 文件提供了便利。 
------------------------------------------------------------------------------ 
一. .properties 文件的形式 
# 以下为服务器、数据库信息 
username= root 
userpwd= mydb 

我们假设该文件名为:test.properties 文件。其中 # 开始的一行为注释信息;在等号“ = ”左边的我们称之为 key ;等号“ = ”右边的我们称之为 value (即键 - 值对), key 应该是我们程序中的变量,而 value 是我们根据实际情况配置的。 

二.JDK 中的Properties 类存在于包 Java.util 中,该类继承自 Hashtable ,它提供了几个主要的方法: 
1. getProperty ( String key) ,用指定的键在此属性列表中搜索属性,也就是通过参数 key,得到 key 所对应的 value。 
2. load ( InputStream inStream) ,从输入流中读取属性列表(键和元素对)。通过对指定的文件(如test.properties 文件)进行装载来获取该文件中的所有键 - 值对,以供 getProperty ( String key) 来搜索。 
3. setProperty ( String key, String value) ,调用 Hashtable 的方法 put 。它他通过调用基类的put方法来设置 键 - 值对。 

4. store ( OutputStream out, String comments) ,   以适合使用 load 方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。 

5. clear () ,清除所有装载的 键 - 值对。该方法在基类中提供。 


有了以上几个方法我们就可以对 .properties 文件进行操作了!

参考自http://qinya.iteye.com/blog/737446

 

----------------------------------------------------------------------------- 

假如我们把这个test.properties文件放在Java工程中 WEB-INF/classes/test.properties路径中

String path = "WEB-INF/classes/test.properties";           //此处使用相对路径

ServletContext ctx = getServletContext();                      //获取上下文对象

String realpath = ctx.getRealPath(path);                       //由上下文对象获得test.properties文件的绝对路径

InputStream is = new FileInputStream(realpath);        

Properties p = new Properties();

 p.load(is);                                                                      //从输入流中读取键值对

DBConfig db = new DBConfig();                                    //DBConfig为专为 数据库信息 设计的类

String username = p.getProperty("username");

String userpwd = p.getProperty("userpwd");

db.setUsername(username);

db.setUserpwd(userpwd);

ctx.setAttribute("dbconfig", db);                                    //将数据库信息保存在servlet上下文对象

is.close();

这样就可以在其他servlet中使用DBConfig db = (DBConfig)ctx.getAttribute("dbconfig");获得保存的信息。


除使用properties配置文件的方法外,还可以直接在web.xml文件的中添加:

 

dbuser

root

dbpwd

sqlserver

添加键值对,并在servlet的init()方法中使用ServletContext 的getInitParameter()获取到键值信息,init()方法是自动运行的:

ServletContext ctx = getServletContext();

String username1 = ctx.getInitParameter("dbuser");

String userpwd1 = ctx.getInitParameter("dbpwd");

DBConfig db1 = new DBConfig();

db1.setUsername(username1);

db1.setUserpwd(userpwd1);

ctx.setAttribute("dbconfig1", db1);

 

如果希望配置信息只能被某一个servlet来读取,可以在web.xml文件将初始化参数配置到中,通过在该servlet的init()中调用ServletConfig来读取。

     ReadInit

    com.ouc.ReadInit

   

    dbuser

    root

   

   

    dbpwd

    mysql

   

    1

 

1是标记容器是否在启动的时候就加载这个servlet,当值为0或者大于0时,表示容器在应用启动时就加载这个servlet;当是一个负数时或者没有指定时,则指示容器在该servlet被选择时才加载,正数的值越小,启动该servlet的优先级越高。

ServletConfig config=getServletConfig();

String username=config.getInitParameter("dbuser");

String userpwd = config.getInitParameter("dbpwd");

DBConfig db = new DBConfig();

db.setUsername(username);

db.setUserpwd(userpwd);

ServletContext ctx = getServletContext();

ctx.setAttribute("dbconfig", db);



0 0
原创粉丝点击