bean的配置方式:通过全类名(反射),通过工厂方法(静态工厂方法&实例工厂方法),FactoryBean
静态工厂方法
package com.hous.factory;
import java.util.HashMap;
import java.util.Map;
import com.hous.spring.Car;
public class StaticCarFactory {
private static Map<String, Car> cars = new HashMap<String, Car>();
static {
cars.put("qirui", new Car("qirui", 200000));
cars.put("hanma", new Car("hanma", 100000));
}
/**
* 静态工厂方法
*
* @param name
* @return
*/
public static Car getCar(String name) {
return cars.get(name);
}
}
实例工厂方法
package com.hous.factory;
import java.util.HashMap;
import java.util.Map;
import com.hous.spring.Car;
/**
* 实例工厂方法需要创建工厂本身,再调用工厂实例方法返回bean实例
*
*/
public class InstanceCarFactory {
private Map<String, Car> cars = null;
public InstanceCarFactory() {
this.cars = new HashMap<String, Car>();
cars.put("qirui", new Car("qirui", 200000));
cars.put("luhu", new Car("luhu", 100000));
}
public Car getCar(String brand) {
return cars.get(brand);
}
}
applicationContext.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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 通过静态工厂方法配置Bean,不是工厂方法实例而是Bean的实例 --> <!-- class:静态工厂全类名 factory-method:指向静态工厂类的静态方法 constructor-arg:传入静态方法中的参数 --> <bean id="car1" class="com.hous.factory.StaticCarFactory" factory-method="getCar"> <constructor-arg value="hanma"></constructor-arg> </bean> <!-- 配置工厂实例 --> <bean id="carFactory" class="com.hous.factory.InstanceCarFactory"></bean> <!-- 通过实例工厂方法配置bean --> <bean id="car2" factory-bean="carFactory" factory-method="getCar"> <constructor-arg value="luhu"></constructor-arg> </bean> </beans>
FactoryBean配置
package com.hous.factory;
import org.springframework.beans.factory.FactoryBean;
import com.hous.spring.Car;
/**
* 自定义FactoryBean实现接口
* @author Administrator
*
*/
public class CarFactoryBean implements FactoryBean<Car> {
private String brand;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
@Override
public Car getObject() throws Exception {
return new Car(brand, 5000);
}
@Override
public Class<?> getObjectType() {
return Car.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
<?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:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:context="http://www.springframework.org/schema/context" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"> <!-- 通过FactoryBean配置bean实例 class:配置FactoryBean的全类名 property:指向FactoryBean的属性 通过FactoryBean的getObject方法返回bean实例 --> <bean id="car" class="com.hous.factory.CarFactoryBean"> <property name="brand" value="fute"></property> </bean> </beans>