天天看點

spring (學習記錄)通過factorybean配置bean

之前學了,配置bean可以用普通全類名配置、用工廠方法配置,FactoryBean又是什麼呢

有時候配置bean要用到,IOC其他Bean,這時,用FactoryBean配置最合适。

FactoryBean是一個接口,要用的話就要實作它。他有三個方法:

getObject() //傳回bean對象

getObjectType() //傳回bean的類型

isSingleton() //是否單例

看例子:

Car類:

package com.beans.factorybean;

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;
    }
        
}      

CarFactoryBean類:

package com.beans.factorybean;

import org.springframework.beans.factory.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 {
        // TODO Auto-generated method stub
        return new Car("BMW",2000000);
    
    }
    //傳回bean的類型
    @Override
    public Class<?> getObjectType() {
        // TODO Auto-generated method stub
        return Car.class;
    }
    
    @Override   
    public boolean isSingleton() {
        
        return true;
    }
    
}      

配置檔案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
    "
    >
    <!-- 通過factorybean 來配置bean的執行個體
    class:指向factorybean的全類名
    property:配置factorybean的屬性
    但實際傳回的執行個體确是factorybean的getObject()方法傳回的執行個體!
     -->
    <bean id="car" class="com.beans.factorybean.CarFactoryBean">
         <property name="brand" value="BMW"></property>
    </bean>
                                                                                                                                               
</beans>      
package com.beans.factorybean;

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

public class main {
        public static void main(String[] args) {
      ApplicationContext ctx=new ClassPathXmlApplicationContext("factorybean.xml");
      Car car=(Car) ctx.getBean("car");
      System.out.println(car);
    }
}