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 的屬性值或依賴的對象。屬性注入使用 元素, 使用 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)識獲取對象,id
DBParaProperty dbParaProperty = (DBParaProperty) cpxac.getBean("DBParaProperty");
System.out.println(dbParaProperty.getSqlServerUserName());
return dbParaProperty.getSqlServerUserName();
}
}
二、構(gòu)造器注入
通過構(gòu)造方法注入Bean 的屬性值或依賴的對象,它保證了 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創(chuàng)建監(jiān)聽器的方法示例
在Java中,監(jiān)聽器(Listener)是一種設(shè)計(jì)模式,它允許對象在 特定事件 發(fā)生時(shí) 自動(dòng)執(zhí)行某些操作,這種設(shè)計(jì)模式通常用于實(shí)現(xiàn) 發(fā)布-訂閱模型,本文給大家介紹了SpringBoot創(chuàng)建監(jiān)聽器的方法示例,感興趣的通過可以參考一下2024-04-04
Mybatis-plus如何查詢表中指定字段(不查詢?nèi)孔侄?
這篇文章主要介紹了Mybatis-plus如何查詢表中指定字段(不查詢?nèi)孔侄?,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Spring Task 動(dòng)態(tài)修改任務(wù)執(zhí)行計(jì)劃cron方式
這篇文章主要介紹了Spring Task 動(dòng)態(tài)修改任務(wù)執(zhí)行計(jì)劃cron方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java實(shí)現(xiàn)級聯(lián)下拉結(jié)構(gòu)的示例代碼
在開發(fā)過程中,會(huì)遇到很多的實(shí)體需要將查出的數(shù)據(jù)處理為下拉或者級聯(lián)下拉的結(jié)構(gòu),提供給前端進(jìn)行展示。本文為大家介紹了java封裝下拉和級聯(lián)下拉的通用工具類,需要的可以參考一下2022-06-06
Java基礎(chǔ)知識精通二維數(shù)組的應(yīng)用
為了方便組織各種信息,計(jì)算機(jī)常將信息以表的形式進(jìn)行組織,然后再以行和列的形式呈現(xiàn)出來。二維數(shù)組的結(jié)構(gòu)決定了其能非常方便地表示計(jì)算機(jī)中的表,以第一個(gè)下標(biāo)表示元素所在的行,第二個(gè)下標(biāo)表示元素所在的列。下面簡單了解一下二維數(shù)組,包括數(shù)組的聲明和初始化2022-04-04
Spring實(shí)現(xiàn)上拉刷新和下拉加載效果
這篇文章主要為大家詳細(xì)介紹了Spring實(shí)現(xiàn)上拉刷新和下拉加載效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Java通過wait()和notifyAll()方法實(shí)現(xiàn)線程間通信
這篇文章主要為大家詳細(xì)介紹了Java通過wait()和notifyAll()方法實(shí)現(xiàn)線程間通信的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
劍指Offer之Java算法習(xí)題精講鏈表專項(xiàng)訓(xùn)練
跟著思路走,之后從簡單題入手,反復(fù)去看,做過之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化2022-03-03

