Spring Framework Learning Notes (ix)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.