spring xml配置(1)

本文详细介绍了Spring框架中的IoC容器概念,包括三种主要的ApplicationContext实现:FileSystemXmlApplicationContext、ClassPathXmlApplicationContext和WebXmlApplicationContext。阐述了如何通过XML配置文件定义bean,以及bean的生命周期管理、依赖注入和作用域设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

XML配置方式里面,包含.java文件和.xml文件,有点类似于android里面的代码和布局文件。xml文件负责管理组织各个容器

1.ApplicationContext

最常被使用的 ApplicationContext 接口实现:

  • FileSystemXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你需要提供给构造器 XML 文件的完整路径。

  • ClassPathXmlApplicationContext:该容器从 XML 文件中加载已被定义的 bean。在这里,你不需要提供 XML 文件的完整路径,只需正确配置 CLASSPATH 环境变量即可,因为,容器会从 CLASSPATH 中搜索 bean 配置文件。

  • WebXmlApplicationContext:该容器会在一个 web 应用程序的范围内加载在 XML 文件中已被定义的 bean。

逻辑一般是这样的

ApplicationContext context = 
				new ClassPathXmlApplicationContext("Beans.xml");
//另一种实现方法 指定文件的路径
//ApplicationContext context = new FileSystemXmlApplicationContext("d://.../beanx.xml");
helloworld obj = (helloworld) context.getBean("helloworld");
obj.getMessage();

加载bean的配置文件,然后根据context.getBean()里的id找到类,然后bean会自动的创建和管理对象。

这里容器创建对象分单例和非单例模式,单例模式中容器中只存在一个对象的实例,每次都会返回这个实例。非单例则相当于每次new ...() 一个新的实例

涉及到的去其他东西,如继承,生命周期管理和依赖注入(构造函数传参)都是写在XML里面的,代码里面都只管获取对象就行了。

 

2.XML配置

快速获取xml配置文件

new-other-spring bean configuration file 快速创建.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"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
		
	<bean id = "helloworld" class = "spring_test.helloworld"
	scope = "singleton">
	<!-- 这是注释部分   指定类成员的值
	<property name="message" value = "Hello World!"/>
	-->
	<property name="message" value = "Hello World!"/>
	</bean>
	
	<bean id = "example"  class = "spring_test.example"
	init-method = "Init"
	destroy-method = "destory">
	</bean>
	
	<bean id = "texteditor" class = "spring_test.TextEditor">
	<constructor-arg ref = "spellchecker"/>
	</bean>
	
	<bean id = "spellchecker" class = "spring_test.SpellChecker">
	</bean>
	
	<bean id = "foo" class = "spring_test.foo">
	<constructor-arg index = "0" value = "2019"/>
	<constructor-arg index = "1" value = "zhang"/>
	</bean>
</beans>

一个XML代码的示例

<bean id = "helloworld" class = "spring_test.helloworld"> 将代码里的id和类对应起来

<property name="message" value = "Hello World!"/>  则指定每一个成员变量的值

init-method = "Init" 和 destroy-method = "destory" 会指定该类中的一个方法为初始化回调(销毁回调),然后再初始化(销毁)的时候调用该方法

<constructor-arg ref = "spellchecker"/> 这里是依赖注入TextEditor创建的时候构造函数需要一个spellchecker对象的参数,将由容器自动创建

<constructor-arg index = "0" value = "2019"/>  <constructor-arg index = "1" value = "zhang"/>

也是依赖注入foo的构造函数  foo(int,string)这里index指定参数位置  value 确定值

以及在XML中所有的值都需要加“”号

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值