一、费静态方法注入
package com.mjy.pojo;
/**
* @author 蒙锦远
* @description
*/
public class Car {
private String name;
private double price;
public Car(String name,double price) {
this.name=name;
this.price=price;
}
}
package com.mjy.pojo;
/**
* @author 蒙锦远
* @description
*/
public class CarFactory1 {
public Car getCar(){
Car car=new Car("红旗牌", 100000);
return car;
}
}
xml配置
<bean class="com.mjy.pojo.CarFactory1" id="carFactory"></bean>
<bean class="com.mjy.pojo.Car" id="Car1" factory-bean="carFactory" factory-method="getCar"></bean>
二、静态工厂方法注入
Car类不变
静态工厂类
package com.mjy.pojo;
/**
* @author 蒙锦远
* @description
*/
public class CarFactory2 {
public static Car getCar() {
return new Car("奔驰", 2000000);
}
}
xml配置
<bean class="com.mjy.pojo.CarFactory2" id="car2" factory-method="getCar"></bean>