forked from Vip-Augus/spring-analysis-note
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
219 additions
and
10 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
spring-analysis-note/src/main/java/base/factory/bean/Car.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + | ||
'}'; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
spring-analysis-note/src/main/java/base/factory/bean/CarFactoryBean.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
spring-analysis-note/src/main/java/base/factory/bean/FactoryBeanBootstrap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
|
||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
spring-analysis-note/src/main/resources/factory.bean/factoryBean.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.