<?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"></beans>
读取配置xml文件的路径分为三种:
1,把spring的配置文件放到磁盘的目录中,
使用FileSystemXmlApplicationContext
ApplicationContext ctx = new FileSystemXmlApplicationContext("D:/data/applicationContext.xml");
2,spring的配置文件放在项目的根目录下,
配置文件放在项目的根目录下,只需要提供文件名
使用FileSystemXmlApplicationContext
ApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContext.xml");
3,spring配置文件放在类路径中
使用ClassPathXmlApplicationContext
ApplicationContext ctx = new ClassPathXmlApplicationContext(“applicationContext.xml”);
1).spring创建对象的时间:在创建容器的时候,会创建配置文件中的所有对象。
2).使用ApplicationContext的实现类为容器对象接口
3).获取容器中的对象,使用getBean(“的id”)
<!--
声明bean对象(bean对象是放入到spring的Map中)
id:对象的唯一名称, 可以使用java变量的命名规则。
class:全限定的类名(不能是接口)
-->
<bean id="someService" class="com.bjpowernode.ba01.SomeServiceImpl" />
bean的生命始末方法:spring默认使用无参数构造方法创建对象
<bean id="someService" class="com.bjpowernode.ba03.SomeServiceImpl"
init-method="setUp" destroy-method="tearDown"/>
如果需要对容器进行监控的起始情况下, init-method=为起始方法名称,在本条class类中编写好相对应的方法即可
起始方法等同于构造方法
如果需要对容器进行监控的销毁情况下, destroy-method=为销毁方法名称,在本条class类中编写好相对应的方法即可
如果需要对容器进行关闭的情况下,需要对ApplicationContext ctx 进行强制转型,再次转为容器类型,及为两种类型 ClassPathXmlApplicationContext FileSystemXmlApplicationContext 在进行调用close();即可