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"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
"
>
<!-- 通过静态工厂方法来配置bean,注意不是配置静态工厂方法实例,而是配置bean实例 -->
<!--
class 属性:指向静态工厂方法的全类名
factory-method:指向静态工厂方法的名字
constructor-arg:如果工厂方法需要传入参数,则使用constructor-arg来配置参数
-->
<bean id="car1" class="com.beans.factory.StaticCarFactory"
factory-method="getCar">
<constructor-arg value="audi"></constructor-arg>
</bean>
<!-- 配置工厂实例 -->
<bean id="carFactory" class="com.beans.factory.InstanceCarFactory">
</bean>
<!-- 通过实例工厂方法来配置bean -->
<!--
class 属性:指向实例工厂方法的bean
factory-method : 指向静态工厂方法的名字
constructor-arg :如果工厂方法需要传入参数,则使用constructor-arg来配置参数。
-->
<bean id="car2" factory-bean="carFactory" factory-method="getCar">
<constructor-arg value="ford"></constructor-arg>
</bean>
</beans>
Car类
package com.beans.factory;
public class Car {
public String brand;
public double price;
public String getBrand() {
return brand;
}
public Car() {
super();
// TODO Auto-generated constructor stub
System.out.println("Car's constructor....");
}
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(String brand, double price) {
super();
this.brand = brand;
this.price = price;
}
}
StaticCarFactory.java
package com.beans.factory;
import java.util.HashMap;
import java.util.Map;
//静态工厂方法:直接调用某一个类的静态方法就可以返回bean的实例
public class StaticCarFactory {
private static Map<String, Car> cars=new HashMap<String,Car>();
static {
cars.put("audi", new Car("audi",3000000));
cars.put("ford", new Car("ford",4000000));
}
//静态工厂方法
public static Car getCar(String name) {
return cars.get(name);
}
}
InstanceCarFactory.java
package com.beans.factory;
import java.util.HashMap;
import java.util.Map;
import javax.naming.event.NamespaceChangeListener;
/*实例工厂方法:实例工厂的方法,即先需要创建工厂本身,再调用工厂实例方法
* 来 返回bean的实例
* */
public class InstanceCarFactory {
private Map<String, Car> cars=null;
public InstanceCarFactory() {
cars=new HashMap<String,Car>();
cars.put("audi", new Car("audi",3000000));
cars.put("ford",new Car("ford",4000000));
}
public Car getCar(String brand) {
return cars.get(brand);
}
}
运行主类
package com.beans.factory;
import org.omg.CORBA.CTX_RESTRICT_SCOPE;
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);
}
}