1.1使用构造器创建Bean实例
package com.jtwl.office.service;
/**
* @todo
* @author 成明俊
* @email 1047914020@qq.com
* @date 2020年1月21日
*/
public interface Person {
public void useAxe();
}
package com.jtwl.office.service.imp;
import com.jtwl.office.service.Axe;
import com.jtwl.office.service.Person;
/**
* @todo
* @author 成明俊
* @email 1047914020@qq.com
* @date 2020年1月21日
*/
public class Chinese implements Person{
private Axe axe;
/* public void setAxe(Axe axe) {
this.axe = axe;
}*/
public Chinese(){
System.out.println("Spring实例化主调Bean:Chinese实例...");
}
public void setAxe(Axe axe) {
System.out.println("Spring执行依赖关系注入");
this.axe = axe;
}
public void useAxe() {
System.out.println(axe.chop());
}
}
package com.jtwl.office.service;
/**
* @todo
* @author 成明俊
* @email 1047914020@qq.com
* @date 2020年1月21日
*/
public interface Axe {
public String chop();
}
package com.jtwl.office.service.imp;
import com.jtwl.office.service.Axe;
/**
* @todo
* @author 成明俊
* @email 1047914020@qq.com
* @date 2020年1月21日
*/
public class SteelAxe implements Axe{
public SteelAxe() {
System.out.println("Spring实例化依赖Bean:SteelAxe实例...");
}
public String chop() {
return "钢斧砍柴真快";
}
}
<bean id="chinese" class="com.jtwl.office.service.imp.Chinese">
<property name="axe" ref="steelAxe"/>
</bean>
<bean id="stonAxe" class="com.jtwl.office.service.imp.StoneAxe"/>
<bean id="steelAxe" class="com.jtwl.office.service.imp.SteelAxe"/>
package com.jtwl.office.controller;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.jtwl.office.entity.PersonService;
import com.jtwl.office.service.Person;
/**
* @todo
* @author 成明俊
* @email 1047914020@qq.com
* @date 2020年1月2日
*/
public class SpringTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
Person p = ctx.getBean("chinese", Person.class);
p.useAxe();
}
}
结果为
结论:
1.2使用静态工厂方法创建Bean
静态工厂方法所产生的产品是该接口的实例
package com.jtwl.office.service;
/**
* @todo
* @author 成明俊
* @email 1047914020@qq.com
* @date 2020年2月24日
*/
public interface Being {
//接口定义testBeing方法
public void testBeing();
}
实现类(产生这两个实现类的实例)
package com.jtwl.office.service.imp;
import com.jtwl.office.service.Being;
/**
* @todo
* @author 成明俊
* @email 1047914020@qq.com
* @date 2020年2月24日
*/
public class Dog implements Being{
private String msg;
//依赖注入时候必须的setter方法
public void setMsg(String msg) {
this.msg = msg;
}
public void testBeing() {
System.out.println(msg + "狗爱啃骨头");
}
}
返回Being实例
package com.jtwl.office.factory;
import com.jtwl.office.service.Being;
import com.jtwl.office.service.imp.Dog;
/**
* @todo
* @author 成明俊
* @email 1047914020@qq.com
* @date 2020年2月24日
*/
public class BeingFactory {
/**
* 获取Being实例的静态工厂方法
* @param arg决定返回那个Being实例的参数
*/
public static Being getBeing(String arg) {
//调用此静态方法的参数为dog,则返回Dog实例
if(arg.equalsIgnoreCase("dog")) {
return new Dog();
}else {
return new Dog();
}
}
}
配置文件
<bean id="dog" class="com.jtwl.office.factory.BeingFactory" factory-method="getBeing">
<constructor-arg value="dog"/>
<property name="msg" value="我是狗"/>
</bean>
主程序
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
Being b1 = ctx.getBean("dog",Being.class);
b1.testBeing();
结果
与构造器模式比较
1.3调用实例工厂方法创建Bean
与静态工厂区别
配置静态工厂方法时指定静态工厂类,配置实例工厂方法则指定工厂实例,Spring容器不在直接实例化该Bean,Spring容器仅仅调用实例工厂的工厂方法,工厂方法负责创建Bean实例。
实例工厂方法所产生的对象实现接口
package com.jtwl.office.service;
/**
* @todo
* @author 成明俊
* @email 1047914020@qq.com
* @date 2020年2月24日
*/
public interface Person1 {
public String sayHello(String name);
public String sayGoodBye(String name);
}
实现类
package com.jtwl.office.service.imp;
import com.jtwl.office.service.Person1;
/**
* @todo
* @author 成明俊
* @email 1047914020@qq.com
* @date 2020年2月24日
*/
public class Chinese1 implements Person1{
public String sayHello(String name) {
return name + ",你好!";
}
public String sayGoodBye(String name) {
return name + ",下次再见!";
}
}
package com.jtwl.office.service.imp;
import com.jtwl.office.service.Person1;
/**
* @todo
* @author 成明俊
* @email 1047914020@qq.com
* @date 2020年2月24日
*/
public class American implements Person1{
public String sayHello(String name) {
return name + ",Hello!";
}
public String sayGoodBye(String name) {
return name + ",Good Bye!";
}
}
实例工厂
package com.jtwl.office.factory;
import com.jtwl.office.service.Person1;
import com.jtwl.office.service.imp.American;
import com.jtwl.office.service.imp.Chinese1;
/**
* @todo
* @author 成明俊
* @email 1047914020@qq.com
* @date 2020年2月24日
*/
public class PersonFactory {
public Person1 getPerson(String ethnic) {
if(ethnic.equalsIgnoreCase("chin")) {
return new Chinese1();
}else {
return new American();
}
}
}
配置文件
<bean id="personFactory" class="com.jtwl.office.factory.PersonFactory" />
<bean id="chinese1" factory-bean="personFactory" factory-method="getPerson">
<constructor-arg value="chin"/>
</bean>
两者区别与相似