spring mvc 讀取xml文件數(shù)據(jù)庫配置參數(shù)的方法
本文主要介紹怎么通過屬性注入與構(gòu)造器注入實(shí)現(xiàn)把我們項(xiàng)目中要用到的數(shù)據(jù)庫參數(shù)放到xml文件里面去,方便部署。
spring mvc 4.2.6項(xiàng)目
SQL Server 2008數(shù)據(jù)庫
本文介紹的主要使用ApplicationContext以及其實(shí)現(xiàn)類實(shí)現(xiàn)。主要用到的是ClassPathXmlApplicationContext。
ClassPathXmlApplicationContext:從類路徑ClassPath中尋找指定的XML配置文件,找到并裝載
完成ApplicationContext的實(shí)例化工作。例如:
//裝載單個(gè)配置文件實(shí)例化ApplicationContext容器
ApplicationContext cxt = new ClassPathXmlApplicationContext
("applicationContext.xml");
//裝載多個(gè)配置文件實(shí)例化ApplicationContext容器
String[] configs = {"bean1.xml","bean2.xml","bean3.xml"};
ApplicationContext cxt = new ClassPathXmlApplicationContext(configs);
下面是具體步驟:
一、屬性注入
屬性注入即通過 setAttribute 方法注入Bean 的屬性值或依賴的對(duì)象。屬性注入使用 元素, 使用 name 屬性指定 Bean 的屬性名稱,value 屬性或 子節(jié)點(diǎn)指定屬性值。
1、創(chuàng)建一個(gè)bean類DBParaProperty
package com;
public class DBParaProperty {
//jdbc sqlserver 驅(qū)動(dòng)類
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)建一個(gè)xml文件

文件內(nèi)容如下
<?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中使用
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é)點(diǎn)的標(biāo)識(shí)獲取對(duì)象,id
DBParaProperty dbParaProperty = (DBParaProperty) cpxac.getBean("DBParaProperty");
System.out.println(dbParaProperty.getSqlServerUserName());
return dbParaProperty.getSqlServerUserName();
}
}
二、構(gòu)造器注入
通過構(gòu)造方法注入Bean 的屬性值或依賴的對(duì)象,它保證了 Bean 實(shí)例在實(shí)例化后就可以使用。構(gòu)造器注入在 元素里聲明屬性。
步驟如下:
1、創(chuàng)建DBParaConstructor類
package com;
public class DBParaConstructor {
//jdbc sqlserver 驅(qū)動(dòng)類
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)建一個(gè)xml文件。
<?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中使用
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;
}
}
項(xiàng)目目錄如下:

關(guān)于那個(gè)路徑的,Java會(huì)把java文件編譯成.class文件放到classes目錄下,這個(gè)也是項(xiàng)目Java代碼運(yùn)行的根目錄。所以當(dāng)你把xml文件放在src下面的時(shí)候,可以直接寫文件名就可以找到了,但是如果你把它放在其他的目錄下面了,要把路徑寫好,例如:/test/xxx.xml。
以上這篇spring mvc 讀取xml文件數(shù)據(jù)庫配置參數(shù)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot中HttpSessionListener的簡單使用方式
這篇文章主要介紹了SpringBoot中HttpSessionListener的簡單使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java面向?qū)ο缶幊讨衒inal關(guān)鍵字的使用方法詳解
這篇文章主要介紹了Java面向?qū)ο缶幊讨衒inal關(guān)鍵字的使用方法詳解,包括對(duì)內(nèi)部匿名類無法訪問外面的非 final 的變量問題的解讀,需要的朋友可以參考下2016-06-06
使用Filter實(shí)現(xiàn)登錄權(quán)限驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了使用Filter實(shí)現(xiàn)登錄權(quán)限驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
MybatisPlus修改時(shí)空字段無法修改的解決方案
這篇文章主要介紹了MybatisPlus修改時(shí)空字段無法修改的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Java類加載機(jī)制實(shí)現(xiàn)步驟解析
這篇文章主要介紹了Java類加載機(jī)制實(shí)現(xiàn)步驟解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
淺談Maven安裝及環(huán)境配置出錯(cuò)的解決辦法
這篇文章主要介紹了淺談Maven安裝及環(huán)境配置出錯(cuò)的解決辦法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09

