这里有新鲜出炉的精品教程, 程序狗速度看过来!
Spring Framework 开源 j2ee 框架
Spring 是什么呢? 首先它是一个开源的项目, 而且目前非常活跃; 它是一个基于 IOC 和 AOP 的构架多层 j2ee 系统的框架, 但它不强迫你必须在每一层 中必须使用 Spring, 因为它模块化的很好, 允许你根据自己的需要选择使用它的某一个模块; 它实现了很优雅的 MVC, 对不同的数据访问技术提供了统一的接口, 采用 IOC 使得可以很容易的实现 bean 的装配, 提供了简洁的 AOP 并据此实现 Transcation Managment, 等等
下面小编就为大家带来一篇 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
来源: http://www.phperz.com/article/18/0314/352871.html