本文主要介紹怎么通過屬性注入與構造器注入實現把我們項目中要用到的數據庫參數放到xml文件里面去,方便部署。
spring mvc 4.2.6項目
sql server 2008數據庫
本文介紹的主要使用applicationcontext以及其實現類實現。主要用到的是classpathxmlapplicationcontext。
classpathxmlapplicationcontext:從類路徑classpath中尋找指定的xml配置文件,找到并裝載
完成applicationcontext的實例化工作。例如:
1
2
3
4
5
6
|
//裝載單個配置文件實例化applicationcontext容器 applicationcontext cxt = new classpathxmlapplicationcontext ( "applicationcontext.xml" ); //裝載多個配置文件實例化applicationcontext容器 string[] configs = { "bean1.xml" , "bean2.xml" , "bean3.xml" }; applicationcontext cxt = new classpathxmlapplicationcontext(configs); |
下面是具體步驟:
一、屬性注入
屬性注入即通過 setattribute 方法注入bean 的屬性值或依賴的對象。屬性注入使用 元素, 使用 name 屬性指定 bean 的屬性名稱,value 屬性或 子節點指定屬性值。
1、創建一個bean類dbparaproperty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package com; public class dbparaproperty { //jdbc sqlserver 驅動類 string sqlserverdriverclassname; //sqlserver 連接地址 string sqlserverurl; //sqlserver 用戶名 string sqlserverusername; //sqlserver 密碼 string sqlserverpassword; public string getsqlserverdriverclassname(){ return this .sqlserverdriverclassname; } public void setsqlserverdriverclassname(string sqlserverdriverclassname){ this .sqlserverdriverclassname = sqlserverdriverclassname; } public string getsqlserverurl(){ return this .sqlserverurl; } public void setsqlserverurl(string sqlserverurl){ this .sqlserverurl = sqlserverurl; } public string getsqlserverusername(){ return this .sqlserverusername; } public void setsqlserverusername(string sqlserverusername){ this .sqlserverusername = sqlserverusername; } public string getsqlserverpassword(){ return this .sqlserverpassword; } public void setsqlserverpassword(string sqlserverpassword){ this .sqlserverpassword = sqlserverpassword; } } |
2、創建一個xml文件
文件內容如下
1
2
3
4
5
6
7
8
9
10
11
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" 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.xsd" > <bean id= "dbparaproperty" class = "com.dbparaproperty" > <property name= "sqlserverdriverclassname" value= "com.microsoft.sqlserver.jdbc.sqlserverdriver" ></property> <property name= "sqlserverurl" value= "jdbc:sqlserver://127.0.0.1:1433;databasename=test;" ></property> <property name= "sqlserverusername" value= "sadbparaproperty" ></property> <property name= "sqlserverpassword" value= "admin123" ></property> </bean> </beans> |
3、在controller中使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
package test; import com.dbparaconstructor; import com.dbparaproperty; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.responsebody; @controller @requestmapping ( "/test2" ) public class test2 { @requestmapping ( "/test" ) @responsebody public object test2() { //如果xml文件在src下面的話,直接寫文件名就行 applicationcontext cpxac = new classpathxmlapplicationcontext( "dbparaproperty.xml" ); //根據bean節點的標識獲取對象,id dbparaproperty dbparaproperty = (dbparaproperty) cpxac.getbean( "dbparaproperty" ); system.out.println(dbparaproperty.getsqlserverusername()); return dbparaproperty.getsqlserverusername(); } } |
二、構造器注入
通過構造方法注入bean 的屬性值或依賴的對象,它保證了 bean 實例在實例化后就可以使用。構造器注入在 元素里聲明屬性。
步驟如下:
1、創建dbparaconstructor類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package com; public class dbparaconstructor { //jdbc sqlserver 驅動類 public string sqlserverdriverclassname; //sqlserver 連接地址 public string sqlserverurl; //sqlserver 用戶名 public string sqlserverusername; //sqlserver 密碼 public string sqlserverpassword; public dbparaconstructor(){} public dbparaconstructor(string sqlserverdriverclassname,string sqlserverurl,string sqlserverusername,string sqlserverpassword){ this .sqlserverdriverclassname = sqlserverdriverclassname; this .sqlserverurl = sqlserverurl; this .sqlserverusername = sqlserverusername; this .sqlserverpassword = sqlserverpassword; } } |
2、在src下面的文件夾test下創建一個xml文件。
1
2
3
4
5
6
7
8
9
10
11
|
<?xml version= "1.0" encoding= "utf-8" ?> <beans xmlns= "http://www.springframework.org/schema/beans" 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.xsd" > <bean id= "dbparaconstructor" class = "com.dbparaconstructor" > <constructor-arg name= "sqlserverdriverclassname" value= "com.microsoft.sqlserver.jdbc.sqlserverdriver" ></constructor-arg> <constructor-arg name= "sqlserverurl" value= "jdbc:sqlserver://127.0.0.1:1433;databasename=test;" ></constructor-arg> <constructor-arg name= "sqlserverusername" value= "sadbparaconstructor" ></constructor-arg> <constructor-arg name= "sqlserverpassword" value= "admin456" ></constructor-arg> </bean> </beans> |
3、在controller中使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package test; import com.dbparaconstructor; import com.dbparaproperty; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframework.web.bind.annotation.responsebody; @controller @requestmapping ( "/test2" ) public class test2 { @requestmapping ( "/test" ) @responsebody public object test2() { applicationcontext cpxac = new classpathxmlapplicationcontext( "dbparaproperty.xml" ); dbparaproperty dbparaproperty = (dbparaproperty) cpxac.getbean( "dbparaproperty" ); system.out.println(dbparaproperty.getsqlserverusername()); applicationcontext acc = new classpathxmlapplicationcontext( "/test/dbparaconstructor.xml" ); dbparaconstructor dbparaconstructor = (dbparaconstructor)acc.getbean( "dbparaconstructor" ); system.out.println(dbparaconstructor.sqlserverusername); return dbparaproperty.getsqlserverusername()+ "*****" +dbparaconstructor.sqlserverusername; } } |
項目目錄如下:
關于那個路徑的,java會把java文件編譯成.class文件放到classes目錄下,這個也是項目java代碼運行的根目錄。所以當你把xml文件放在src下面的時候,可以直接寫文件名就可以找到了,但是如果你把它放在其他的目錄下面了,要把路徑寫好,例如:/test/xxx.xml。
以上這篇spring mvc 讀取xml文件數據庫配置參數的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/u012835032/article/details/51497570