本文主要介绍怎么通过属性注入与构造器注入实现把我们项目中要用到的数据库参数放到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