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 @   Binge-和时间做朋友  阅读(160)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示