装配方式有如下几种:一:属性注入
二:构造方法注入
三:工厂方法注入
优缺点:构造方法注入可以保证属性在bean实例化时就设置好,不需要为每个属性提供setter方法;多个构造函数让配置文件复杂;不利于继承和扩展;有时候会造成循环依赖;属性太多,构造函数复杂,可读性差;对于一个全新的应用来说不推荐使用工厂方式,因为还需要添加工厂类;所以建议使用属性注入方式;
此外,如果想要在singleton 的bean中获取 prototype 类型的bean,则需要lookup方法注入,详细见点击打开链接;如果bean的实例化过程比较复杂,通过配置就不是很方便, spring提供了FactoryBean<T>类,该接口中定义了 T getObject()方法,返回自定义实例化的bean,如果希望得到spring提供的实例化bean,则可以通过getBean("&beanId")获取;
举例:将下面三个类按照三种不同的方式装配到容器中
package com.yaspeed.spring.bean.assembly;
public class Iphone7{
private String name;
private int price;
private int contain;
public Iphone7(){
System.out.println("调用构造函数Iphone7()...");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getContain() {
return contain;
}
public void setContain(int contain) {
this.contain = contain;
}
}
package com.yaspeed.spring.bean.assembly;
public class Iphone7Plus{
private String name;
private int price;
private int contain;
public Iphone7Plus(){
System.out.println("调用构造函数Iphone7()...");
}
public Iphone7Plus(String name, int price, int contain) {
super();
this.name = name;
this.price = price;
this.contain = contain;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getContain() {
return contain;
}
public void setContain(int contain) {
this.contain = contain;
}
}
package com.yaspeed.spring.bean.assembly;
public class Iphone7s{
private String name;
private int price;
private int contain;
public Iphone7s(){
System.out.println("调用构造函数Iphone7s()...");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public int getContain() {
return contain;
}
public void setContain(int contain) {
this.contain = contain;
}
}
下面是配置文件:
<?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.xsd">
<!-- 属性装配bean -->
<bean id="iphone7" class="com.yaspeed.spring.bean.assembly.Iphone7" scope="singleton">
<property name="name" value="iphone7"></property>
<property name="price" value="7188"></property>
<property name="contain" value="128"></property>
</bean>
<!-- <bean id="iphone7" class="com.yaspeed.spring.bean.cycle.Iphone7" scope="singleton"
p:name="iphone7" p:price="7188">
</bean> -->
<!-- 构造装配bean -->
<bean id="iphone7Plus" class="com.yaspeed.spring.bean.assembly.Iphone7Plus">
<constructor-arg value="iphone7Plus" index="0" type="java.lang.String"></constructor-arg>
<!-- type不要写成java.lang.Integer -->
<constructor-arg value="7188" index="1" type="int"></constructor-arg>
<constructor-arg value="128" index="2" type="int"></constructor-arg>
</bean>
<!-- 工厂方法装备bean -->
<!-- 非静态方法装配bean -->
<bean id="phoneFactory" class="com.yaspeed.spring.bean.assembly.PhoneFactory"></bean>
<bean id="iphone7s" factory-bean="phoneFactory" factory-method="createIphone7s"></bean>
<!-- 静态方法装配bean -->
<!-- <bean id="iphone7s" class="com.yaspeed.spring.bean.assembly.PhoneFactory" factory-method="createIphone7sStatic"></bean> -->
</beans>
测试方法:
package com.yaspeed.spring.bean.assembly;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
//测试Ioc容器装配bean的几种方式
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("bean-assembly.xml");
Iphone7 iphone7 =(Iphone7)ctx.getBean("iphone7");
System.out.println(iphone7.getName());
Iphone7Plus iphone7Plus =(Iphone7Plus)ctx.getBean("iphone7Plus");
System.out.println(iphone7Plus.getName());
Iphone7s iphone7s =(Iphone7s)ctx.getBean("iphone7s");
System.out.println(iphone7s.getName());
}
}