Spring通过FactoryBean来配置bean


引言:

通过实现FactoryBean接口来配置bean

  • Car.java
  • CarFactoryBean.java
  • Main.java
  • beans-beanfactory.xml

Car.java

package com.yczlab.spring.beans.factorybean;

public class Car {
    private String brand;
    private double price;

    public Car() {
    }
    public Car(String brand, double price) {
        this.brand = brand;
        this.price = 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 +
                '}';
    }
}

CarFactoryBean.java

package com.yczlab.spring.beans.factorybean;

import org.springframework.beans.factory.FactoryBean;

//自定义的FactoryBean需要实现FactoryBean接口
public class CarFactoryBean implements FactoryBean<Car> {

    private String brand;

    public void setBrand(String brand) {
        this.brand = brand;
    }

    //返回bean的对象
    @Override
    public Car getObject() throws Exception {
        return new Car(brand, 500000);
    }

    //返回bean的类型
    @Override
    public Class<?> getObjectType() {
        return Car.class;
    }

    //是否为单实例
    @Override
    public boolean isSingleton() {
        return true;
    }
}

Main.java

package com.yczlab.spring.beans.factorybean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans-beanfactory.xml");

        Car car = (Car) context.getBean("car");
        System.out.println(car);

    }
}

beans-beanfactory.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">

    <!--
        通过FactoryBean来配置Bean的实例
        class:指向FactoryBean的全类名
        property:配置FactoryBean的属性
        但实际返回的实例却是FactoryBean的getObject()方法返回的实例!
    -->
    <bean id="car" class="com.yczlab.spring.beans.factorybean.CarFactoryBean">
        <property name="brand" value="BMW"/>
    </bean>

</beans>

文章作者: YangChongZhi
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 YangChongZhi !
评论
 上一篇
Spring中使用注解配置Bean(1) Spring中使用注解配置Bean(1)
引言: 在Spring中通过添加注解的方式来配置Bean,Spring容器会自动管理所扫描包(可以自行配置所要扫描的包的路径)下的标有@Component、@Controller、@Service、@Repository注解的类 Tes
2020-03-08
下一篇 
Spring中使用工厂方法配置Bean(静态工厂,实例工厂) Spring中使用工厂方法配置Bean(静态工厂,实例工厂)
引言: 通过静态工厂方法来配置bean,注意不是配置静态工厂方法实例,而是配置bean实例。通过实例工厂方法来配置bean,先配置工厂的bean实例,再配置所需bean实例。 Car.java StaticCarFactory.java
2020-03-08
  目录