Spring框架中如何读取Spring的XML配置文件

来源:互联网 发布:软件开发流程图绘制 编辑:程序博客网 时间:2024/06/07 01:24


今天我们来说说Spring框架中如何读取Spring的XML配置文件,通常Spring的配置文件是applicationContext.xml 或者 spring-servlet.xml 也可能是自定义的XML配置文件。我们通常将这些文件放在/

项目名称/WEB-INF/目录下,比如:
/SpringTest/WEB-INF/applicationContext.xml
/SpringTest/WEB-INF/spring-servlet.xml

最常见的方法是:通过ClassPathXmlApplicationContext这个类来获取,该类位于org.springframework.context.support包下。网上有很多例子,我主要说一下路径问题,关于这个路径,网上的大部分版本,都是直接这样写:



//spring-servlet.xml是我的SpringMVC的配置文件,位于项目的/SpringTest/WEB-INF/spring-servlet.xml目录下ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-servlet.xml"); 

很遗憾,这样做,从我的测试来看,是读取不到这个配置文件的,会报错,根本原因是找不到这个文件
java.io.FileNotFoundException: class path resource [webTest.xml] cannot be opened because it does not exist

我测试了一下,通过ClassPathXmlApplicationContext这个类,默认是加载E:\myeclipse\myeclipse10InstallFile\Spring\WebRoot\WEB-INF\classes\,也就是你的硬盘上的这个位置的配置文件,所以直接写ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-servlet.xml"); 是加载不到的。

我们在MyEclipse中看到的目录,和硬盘上的目录结构是不一样的,E:\myeclipse\myeclipse10InstallFile\Spring\WebRoot\WEB-INF\classes\目录下是编译以后的java文件,如果你把spring-servlet.xml文件,复制到这个目录下,那么你再运行这段代码,是可以加载到这个配置文件的,

<span style="font-size:14px;">ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-servlet.xml"); </span>


但是这显然不是我们想要的结果,那该怎么办呢,我是通过这种方法来做的,ClassPathXmlApplicationContext默认加载的不是E:\myeclipse\myeclipse10InstallFile\Spring\WebRoot\WEB-INF\classes\目录下的配置文件吗,那我们就进入classes的上一级目录,不就是WEB-INF了吗,而WEB-INF正是我们的配置文件所在的目录,代码如下:

<span style="font-size:14px;">ApplicationContext applicationContext = new ClassPathXmlApplicationContext("../spring-servlet.xml");</span>

经过测试,这种方法是可行的。可以加载到我们的配置文件。

然后我们通过
applicationContext.getBean("dataSource");
就可以获得id为dataSource的bean,运行结果如下:
com.spring.dynamic_datasource.DynamicDataSource@1517e5e

有一点需要说明的是,这种方法,我们是读取的Spring框架的配置文件,是没有办法读取web.xml的配置文件的。web.xml和Spring的配置文件是不同的。下面我们来对比一下:

spring-servlet.xml


<?xml version="1.0" encoding="UTF-8"?>  <!-- Bean头部 -->  <beans xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:p="http://www.springframework.org/schema/p"      xmlns:mvc="http://www.springframework.org/schema/mvc"      xmlns:context="http://www.springframework.org/schema/context"      xmlns:util="http://www.springframework.org/schema/util"      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd              http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">   <!-- 配置多数据源 连接MySQL数据库中的2个不同的数据库 -->    <bean id="dataSource" class="com.spring.dynamic_datasource.DynamicDataSource">        <property name="targetDataSources">            <map key-type="java.lang.String">                <entry value-ref="datasource_test" key="datasource_test"></entry>                <entry value-ref="datasource_learn_system" key="datasource_learn_system"></entry>            </map>        </property>        <property name="defaultTargetDataSource" ref="datasource_learn_system"></property></bean>   </beans> 

web.xml


<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"><!--  配置Spring MVC核心控制器 DispatcherServlet 和 URL --> <servlet>  <servlet-name>spring</servlet-name>   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> </servlet><servlet-mapping>  <servlet-name>spring</servlet-name>   <url-pattern>*.action</url-pattern> </servlet-mapping></web-app>

spring-servlet.xml 的根元素是<beans></beans>
web.xml 的根元素是<web-app></web-app>

当然最根本的不同,来自命名空间,也就是这部分:


<beans xmlns="http://www.springframework.org/schema/beans"      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"      xmlns:p="http://www.springframework.org/schema/p"      xmlns:mvc="http://www.springframework.org/schema/mvc"      xmlns:context="http://www.springframework.org/schema/context"      xmlns:util="http://www.springframework.org/schema/util"      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd              http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd              http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd              http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> 

这是Spring的配置文件的规范,如果你把一个XXX.xml的xml文件的规范定义成这样,Spring容器能自动识别,把该文件显示成Spring配置文件特有的标识。

好了,今天就说到这里。


0 0
原创粉丝点击