Spring IoC容器 XML 配置与加载

IoC 容器 XML 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	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-3.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 
</beans>

加载 IoC 容器 XML 配置文件

有三种方式可以加载 IoC 容器,如下:

  • ClassPathXmlApplication(相对路径)

从类路径下的 XML 配置文件中加载上下文定义,把应用上下文定义文件当做类资源

ApplicationContext ctx = new ClassPathXmlApplicationContext(“包路径/beans.xml”)

  • FileSystemXmlApplicationContext(绝对路径)

读取文件系统下的 XML 配置文件并加载上下文定义

ApplicationContext ctx = new FileSystemtXmlApplicationContext(“文件路径/beans.xml”)
  • XmlWebApplicationContext(web 项目配置路径)

读取 Web 应用下的 XML 配置文件并自动加载,无需手动写加载代码

posted @ 2020-10-19 09:25  Binge-和时间做朋友  阅读(157)  评论(0编辑  收藏  举报