IOC
IOC(Inversion of Control):其思想是反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源. 作为回应, 容器适时的返回资源. 而应用了 IOC 之后, 则是容器主动地将资源推送给它所管理的组件, 组件所要做的仅是选择一种合适的方式来接受资源. 这种行为也被称为查找的被动形式
DI
DI(Dependency Injection) — IOC 的另一种表述方式:即组件以一些预先定义好的方式(例如: setter 方法)接受来自如容器的资源注入. 相对于 IOC 而言,这种表述更直接
总结
IOC Inversion of Control,注意是名词Control,本来控制是顺着来的,要什么搞什么,但是Inverse之后,就可以把控制扭转过来,意思就是那样...
一、Bean<----配置文件的配置方法(两种)
1、利用属性名赋值
<bean id="helloWorld" class="top.biglin.HelloWorld">
<property name="name" value="Spring"></property>
</bean>
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void hello(){
System.out.println("Hello " + name);
}
}
2、利用构造器赋值
<bean id="car" class="top.biglin.Car">
<constructor-arg value="Audi"></constructor-arg>
<constructor-arg value="ShangHai"></constructor-arg>
<constructor-arg value="300000"></constructor-arg>
</bean>
package top.biglin;
public class Car {
private String brand;
private String corp;
private double price;
private int maxSpeed;
public Car(String brand, String corp, double price) {
this.brand = brand;
this.corp = corp;
this.price = price;
}
public Car(String brand, String corp, int maxSpeed) {
this.brand = brand;
this.corp = corp;
this.maxSpeed = maxSpeed;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", corp='" + corp + '\'' +
", price=" + price +
", maxSpeed=" + maxSpeed +
'}';
}
}
二、引用其他bean的方法(两种)
使用ref
<!-- Use 'ref' refrence the other javabeans -->
<bean id="person" class="top.biglin.Person">
<property name="name" value="Tome" ></property>
<property name="age" value="24"></property>
<property name="car" ref="car2"></property>
</bean>
使用内部bean
<bean id="car2" class="top.biglin.Car">
<constructor-arg value="Baoma" type="java.lang.String"></constructor-arg>
<constructor-arg value="ShangHai" type="java.lang.String"></constructor-arg>
<constructor-arg value="240" type="int"></constructor-arg>
</bean>
<bean id="person2" class="top.biglin.Person">
<property name="name" value="Tom" ></property>
<property name="age" value="24"></property>
<property name="car" >
<bean class="top.biglin.Car">
<constructor-arg value="Benz"></constructor-arg>
<constructor-arg value="ShangHai"></constructor-arg>
<constructor-arg value="250" type="int"></constructor-arg>
</bean>
</property>
</bean>
三、特殊字符如何处理
<!--特殊字符用<![CDATA[ special Strings ]]>-->
<bean id="car3" class="top.biglin.Car">
<constructor-arg type="java.lang.String">
<value><![CDATA[<!hh]]></value>
</constructor-arg>
<constructor-arg value="ShangHai" type="java.lang.String"></constructor-arg>
<constructor-arg value="240" type="int"></constructor-arg>
</bean>
注意:内部bean无法获取并使用
给元素赋null值<null></null>或<null />
<bean id="person3" class="top.biglin.Person">
<property name="name" value="Tom" ></property>
<property name="age" value="24"></property>
<property name="car" >
<null></null>
</property>
</bean>
四、级联赋值
<!-- Use the Constructor 赋值,not point the type ,that will leads the error when you have two or more beans of same class -->
<bean id="car" class="top.biglin.Car">
<constructor-arg value="Audi"></constructor-arg>
<constructor-arg value="ShangHai"></constructor-arg>
<constructor-arg value="300000"></constructor-arg>
</bean>
<bean id="person4" class="top.biglin.Person">
<property name="name" value="Tom" ></property>
<property name="age" value="24"></property>
<property name="car" ref="car"></property>
<!--Car 类要有setter and getter-->
<property name="car.maxSpeed" value="300"></property>
</bean>
注意:级联属性赋值要在bean先初始化以后才可以
五、集合赋值
<!--配置集合属性-->
<bean id="person6" class="top.biglin.collection.Person">
<property name="name" value="Bob"></property>
<property name="age" value="18"></property>
<property name="car">
<list>
<ref bean="car"/>
<ref bean="car2"/>
<ref bean="car3"/>
</list>
</property>
</bean>
<set>和<map>与<list>类似
使用Map节点(<entry></entry>)
<!--配置Map属性值-->
<bean id="newPerson" class="top.biglin.collection.NewPerson">
<property name="age" value="88"></property>
<property name="name" value="linjiawei"></property>
<property name="car">
<map>
<entry key="AA" value-ref="car11"></entry>
<entry key="BB" value-ref="car12"></entry>
<entry key="CC" value-ref="car13"></entry>
</map>
</property>
</bean>
配置Properties,Properties是Map的一个实现类
<!-- 配置Properties-->
<bean id="dataSource" class="top.biglin.collection.DataSource">
<property name="properties">
<props>
<prop key="username">root</prop>
<prop key="password">123456</prop>
<prop key="jdbcUrl">jdbc:mysql://test</prop>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
</props>
</property>
</bean>
配置 Configure 独立的集合bean
<!-- 配置独立的集合bean,Make it could be quote for other beans
,this required to import the util name spacing
-->
<util:list id="cars2">
<ref bean="car11" />
<ref bean="car12" />
</util:list>
<bean id="person5" class="top.biglin.collection.Person">
<property name="name" value="Jack"></property>
<property name="age" value="88"/>
<property name="car" ref="cars2"></property>
</bean>
六 使用p命名空间(在beans中导入 xmlns:p="http://www.springframework.org/schema/p")
<!-- Use p name space 使用p命名空间-->
<bean id="person7" class="top.biglin.collection.Person"
p:age="53" p:name="person7" p:car-ref="cars2"
></bean>