Fetch Bean Instantiation object in spring through static factory method

Source: Internet
Author: User
Tags constructor xmlns

1:bean

Package Com.study.spel;


public class Car {

Private String brand;
Private String Corp;
Private double typeperimeter;//tyre circumference
Public Car () {
System.out.println ("Car ' s Constructor ...");
}

Public Car (string brand, String corp, double Typeperimeter) {
Super ();
This.brand = brand;
This.corp = corp;
This.typeperimeter = Typeperimeter;
}


Public String Getbrand () {
return brand;
}


Public double Gettypeperimeter () {
return typeperimeter;
}


public void Settypeperimeter (double typeperimeter) {
This.typeperimeter = Typeperimeter;
}


public void Setbrand (String brand) {
This.brand = brand;
}


Public String Getcorp () {
Return Corp;
}


public void Setcorp (String Corp) {
This.corp = corp;
}


Public Car (string brand, String corp) {
Super ();
This.brand = brand;
This.corp = corp;
}


@Override
Public String toString () {
Return "Car [brand=" + Brand + ", corp=" + corp + ", typeperimeter="
+ Typeperimeter + "]";
}

}

2;bean

<?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:context= "Http://www.springframework.org/schema/context"
Xsi:schemalocation= "Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-4.0.xsd ">
<!--Configure the Bean with a static factory method, note that instead of configuring a static factory method instance, configure the bean instance--
<!--class Properties: A full-class name that points to a static factory method, Factory-method: The name of the static factory Method--
<!--Constructor-arg: To pass parameters to the factory method--
<bean id= "Cara" class= "Com.study.spel.StaticFactory"
factory-method= "Getcar" >
<constructor-arg value= "Audi" ></constructor-arg>
</bean>
</beans>


3: Static Factory method

Package Com.study.spel;


Import Java.util.HashMap;
Import Java.util.Map;


/**
* Static Factory Method: A static method that invokes a class directly can return an instance of a bean
* @author Administrator
*
*/
public class Staticfactory {
private static map<string,car> cars=new hashmap<string,car> ();
Static
{
Cars.put ("Audi", New Car ("Audi", "3000000", 110.0));
Cars.put ("Ford", New Car ("Ford", "2000000", 210.0));
Cars.put ("Didi", New Car ("Didi", "1000000", 310.0));
}
Static Factory method
public static Car Getcar (String name)
{
return Cars.get (name);
}
}


4:test

Package Com.study.spel;


Import org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;


Import Com.study.spel.Car;


public class Testmain {
  public static void main (string[] args) {
 applicationcontext ctx=new C Lasspathxmlapplicationcontext ("Beans-factory.xml");
Car car1= (car) ctx.getbean ("Cara");
System.out.println (CAR1);
}

}

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.