spring容器
- 1、在springIOC 容器读取 Bean 配置,创建 Bean 实例之前,需要先对spring 容器进行实例化。只有在容器实例化之后,才可以 IOC 容器中获取 Bean 实例,并且使用。
- 2、spring提供了两种类型的springIOC容器实现。
- BeanFactory: IOC容器的底层实现。
- ApplicationContext: 提供了更多得高级特性,是BeanFactory的子接口。
- BeanFactory是Spring框架的基础设施,面向spring本身。ApplicationContext面向使用Spring框架的开发者,几乎所有的应用场合都是使用ApplicationContext,而不是BeanFactory.
- 无论使用何种方式,配置文件都相同。
- 3、spring容器接口ApplicationContext
- ConfigurableApplicationContext 扩展于 ApplicationContext, 新增两个主要方法:refresh(),close(),让ApplicationContext具有启动,刷新和关闭上下文的能力。
- ClassPathXmlApplicationContext: 实现 ConfigurableApplicationContext接口,从类路径下加载配置文件。
- FileSystemXmlApplicationContext:实现 ConfigurableApplicationContext接口,从文件系统中加载配置文件。
- ApplicationContext 在初始化上下文时就实例化所有单例的Bean。
- WebApplicationContext 是专门为Web应用而准备的,它允许从相对于WEB根目录的路径中完成初始化工作。
# 1.创建iOC容器,并且读取配置文件
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
# 2.从容器中获取bean
HelloWorld helloWorld = (HelloWorld) applicationContext.getBean("helloworld");
# 3.获取bean的属性
helloWorld.hello();
关于xml中的文件头
- xmlns : 表示默认的命名空间,对于默认的namespace中的元素,可以不使用前缀。
- xmlns:xsi : 表示使用xsi作为前缀的命名空间,前缀xsi需要在文档中声明。
- xsi:schemaLocation : 表示是namespace为xxx的schemaLocation属性。它定义了xml Namespace和对应的XSD(xml schema Defination)文档的位置的关系。由多个URI组成,由空格或者换行分隔。第一个URI是定义xml Namespace的值,第二个URI给出schema文档的位置,schema处理器将从这个位置读取schema文档,该文档的targetNamespace必须与第一个URI相匹配。
<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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.1.xsd">
一、配置Bean,以及从spring容器中获取Bean
# 一个 <bean> 标签就表示一个bean对象,至少要含有id,class属性。
# id 表示 bean 对象的名称。 class 表示 bean 的类型。
<bean id="xxx" class="xxxxx"></bean>
#在容器中,通过 id 获取 bean。
Object bean = contex.getBean("id");
#在容器中,通过 class 获取 bean。
#通过 class 类型获取 bean ,需要保证 IOC 容器中,该类型实例只存在一个。
Object bean = context.getBean("Hello.class");
二、注入Bean属性
#<constructor-arg> 标签来设置构造器注入
#value属性: 表示基础类型/string注入。
#ref属性: 表示对象类型注入。
#index属性: 表示参数的索引位置。
#type属性: 表示参数的类型.
<bean id="car" class="com.mz.Car">
#<constructor-arg></constructor-arg>标签进行构造器方法注入
<constructor-arg value="BYD" index="0" type="float"></constructor-arg>
<constructor-arg value="1200" index="1" type="java.lang.String"></constructor-arg>
</bean>
- 2、通过 setter 方法配置 bean 的属性。
#<property>标签来设置 setter 方法注入。
#使用 <ref> 标签或者 ref 属性进行注入属性对象。
<bean id="person" class="com.mz.Person">
<property name="name" value="meng"></property>
<property name="age" value="20"></property>
<property name="car" ref="car"></property>
</bean>
- 3、关于字面值,以及特殊字符的注入 value属性
- 字面值:可以用字符串表示的值,可以通过 标签或者 value 属性进行注入。
- 基本数据类型及其封装类, String 等类型都可以采取字面值注入方式。
- 若字面值中包含特殊字符,可以使用 <![CDATA[]]> 把字面值包裹起来 。
<bean id="car1" class="com.mz.Car">
<property name="name">
<value><![CDATA[shanghai&&]]></</