一. 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。