一. Spring 自動裝配(AutoWire)
實體bean
public class People {
private int id;
private String name;
private int age;
private Dog dog;
......
public People(Dog dog) {
this.dog = dog;
}
......
}
- 通過配置 default-autowire 屬性,Spring IoC 容器可以自動為程式注入 bean。預設是 no,不啟用自動裝配機制。
<?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"
default-autowire="byName">
- default-autowire 的類型
- byName:通過名稱進行自動比對;
<?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"
default-autowire="byName">
根據bean中的屬性名(dog)來自動裝配
<bean id="dog" class="com.bee.entity.Dog">
<property name="name" value="Jack"></property>
</bean>
bean中的屬性名(dog)不比對,不自動裝配之。
<bean id="dog2" class="com.bee.entity.Dog">
<property name="name" value="Tom"></property>
</bean>
<bean id="people" class="com.bee.entity.People">
<property name="id" value="1"></property>
<property name="name" value="張麻子"></property>
<property name="age" value="11"></property>
</bean>
</beans>
- byType:根據類型進行自動比對;
<?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"
default-autowire="byType">
根據bean中的屬性類型(Dog)來自動裝配
<bean id="dog" class="com.bee.entity.Dog">
<property name="name" value="Jack"></property>
</bean>
如果保留下邊的配置,那麼bean中的屬性類型(Dog)在本配置檔案中就有多個比對,Spring将報錯。
<!--
<bean id="dog2" class="com.bee.entity.Dog">
<property name="name" value="Tom"></property>
</bean>
-->
<bean id="people" class="com.bee.entity.People">
<property name="id" value="1"></property>
<property name="name" value="張麻子"></property>
<property name="age" value="11"></property>
</bean>
</beans>
- constructor:和 byType 類似,隻不過它是根據構造方法注入而言的,根據類型,自動注入;
<?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"
default-autowire="constructor">
根據bean中的與屬性相關的構造方法(public People(Dog dog) {})來自動裝配(還需參考參數類型)
<bean id="dog" class="com.bee.entity.Dog">
<property name="name" value="Jack"></property>
</bean>
<bean id="people" class="com.bee.entity.People">
<property name="id" value="1"></property>
<property name="name" value="張麻子"></property>
<property name="age" value="11"></property>
</bean>
</beans>
- 建議:自動裝配機制慎用,它屏蔽了裝配細節,容易産生潛在的錯誤。
二. 方法注入
Spring bean 作用域預設是單例 singleton,可以通過配置prototype實作多執行個體。
方法注入需使用關鍵字lookup-method。
<?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">
<bean id="dog" class="com.bee.entity.Dog" scope="prototype">
<property name="name" value="Jack"></property>
</bean>
<bean id="people1" class="com.bee.entity.People">
<property name="id" value="1"></property>
<property name="name" value="張麻子"></property>
<property name="age" value="18"></property>
<lookup-method name="getDog" bean="dog"/>
</bean>
</beans>
實體bean
public abstract class People {
private int id;
private String name;
private int age;
private Dog dog;
......
public abstract Dog getDog();
......
}
三. 方法替換
public class People {
private int id;
private String name;
private int age;
private Dog dog;
......
public Dog getDog() {
Dog dog=new Dog();
dog.setName("Jack");
return dog;
}
}
public class People2 implements MethodReplacer {
@Override
public Object reimplement(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
Dog dog=new Dog();
dog.setName("Tom");
return dog;
}
}
<?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">
<bean id="people" class="com.bee.entity.People">
<property name="id" value="1"></property>
<property name="name" value="張麻子"></property>
<property name="age" value="28"></property>
<replaced-method name="getDog" replacer="people2"></replaced-method>
</bean>
<bean id="people2" class="com.bee.entity.People2"></bean>
</beans>
四. bean 之間的關系
<?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">
<bean id="weapon" class="com.bee.entity.Weapon">
<property name="name" value="血滴子"></property>
</bean>
<bean id="spider" class="com.bee.entity.Spider" abstract="true">
<property name="ablility" value="蛛絲"></property>
<property name="energy" value="90"></property>
</bean>
繼承——parent 依賴——depends-on
<bean id="spiderMan" parent="spider" depends-on="autority">
<property name="id" value="1"></property>
<property name="name" value="張麻子"></property>
</bean>
<bean id="spiderGirl" parent="spider">
<property name="id" value="2"></property>
<property name="name" value="王二丫"></property>
<property name="energy" value="100"></property> 重寫屬性
<property name="weapon" ref="weapon"></property> 引用——ref
</bean>
<bean id="autority" class="com.bee.service.Authority"></bean>
</beans>
五. bean 作用範圍
- singleton Spring IoC 容器中僅有一個 Bean 執行個體,Bean 以單例的方式存在。
- prototype 每次從容器中調用 Bean 時,都傳回一個新的執行個體。
- request 每次 HTTP 請求都會建立一個新的 Bean。
- session 同一個 HTTP Session 共享一個 Bean。
- global session 同一個全局 Session 共享一個 Bean,一般用于 Portlet 應用環境。
- application 同一個 Application 共享一個 Bean。