Configuring Beans Through Factory methods
Gets the bean by pointing to the full class name of the static factory, and Factory-method, passing in parameters.
Configured to:
<bean id="schoolbean"class=" Com.pfSoft.beanFactory.myFactory "factory-method="getschool"> <constructor-arg value="wuda"></constructor-arg> </ Bean>
New Static factory class:
public class MyFactory {private static map<string, school> schools=new hashmap<string, school> (); static{ Schools.put ("Wuda", New School ("Wuhan University", "East Lake shore of Wuhan")), Schools.put ("Qinghua", New School ("Tsinghua", "Haidian Huang Zhuang")); public static School Getschool (String schoolname) {return schools.get (schoolname);}}
New Bean:school
public class School {private string Schoolname;private string address;/** * * @return the Schoolname */public string g Etschoolname () {return schoolname;} /** * @param schoolname the schoolname to set */public void Setschoolname (String schoolname) {this.schoolname = Schoolname ;} /** * * @return The address */public String getaddress () {return address;} /** * @param address the address to set */public void setaddress (String address) {this.address = address;} Public School (String schoolname, string address) {super (); this.schoolname = schoolname;this.address = Address;} /* (non-javadoc) * @see java.lang.object#tostring () */@Overridepublic String toString () {return ' School [schoolname= ' + SC Hoolname + ", address=" + address+ "]";}}
Test code:
Private ApplicationContext ctx; @Beforepublic void init () {ctx=new classpathxmlapplicationcontext (" Spring-beanfactory.xml ");} @Testpublic void Testbeanstaticfactory () {School school= (School) Ctx.getbean ("Schoolbean"); System.out.println (School.tostring ());}
Output: School [schoolname= Wuhan University, address= East Lake of Wuhan]
Instance Factory method
Unlike the static factory method, you want to invoke instances of the bean that need to be created first to create an instance of the factory itself.
In this example, you need to configure an instance of the factory first.
The configuration is as follows:
<bean id= instancefactorybean " class = Com.pfSoft.beanFactory.InstanceFactory ></bean> <bean ID = " " factory-bean=" instancefactorybean factory-method=" getschool > <constructor-arg value=" qinghua " ></ Constructor-arg> </bean>
The instance factory code is as follows:
Public classInstancefactory {PrivateMap<string, school> schools=NewHashmap<string, school>(); Publicinstancefactory () {Schools.put ("Wuda",NewSchool ("Wuhan University","Wuhan East Lake Shore")); Schools.put ("Qinghua",NewSchool ("Tsinghua","Haidian Huang Zhuang")); } PublicSchool Getschool (String schoolname) {returnSchools.Get(Schoolname); }}
The test code is as follows:
@Test publicvoid testbeanstaticfactory () { School School= (School ) Ctx.getbean ("schoolbean"); School school2= (School) ctx.getbean ("schoolBean2"); System. out . println (School.tostring ()); System. out . println (School2.tostring ()); }
The output is:
School [Schoolname= Wuhan University, address= Wuhan East Lake Shore]school [Schoolname= Tsinghua, address= Haidian Huang Zhuang]
You can see that School2 was created by the instance factory.
Spring Framework Learning Notes (ix)