Skip to content

Commit

Permalink
分析 bean 的加载完成,搜索内容查看 : 注释 4.
Browse files Browse the repository at this point in the history
  • Loading branch information
Vip-Augus committed Jun 11, 2019
1 parent 7f85702 commit 26c25da
Show file tree
Hide file tree
Showing 11 changed files with 219 additions and 10 deletions.
46 changes: 46 additions & 0 deletions spring-analysis-note/src/main/java/base/factory/bean/Car.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package base.factory.bean;

/**
* @author JingQ at 2019-06-08
*/
public class Car {

private int maxSpeed;

private String brand;

private double price;

public int getMaxSpeed() {
return maxSpeed;
}

public void setMaxSpeed(int maxSpeed) {
this.maxSpeed = maxSpeed;
}

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{" +
"maxSpeed=" + maxSpeed +
", brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package base.factory.bean;

import org.springframework.beans.factory.FactoryBean;

/**
* Factory Bean
* @author JingQ at 2019-06-08
*/
public class CarFactoryBean implements FactoryBean<Car> {

private String carInfo;

@Override
public Car getObject() throws Exception {
Car car = new Car();
String[] infos = carInfo.split(",");
car.setBrand(infos[0]);
car.setMaxSpeed(Integer.valueOf(infos[1]));
car.setPrice(Double.valueOf(infos[2]));
return car;
}

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

public String getCarInfo() {
return carInfo;
}

public void setCarInfo(String carInfo) {
this.carInfo = carInfo;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package base.factory.bean;

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

/**
* 注释 4.5 FactoryBean 启动类
* @author JingQ at 2019-06-08
*/
public class FactoryBeanBootstrap {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("factory.bean/factoryBean.xml");
// 不使用 & 前缀,识别到的是 FactoryBean.getObject 返回的 car 类型
Car car = (Car) context.getBean("carFactoryBean");
// 输出 Car{maxSpeed=400, brand='超级跑车', price=20000.0}
System.out.println(car.toString());

// 使用 & 前缀,识别到的是 CarFactoryBean 本身
CarFactoryBean bean = (CarFactoryBean) context.getBean("&carFactoryBean");
// 输出 base.factory.bean.CarFactoryBean@4629104a
System.out.println(bean.toString());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?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">

<bean id="carFactoryBean" class="base.factory.bean.CarFactoryBean">
<!--注释 4.4 还记得这个属性使用么哈哈哈-->
<property name="carInfo" value="超级跑车,400,20000"/>
</bean>
</beans>
Loading

0 comments on commit 26c25da

Please sign in to comment.