思路清晰
创建Bean实例的方式:
1) 通过构造器(有参或无参)
方式: <bean id="" class=""/>
2) 通过静态工厂方法
方式: <bean id="" class="工厂类" factory-method="静态工厂方法"/>
注: 工厂类实例没有创建
3) 通过实例工厂方法(非静态方法)
方式:
<bean id="factory" class="工厂类"/>
<bean id="" factory-bean="factory" factory-method="实例工厂方法"/>
注: 工厂类实例被创建
方法实用
示例1:
需求:
1 不想再bean.xml加载的时候实例化bean,而是想把加载bean.xml与实例化对象分离。
2 实现单例的bean
以上的情况,都可以通过工厂方法factory-method来创建bean。
这样再加载bean.xml时,不会直接实例化bean,而是当调用factory-method所指的方法时,才开始真正的实例化。
实现:通过spring的factory-method来创建单例的bean
首先通过静态内部类创建一个单例对象
第一种方式: 1、通过构造方法创建bean对象
bean.xml --------- spring的配置
Person接口
package dao; public interface Person { public void say(); }
ChinesePerson实现类
package dao; public class ChinesePerson implements Person{ private String msg; public ChinesePerson(String msg){ this.msg = msg; } public void say() { System.out.println(msg+" "+"打倒帝国主义"); } // public void setMsg(String msg) { // this.msg = msg; // } }
AmericanPerson实现类
package dao; public class AmericanPerson implements Person{ private String msg; public void say() { System.out.println(msg+" "+"我要返回澳大利亚"); } public void setMsg(String msg) { this.msg = msg; } }
Main测试类:
package test; import dao.ChinesePerson; import dao.Person; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); Person chinesePerson = (Person) ac.getBean("chinese"); chinesePerson.say(); Person american = (Person)ac.getBean("american"); american.say(); } }
说明
constructor-arg:通过构造函数注入。
property:通过setter对应的方法注入。
第二种方法:
2、通过静态工厂创建bean对象
bean.xml
Person接口:
package dao; public interface Person { public void say(); void setMsg(String msg); }
ChinesePerson实现类
package dao; public class ChinesePerson implements Person{ private String msg; public ChinesePerson(){} public ChinesePerson(String msg){ this.msg = msg; } public void say() { System.out.println(msg+" "+"打倒帝国主义"); } public void setMsg(String msg) { this.msg = msg; } }
American实现类
package dao; public class AmericanPerson implements Person{ private String msg; public AmericanPerson(){} public void say() { System.out.println(msg+" "+"我要返回澳大利亚"); } public void setMsg(String msg) { this.msg = msg; } }
PersonFactory:
package Factory; import dao.AmericanPerson; import dao.ChinesePerson; import dao.Person; public class PersonFactory { /** * 静态工厂创建bean对象 */ private static Person person; public static Person getPerson(String arg){ if(arg.equalsIgnoreCase("chinese")){ person = new ChinesePerson(); }else if(arg.equalsIgnoreCase("american")){ person = new AmericanPerson(); } return person; } }
TestFactory测试类:
package test; import dao.Person; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestFactory { public static void main(String[] args) { ApplicationContext ab = new ClassPathXmlApplicationContext("bean.xml"); Person chinese = (Person) ab.getBean("chinese"); chinese.say(); Person american = (Person)ab.getBean("american"); american.say(); // System.out.println(chinese==american); } }
第三种方法: 3、通过实例工厂创建bean对象:
bean.xml:
Person接口:
ChinesePerson实现类:
AmericanPerson实现类三者都没变化
PersonFactory:
package Factory; import dao.AmericanPerson; import dao.ChinesePerson; import dao.Person; public class PersonFactory { /** * 静态工厂创建bean对象 */ // private static Person person; // // public static Person getPerson(String arg){ // // // if(arg.equalsIgnoreCase("chinese")){ // // person = new ChinesePerson(); // // }else if(arg.equalsIgnoreCase("american")){ // person = new AmericanPerson(); // // } // // return person; // } /** * 通过实例工厂创建bean对象 */ private static Person person; public Person getPerson(String arg){ if(arg.equalsIgnoreCase("chinese")){ person = new ChinesePerson(); }else if(arg.equalsIgnoreCase("american")){ person = new AmericanPerson(); } return person; } }
TestFactory:
package test; import dao.Person; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TestFactory { public static void main(String[] args) { ApplicationContext ab = new ClassPathXmlApplicationContext("bean.xml"); Person chinese = (Person) ab.getBean("chinese"); chinese.setMsg("中国人"); chinese.say(); Person american = (Person) ab.getBean("american"); american.setMsg("美国人"); american.say(); } }
三者创建bean对象的测试结果都一样得
测试结果: