Beans-factory.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "http://www.springframework.org/schema/beans"
Xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans.xsd ">
<!--configure The bean with a static factory method, note that instead of configuring a static factory method instance, Configure the Bean instance--
<!--
Class property: a full-class name that points to a static factory method
Factory-method: name pointing to the static factory method
Constructor-arg: if the factory method needs to pass in parameters, use Constructor-arg to configure the Parameters.
-
<bean id= "car1"
class= "com.hy.spring.beans.factory.StaticCarFactory"
factory-method= "getcar" >
<constructor-arg value= "audi" ></constructor-arg>
</bean>
<!--configuring Factory instances--
<bean id= "carfactory" class= "com.hy.spring.beans.factory.InstanceCarFactory" >
</bean>
<!--configure The Bean with the instance factory method--
<!--
Class Property: The bean that points to the instance factory method
Factory-method: name pointing to the static factory method
Constructor-arg: if the factory method needs to pass in parameters, use Constructor-arg to configure the Parameters.
-
<bean id= "car2" factory-bean= "carfactory" factory-method= "getcar" >
<constructor-arg value= "ford" ></constructor-arg>
</bean>
</beans>
Car.java
Package com.hy.spring.beans.factory;
public class Car {
Private String brand;
Private Double price;
Public String Getbrand () {
Return brand;
}
public void Setbrand (String Brand) {
This.brand = brand;
}
Public double GetPrice () {
Return price;
}
public void Setprice (double Price) {
This.price = price;
}
@Override
Public String toString () {
return "Car [brand=" + brand + ", price=" + price + "]";
}
Public Car () {
System.out.println ("Car S is counstructor ....");
}
Public Car (String brand, double Price) {
Super ();
This.brand = brand;
This.price = price;
}
}
Staticcarfactory.java
Package com.hy.spring.beans.factory;
Import java.util.HashMap;
Import java.util.Map;
/**
* Static Factory Method: A static method that calls a class directly can return an instance of a bean
*/
public class Staticcarfactory {
private static map<string,car> cars = new hashmap<string, car> ();
static{
Cars.put ("audi", new Car ("audi", 30000));
Cars.put ("ford", new Car ("ford", 40000));
}
Static Factory Method.
public static Car Getcar (String Name) {
return Cars.get (name);
}
}
Instancecarfactory.java
Package com.hy.spring.beans.factory;
Import java.util.HashMap;
Import java.util.Map;
/**
* Instance Factory method: The instance factory method, which now needs to create the factory itself,
* The instance method of the calling factory to return the instance of the bean
*/
public class Instancecarfactory {
Private map<string,car> cars = null;
Public Instancecarfactory () {
cars= New hashmap<string, car> ();
Cars.put ("audi", new Car ("audi", 30000));
Cars.put ("ford", new Car ("ford", 40000));
}
Public Car Getcar (String Brand) {
return Cars.get (brand);
}
}
Main.java
Package com.hy.spring.beans.factory;
Import org.springframework.context.ApplicationContext;
Import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main (string[] Args) {
ApplicationContext CTX = new Classpathxmlapplicationcontext ("beans-factory.xml");
Car car = (car) Ctx.getbean ("car1");
System.out.println (car);
Car = (car) Ctx.getbean ("car2");
System.out.println (car);
}
}
Results:
Spring_ configuring Beans Through factory methods