国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

香港云服务器
服務(wù)器之家 - 編程語言 - Java教程 - spring mvc 讀取xml文件數(shù)據(jù)庫配置參數(shù)的方法

spring mvc 讀取xml文件數(shù)據(jù)庫配置參數(shù)的方法

2021-01-22 11:37u012835032 Java教程

下面小編就為大家?guī)硪黄猻pring mvc 讀取xml文件數(shù)據(jù)庫配置參數(shù)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

本文主要介紹怎么通過屬性注入與構(gòu)造器注入實現(xiàn)把我們項目中要用到的數(shù)據(jù)庫參數(shù)放到xml文件里面去,方便部署。

spring mvc 4.2.6項目

sql server 2008數(shù)據(jù)庫

本文介紹的主要使用applicationcontext以及其實現(xiàn)類實現(xiàn)。主要用到的是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 屬性或 子節(jié)點指定屬性值。

1、創(chuàng)建一個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 驅(qū)動類
 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、創(chuàng)建一個xml文件

spring mvc 讀取xml文件數(shù)據(jù)庫配置參數(shù)的方法

文件內(nèi)容如下

?
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");
 //根據(jù)bean節(jié)點的標(biāo)識獲取對象,id
 dbparaproperty dbparaproperty = (dbparaproperty) cpxac.getbean("dbparaproperty");
 system.out.println(dbparaproperty.getsqlserverusername());
 
 return dbparaproperty.getsqlserverusername();
 }
}

二、構(gòu)造器注入

通過構(gòu)造方法注入bean 的屬性值或依賴的對象,它保證了 bean 實例在實例化后就可以使用。構(gòu)造器注入在 元素里聲明屬性。

步驟如下:

1、創(chuàng)建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 驅(qū)動類
 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下創(chuàng)建一個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;
 }
}

項目目錄如下:

spring mvc 讀取xml文件數(shù)據(jù)庫配置參數(shù)的方法

關(guān)于那個路徑的,java會把java文件編譯成.class文件放到classes目錄下,這個也是項目java代碼運行的根目錄。所以當(dāng)你把xml文件放在src下面的時候,可以直接寫文件名就可以找到了,但是如果你把它放在其他的目錄下面了,要把路徑寫好,例如:/test/xxx.xml。

以上這篇spring mvc 讀取xml文件數(shù)據(jù)庫配置參數(shù)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://blog.csdn.net/u012835032/article/details/51497570

延伸 · 閱讀

精彩推薦
1212
主站蜘蛛池模板: 日韩国产欧美一区 | av网址在线播放 | 欧美在线免费观看 | 久久麻豆| 成人综合网站 | 亚洲国产精品久久久 | 久久久精品综合 | 看亚洲a级一级毛片 | 99re在线| 国产毛片精品 | 久草视频网 | 99在线热视频 | 国产精品日本欧美一区二区三区 | 国产一区欧美 | 日本在线观看一区二区 | 国产精品尤物在线观看 | 激情网在线观看 | 午夜视频导航 | 丁香伊人 | 日韩精品一区二区三区在线观看视频网站 | 91久久极品 | 欧美大片一区二区 | 免费一区二区三区四区 | 日韩免费在线 | 日本一区二区三区在线视频 | 精品久久一二三区 | 久久久久久国产精品 | 日本中文字幕久久 | 日本高清视频在线播放 | 日韩www| 欧美日韩国产中文 | 国产伦精品一区二区三区四区视频 | 人人人人澡人人爽人人澡 | 日韩在线一区二区三区 | 黄色免费网站在线观看 | 亚洲精选一区二区 | 伊人干| 一区二区三区影视 | 国产一级一级毛片女人精品 | 99精品欧美一区二区三区 | 亚洲网视频 |