• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
Y-wee
博客园    首页    新随笔    联系   管理     

spring中FactoryBean类的用法

spring中FactoryBean类的用法

1、简介

工厂bean,它是作用主要就是创建bean对象

2、如何使用

2.1、编写实体类

package com.yl.bean;

public class Car {
    private String brand;
    private Integer price;
    private String color;

    public Car() {
    }

    public Car(String brand, Integer price, String color) {
        this.brand = brand;
        this.price = price;
        this.color = color;
    }

    public String getBrand() {
        return brand;
    }

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

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                ", color='" + color + '\'' +
                '}';
    }
}

2.2、编写一个类实现FactoryBean

重写以下方法

  • T getObject() throws Exception:通过FactoryBean创建bean对象
  • Class<?> getObjectType():指明bean所属的类
  • boolean isSingleton():指明bean的作用域
package com.yl.factory;

import com.yl.bean.Car;
import org.springframework.beans.factory.FactoryBean;

/**
 * 自定义工厂
 */
public class MyFactory implements FactoryBean {
    private String car;//约定格式:车型,颜色,价格

    public void setCar(String car) {
        this.car = car;
    }

    @Override
    public Object getObject() throws Exception {
        //获取car数据
        String carData[]=car.split(",");

        Car car1=new Car();
        car1.setBrand(carData[0]);
        car1.setColor(carData[1]);
        car1.setPrice(Integer.parseInt(carData[2]));

        return car1;
    }

    @Override
    public Class<?> getObjectType() {
        return Car.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }
}

2.3、配置文件

<?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">

    <!--MyFactory-->
    <bean id="factory" class="com.yl.factory.MyFactory">
        <property name="car" value="兰博基尼,红色,1000000"></property>
    </bean>

</beans>

2.4、测试类

package com.yl.ui;

import com.yl.bean.Car;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest1 {

    public static void main(String[] args) {
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("CarBean.xml");

        Car car= (Car) applicationContext.getBean("factory");

        System.out.println(car);
    }

}
记得快乐
posted @ 2020-10-15 16:38  Y-wee  阅读(321)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3