Spring是一个IOC(DI)和AOP容器
IOC(Inversion Of Control):意思是反转资源控制的方向,传统资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源
。而应用了IOC之后则是容器主动地将资源推送它所管理的组件,组件所做的仅是选择合适的方式接收资源。这种形式也称为查找资源的被动形式
DI(Dependency Injection):IOC的另一种表述,即组件以一些预先定义好的方式(如:set方法)接收来自容器的资源注入。
在SpringIOC容器读取bean配置创建bean实例之前,必须对它进行实例化。
Spring提供了两种类型的IOC容器实现:
1.BeanFactory:IOC容器的基本实现。
2.ApplicationContext:提供了更多的高级特性,是BeanFactory的子接口。
几乎所有场合都直接使用ApplicationContext而非底层的BeanFactory,无论使用何种方式,配置文件时相同。
ApplicationContext的主要实现类:
1.ClassPathXmlApplicationContext:类路径下加载配置文件
2.FileSystemXmlApplicationContext:文件系统中加载配置文件
3.WebApplicationContext:相对于WEB根目录路径中完成初始化工作
ApplicationContext新增的两个主要方法:refresh()和close(),让ApplicationContext具有启动、刷新和关闭上下文的能力
ApplicationContext在初始化上下文时就实例化所有单例的bean
实例化容器:
//创建springIOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//从IOC容器获取bean的实例
//HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
//利用类型返回IOC容器中的bean,但要求IOC容器中必须只能用一个该类型的bean
HelloWorld helloWorld = ctx.getBean(HelloWorld.class);
helloWorld.hello();
属性注入和构造器注入
配置文件:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<!-- bean的全类名通过反射的方式在IOC创建bean,所以bean中要有无参构造器
若id没有指定,spring自动将权限定性类名作为bean的名字 -->
<bean id="helloWorld" class="spring.beans.HelloWorld">
<!-- 属性注入 -->
<property name="name" value="spring"></property>
</bean>
<!-- 通过构造方法来配置bean的属性 -->
<bean id="car" class="spring.beans.Car">
<constructor-arg value="audi" index="0"></constructor-arg>
<constructor-arg value="30.12" index="1"></constructor-arg>
</bean>
<!-- 使用构造器注入属性可以是指定参数的位置和类型的参数,以区分重载的构造器 -->
<bean id="car2" class="spring.beans.Car">
<constructor-arg value="ca" type="String"></constructor-arg>
<constructor-arg value="133" type="int"></constructor-arg>
</bean>
</beans>
特殊字符:
<constructor-arg type="String">
<!-- 如果字面值包含特使字符可以用 <![CDATA[]]>包裹起来
value也可以使用标签进行配置-->
<value><![CDATA[<beijing>]]></value>
</constructor-arg>
需要bean能互相访问,必须在bean配置文件中指定对bean的引用
在bean的配置文件中可以通过元素或ref属性为bean的属性或构造器参数指定对bean的引用
也可以在属性或构造器里面包含bean的声明,这样的bean称为内部bean
<bean id="person" class="spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="car" ref="car2"></property>
</bean>
<bean id="person" class="spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="car" >
<ref bean="car2"/>
</property>
</bean>
<bean id="person" class="spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="car" >
<!-- 内部bean ,内部bean不能被外部引用-->
<bean class="spring.beans.Car">
<constructor-arg value="bmw" type="String"></constructor-arg>
<constructor-arg value="163" type="int"></constructor-arg>
</bean>
</property>
</bean>
可以使用专用的元素标签为bean的字符串或其他对象类型注入null值
Spring支持为级联属性赋值:
<bean id="person" class="spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="car" ref="car2"></property>
<!-- 为级联属性赋值,注意:属性需要先初始化后才可以为级联属性赋值,否则会有异常,和strus2不同 -->
<property name="car.price" value="30.00"></property>
</bean>
配置集合属性:
在Spring中可以通过内置的、、
<bean id="person2" class="spring.beans.Person">
<property name="name" value="Lisa"></property>
<!-- 配置集合属性 -->
<property name="cars">
<list>
<ref bean="car" />
<ref bean="car2" />
</list>
</property>
</bean>
Map属性配置:
<bean id="person3" class="spring.beans.Person">
<property name="name" value="Kapa"></property>
<property name="cars2">
<!-- 配置Map属性值 -->
<map>
<entry key="1" value-ref="car"></entry>
<entry key="2" value-ref="car2"></entry>
</map>
</property>
</bean>
使用定义Properties,该标签使用多个子标签,每个标签必须定义key属性
配置独立的集合bean,以供多个bean进行引用:
<!-- 需要在Namespaces中导入util -->
<util:list id="cars">
<ref bean="car"/>
<ref bean="car2" />
</util:list>
<bean id="person4" class="spring.beans.Person">
<property name="name" value="abc"></property>
<property name="cars" ref="cars"></property>
</bean>