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

 更新时间:2017年10月20日 09:26:29   投稿:jingxian  
下面小编就为大家带来一篇spring mvc 读取xml文件数据库配置参数的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

本文主要介绍怎么通过属性注入与构造器注入实现把我们项目中要用到的数据库参数放到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文件数据库配置参数的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • springboot +rabbitmq+redis实现秒杀示例

    springboot +rabbitmq+redis实现秒杀示例

    本文主要介绍了springboot +rabbitmq+redis实现秒杀示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • jstl之map,list访问遍历以及el表达式map取值的实现

    jstl之map,list访问遍历以及el表达式map取值的实现

    下面小编就为大家带来一篇jstl之map,list访问遍历以及el表达式map取值的实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • Spring Boot构建系统安全层的步骤

    Spring Boot构建系统安全层的步骤

    这篇文章主要介绍了Spring Boot构建系统安全层的步骤,帮助大家更好的理解和学习使用Spring Boot框架,感兴趣的朋友可以了解下
    2021-04-04
  • SpringBoot集成mqtt的多模块项目配置详解

    SpringBoot集成mqtt的多模块项目配置详解

    这篇文章主要介绍了SpringBoot集成mqtt的多模块项目配置详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-04-04
  • 使用Springboot整合GridFS实现文件操作

    使用Springboot整合GridFS实现文件操作

    这篇文章主要介绍了使用Springboot整合GridFS实现文件操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-10-10
  • Java 按行读取文件按行写入文件并以空格分割字符串的方法

    Java 按行读取文件按行写入文件并以空格分割字符串的方法

    今天小编就为大家分享一篇Java 按行读取文件按行写入文件并以空格分割字符串的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Java如何获取当前年份、月份和日期字符串

    Java如何获取当前年份、月份和日期字符串

    Java获取当前年份、月份和日期是通过Calendar类的实例对象来获取的,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧
    2024-03-03
  • mybatis-plus自带QueryWrapper自定义sql实现复杂查询实例详解

    mybatis-plus自带QueryWrapper自定义sql实现复杂查询实例详解

    MyBatis-Plus是一个MyBatis(opens new window)的增强工具,在 MyBatis的基础上只做增强不做改变,MyBatis可以无损升级为MyBatis-Plus,这篇文章主要给大家介绍了关于mybatis-plus自带QueryWrapper自定义sql实现复杂查询的相关资料,需要的朋友可以参考下
    2022-10-10
  • idea打不开双击IDEA图标没反应的快速解决方案

    idea打不开双击IDEA图标没反应的快速解决方案

    这篇文章主要介绍了idea打不开双击IDEA图标没反应的快速解决方案,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-12-12
  • 基于Jenkins+Maven+Gitea+Nexus搭建CICD环境的方式

    基于Jenkins+Maven+Gitea+Nexus搭建CICD环境的方式

    这篇文章主要介绍了基于Jenkins+Maven+Gitea+Nexus从0到1搭建CICD环境,大家都知道Nexus是一套“开箱即用”的系统不需要数据库,它使用文件系统加Lucene来组织数据,需要的朋友可以参考下
    2022-01-01

最新评论