spring mvc 读取xml文件数据库配置参数的方法

所属分类: 软件编程 / java 阅读数: 15
收藏 0 赞 0 分享

本文主要介绍怎么通过属性注入与构造器注入实现把我们项目中要用到的数据库参数放到xml文件里面去,方便部署。

spring mvc 4.2.6项目

SQL Server 2008数据库

本文介绍的主要使用ApplicationContext以及其实现类实现。主要用到的是ClassPathXmlApplicationContext。

ClassPathXmlApplicationContext:从类路径ClassPath中寻找指定的XML配置文件,找到并装载

完成ApplicationContext的实例化工作。例如:

//装载单个配置文件实例化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

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文件

文件内容如下

<?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");
 //根据bean节点的标识获取对象,id
 DBParaProperty dbParaProperty = (DBParaProperty) cpxac.getBean("DBParaProperty");
 System.out.println(dbParaProperty.getSqlServerUserName());

 return dbParaProperty.getSqlServerUserName();
 }
}

二、构造器注入

通过构造方法注入Bean 的属性值或依赖的对象,它保证了 Bean 实例在实例化后就可以使用。构造器注入在 元素里声明属性。

步骤如下:

1、创建DBParaConstructor类

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文件。

<?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;
 }
}

项目目录如下:

关于那个路径的,Java会把java文件编译成.class文件放到classes目录下,这个也是项目Java代码运行的根目录。所以当你把xml文件放在src下面的时候,可以直接写文件名就可以找到了,但是如果你把它放在其他的目录下面了,要把路径写好,例如:/test/xxx.xml。

以上这篇spring mvc 读取xml文件数据库配置参数的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Java数据类型的规则

这篇文章主要介绍了Java数据类型的规则的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Spring整合TimerTask实现定时任务调度

这篇文章主要介绍了Spring整合TimerTask实现定时任务调度的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解SpringMVC使用MultipartFile实现文件的上传

本篇文章主要介绍了SpringMVC使用MultipartFile实现文件的上传,本地的文件上传到资源服务器上,比较好的办法就是通过ftp上传。这里是结合SpringMVC+ftp的形式上传的,有兴趣的可以了解一下。
收藏 0 赞 0 分享

SpringMVC上传文件的三种实现方式

本篇文章主要介绍了SpringMVC上传文件的三种实现方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例

本篇文章主要介绍了微信公众帐号开发-自定义菜单的创建及菜单事件响应的实例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
收藏 0 赞 0 分享

浅析Java中的继承与组合

本文将介绍组合和继承的概念及区别,并从多方面分析在写代码时如何进行选择。文中通过示例代码介绍的很详细,有需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

利用反射获取Java类中的静态变量名及变量值的简单实例

下面小编就为大家带来一篇利用反射获取Java类中的静态变量名及变量值的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

java启动线程的3种方式对比分析

这篇文章主要为大家对比分析了java启动线程的3种方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringMVC上传和解析Excel方法

这篇文章主要介绍了SpringMVC上传和解析Excel方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

JAVA中String类与StringBuffer类的区别

这篇文章主要为大家详细介绍了JAVA中String类与StringBuffer类的区别,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多