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);
}
}