1、基本定义
IOC: 其思想是反转资源获取的方向,传统的资源查找方式要求组件向容器发起请求查找资源,作为回应,容器适时的返回资源,而应用了
IOC之后,容器主动将资源推送给它所管理的组件,组件索要做的只是选择一种合适的方式接受资源,这种行为也被称为查找的被动形式。
DI: IOC 的另一种表述方式,即组件以一些预定好的方式(例如Action中属性的setter方法)接受来自容器的资源注入
2、原理
3、Bean的配置
Bean:通过全类名(反射)、通过工厂方法(静态工厂方法&实例工厂方法)、FactoryBean
IOC容器:BeanFactory& ApplicationContext 概述
依赖注入的方式:属性注入、构造器注入
3.1、IOC容器的实现
Spring提供了两种类型的IOC容器
BeanFactory : IOC容器的基本实现,面向Spring本身。
ApplicationContext : 提供了 更多的高级特性,是BeanFactory的子接口,面向开发者。
主要实现类 {ClassPathXmlApplicationContext : 从类路径下加载文件,FileSystemXmlApplicationContext : 从文件系统中加载配置文件}
//1、创建spring 的 ioc 容器,配置文件在类路径下
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml"); //2、ioc 容器中获取 Bean实例,利用id 定位到IOC容器中的bean
HelloWorld helloWorld = (HelloWorld)ctx.getBean("helloWorld");
<bean id="helloWorld" class="com.spring.beans.HelloWorld">
<property name="name" value="spring"></property>
</bean>
3.2、依赖注入的方式
3.2.1 属性注入<property>
通过setter方法注入Bean的属性值或者依赖的对象
3.2.2 构造器注入<constructor-arg>
通过构造方法注入Bean的属性值或依赖的对象,保证了Bean实例在实例化后就可以使用<constructor-arg>
4、bean属性值详解
4.1、出现特殊字符时:
<constructor-arg>
<value><![CDATA[<Audi>]]></value>
</constructor-arg>
4.2、ref 建立bean之间的引用关系 :
<bean id="car_ref" class="com.spring.beans.Car">
<constructor-arg>
<value><![CDATA[<Audi>]]></value>
</constructor-arg>
<constructor-arg value="ShangHai"></constructor-arg>
<constructor-arg value="300000"></constructor-arg>
</bean> <bean id="person" class="com.spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="car" ref="car_ref"></property>
</bean>
4.3、内部bean,无法被外部bean引用
<bean id="person" class="com.spring.beans.Person">
<property name="name" value="Tom"></property>
<property name="age" value="24"></property>
<property name="car">
<bean class="com.spring.beans.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="ChangAn"></constructor-arg>
<constructor-arg value="20000"></constructor-arg>
</bean>
</property>
</bean>
4.4、null值和级联属性
null值
<bean id="person2" class="com.spring.beans.Person">
<constructor-arg value="jerry"></constructor-arg>
<constructor-arg value="23"></constructor-arg>
<constructor-arg><null/></constructor-arg>
</bean>
级联属性
<bean id="car_ref" class="com.spring.beans.Car">
<constructor-arg>
<value><![CDATA[<Audi>]]></value>
</constructor-arg>
<constructor-arg value="ShangHai"></constructor-arg>
<constructor-arg value="300000"></constructor-arg>
</bean> <bean id="person2" class="com.spring.beans.Person">
<constructor-arg value="jerry"></constructor-arg>
<constructor-arg value="23"></constructor-arg>
<constructor-arg ref="car_ref"></constructor-arg>
//必须在bean文件中,定义maxSpeed的set方法【属性注入!】
<property name="car.maxSpeed"></property>
</bean>
集合属性
<bean id="car_ref" class="com.spring.beans.collections.Car">
<constructor-arg>
<value><![CDATA[<Audi>]]></value>
</constructor-arg>
<constructor-arg value="ShangHai"></constructor-arg>
<constructor-arg value="300000"></constructor-arg>
</bean> <bean id="car2_ref" class="com.spring.beans.collections.Car">
<constructor-arg value="Ford"></constructor-arg>
<constructor-arg value="beijing"></constructor-arg>
<constructor-arg value="20000"></constructor-arg>
</bean> <bean id="car3_ref" class="com.spring.beans.collections.Car">
<constructor-arg value="Honda"></constructor-arg>
<constructor-arg value="beijing"></constructor-arg>
<constructor-arg value="120000"></constructor-arg>
</bean> <!-- 测试如何配置集合属性 -->
<bean id="testCollections" class="com.spring.beans.collections.Person" >
<property name="name" value="Mike"></property>
<property name="age" value="27"></property>
<property name="cars">
<list>
<ref bean="car_ref"></ref>
<ref bean="car2_ref"></ref>
<ref bean="car3_ref"></ref>
</list>
</property>
</bean>
package com.spring.beans.collections; import java.util.List; public class Person {
private String name; private int age; private List<Car> cars; public Person(String name, int age, List<Car> cars) {
super();
this.name = name;
this.age = age;
this.cars = cars;
} @Override
public String toString() {
return "Person [age=" + age + ", cars=" + getCarStr(cars) + ", name=" + name + "]";
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public void setAge(int age) {
this.age = age;
} public Person() { } public List<Car> getCars() {
return cars;
} public void setCars(List<Car> cars) {
this.cars = cars;
} //获取汽车
public String getCarStr(List<Car> cars)
{
StringBuffer sb = new StringBuffer();
sb.append("[");
for(Car car :cars){
sb.append("brand: "+car.getBrand()+ "corp:"+ car.getCorp() +"maxSpeed:" +car.getMaxSpeed()+"price:"+car.getPrice()+"\n");
}
sb.append("]");
return sb.toString();
}
}
package com.spring.beans.collections; public class Car {
private String brand;
private String corp;
private String price;
private String maxSpeed; //通过构造器注入
public Car(String brand, String corp, String price) {
super();
this.brand = brand;
this.corp = corp;
this.price = price;
} public String getBrand() {
return brand;
} public void setBrand(String brand) {
this.brand = brand;
} public String getCorp() {
return corp;
} public void setCorp(String corp) {
this.corp = corp;
} public String getPrice() {
return price;
} public void setPrice(String price) {
this.price = price;
} public String getMaxSpeed() {
return maxSpeed;
} public void setMaxSpeed(String maxSpeed) {
this.maxSpeed = maxSpeed;
} }
集合属性-2 【map】
<bean id="testMap" class="com.spring.beans.collections.newPerson">
<property name="name" value="java"></property>
<property name="age" value="30"></property>
<property name="cars">
<map>
<entry key="A1" value-ref="car_ref"></entry>
<entry key="A2" value-ref="car2_ref"></entry>
<entry key="A3" value-ref="car3_ref"></entry>
</map>
</property>
</bean>
集合属性-3【properties】
<bean id="dataSource" class="com.spring.beans.collections.DataSource">
<property name="properties">
<props>
<prop key="user">root</prop>
<prop key="password">root</prop>
<prop key="jdbcUrl">jdbc:mysql:///test</prop>
<prop key="driverClass">com.mysql.jdbc.drivce</prop>
</props>
</property>
</bean>
p标签
<bean id="person5" class="com.spring.beans.collections.Person"
p:name="queen" p:age="30" p:cars-ref="cars"> </bean>
配置单例的集合bean
<!-- 配置单例的集合bean,以供多个bean进行引用,需要导入util命名空间 -->
<util:list id="cars">
<ref bean="car_ref"></ref>
<ref bean="car2_ref"></ref>
<ref bean="car3_ref"></ref>
</util:list> <bean id="person4" class="com.spring.beans.collections.Person">
<property name="name" value="jack"></property>
<property name="age" value="30"></property>
<property name="cars" ref="cars"></property>
</bean>