Spring中实例化Bean的三种方式
程序员文章站
2022-03-03 11:52:27
...
目录直通车
实例化 Bean 是 Bean 生命周期的第一步。
假设现在有一个下面这样的实体类
public class Car {
private String brand;
private int price;
public Car(){}
public Car(String brand, int price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price='" + price + '\'' +
'}';
}
}
1、构造器方法实例化 Bean
构造器方法分有参和无参两种,在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" profile="dev">
<!-- 无参构造实例化 -->
<bean id="car1" class="com.fun.entity.Car"/>
<!-- 有参构造器实例化 -->
<bean id="car2" class="com.fun.entity.Car">
<!-- 指定构造器参数 index对应构造器中参数的位置 -->
<!-- 也可以通过指定参数类型,指定参数名称来注入属性-->
<constructor-arg index="0" value="大众"/>
<constructor-arg index="1" value="300000"/>
</bean>
</beans>
之后再Main函数中,通过 ApplicationContext applicationContext = new ClassPathXmlApplicationContext()创建出IOC容器并 将xml文件加载到容器,通过applicationContext对象即可获取到 person1 和 person2了。
2、静态工厂实例化 Bean
<!-- 通过静态工厂方法来配置bean
此处配置的是car,而不是StaticFactory -->
<bean id="car1" class="com.fun.StaticFactory" factory-method="getCar">
<!-- constructor-args 传入value参数到getCar函数 -->
<constructor-arg value="Audi"/>
</bean>
/**
* 静态工厂方法
*/
public class StaticFactory {
private static Map<String,Car> cars = new HashMap<>();
static {
cars.put("Audi",new Car("Audi",300000));
cars.put("Benz",new Car("Benz",660000));
}
public static Car getCar(String name){
return cars.get(name);
}
}
通过xml中传入的 name,获取到对应 car 对象。
public class StaticFactoryApplication {
public static void main(String[] args) {
ApplicationContext ctx = new
ClassPathXmlApplicationContext("com/spring/beans/InstantiateBean/InstanceAndStaticFactory/bean-factory.xml");
Car car1 = (Car) ctx.getBean("car1");
System.out.println(car1);
}
3、实例工厂实例化 Bean
<!-- 实例工厂方法 -->
<bean id="carFactory" class="com.fun.InstanceFactory"/>
<bean id="car2" factory-bean="carFactory" factory-method="getCar">
<constructor-arg value="benz"/>
</bean>
/**
* 实例工厂方法
* 现需要创建工厂本身再调用工厂的实例方法来返回bean的实例
*/
public class InstanceFactory {
private Map<String,Car> cars = new HashMap<>();
public InstanceFactory(Map<String, Car> cars) {
this.cars = cars;
}
public InstanceFactory() {
cars = new HashMap<>();
cars.put("audi",new Car("audi",300000));
cars.put("benz",new Car("benz",660000));
}
public Car getCar(String name) {
return cars.get(name);
}
}
public class FactoryApplication {
public static void main(String[] args) {
ApplicationContext ctx = new
ClassPathXmlApplicationContext("com/spring/beans/InstantiateBean/InstanceAndStaticFactory/bean-factory.xml");
Car car2 = (Car) ctx.getBean("car2");
System.out.println(car2);
}
}
静态工厂实例化Bean与实例工厂实例化Bean的区别
对比如下两者的 XML。
<!-- 静态工厂方法 -->
<bean id="car1" class="com.fun.StaticFactory" factory-method="getCar">
<constructor-arg value="Audi"/>
</bean>
<!-- 实例工厂方法 -->
<bean id="carFactory" class="com.fun.InstanceFactory"/>
<bean id="car2" factory-bean="carFactory" factory-method="getCar">
<constructor-arg value="benz"/>
</bean>
上一篇: Java(Android)线程池