[TOC]
# 注入對象
在上例中,對Category的name屬性注入了"category 1"字符串
在本例中 ,對Product對象,注入一個Category對象。
## 步驟 1 : 先運行,看到效果,再學習
先將完整的 spring 項目(向老師要相關資料),配置運行起來,確認可用之后,再學習做了哪些步驟以達到這樣的效果。
## 步驟 2 : 模仿和排錯
在確保可運行項目能夠正確無誤地運行之后,再嚴格照著教程的步驟,對代碼模仿一遍。
模仿過程難免代碼有出入,導致無法得到期望的運行結果,此時此刻通過比較**正確答案** ( 可運行項目 ) 和自己的代碼,來定位問題所在。
采用這種方式,**學習有效果,排錯有效率**,可以較為明顯地提升學習速度,跨過學習路上的各個檻。
## 步驟 3 : Product.java
Product類中有對Category對象的setter getter
> 產品帶有類別屬性,一個產品只屬于一個類別。
~~~
package com.dodoke.pojo;
public class Product {
private int id;
private String name;
private int number;
private double price;
private Category category;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
~~~
## 步驟 4 : applicationContext.xml
在創建Product的時候注入一個Category對象
注意,這里要使用ref來注入另一個對象
~~~
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean name="c" class="com.dodoke.pojo.Category">
<property name="name" value="category 1" />
</bean>
<bean name="product" class="com.dodoke.pojo.Product">
<property name="name" value="Apple Watch" />
<property name="number" value="2" />
<property name="price" value="8800" />
<property name="category" ref="c" />
</bean>
</beans>
~~~
> property后面緊跟著的name是類中對象的屬性name。
## 步驟 5 : TestSpring
~~~
package com.dodoke.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.dodoke.pojo.Category;
import com.dodoke.pojo.Product;
public class TestSpring {
public static void main(String[] args) {
// 讀取applicationContext.xml配置文件
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
// 在xml文件中,bean標簽的name是c,獲取到name為c的bean標簽里面的內容強轉為Category類型
Category c = (Category) context.getBean("c");
System.out.println(c.getName());
Product product = (Product) context.getBean("product");
System.out.println(product.getName());
System.out.println(product.getNumber());
System.out.println(product.getPrice());
System.out.println(product.getCategory().getName());
}
}
~~~
通過Spring拿到的Product對象已經被注入了Category對象了。
運行結果如下:

> 重點關注:一個類的成員變量有引用類型的變量的時候,這個引用類型的變量如何初始化,通過ref引入。