# Spring+SpringMVC+MyBatis入門實踐(2)
阿新 • • 發佈:2018-12-13
注入物件
- Product.java
package com.happycoder.pojo;
public class Product {
private int id;
private String name;
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 Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
}
- 修改spring-config.xml
...
<bean name="c" class="com.happycoder.pojo.Product" >
<property name="name" value="Product" />
<property name="category" ref="c" />
</bean>
...
- 單元測試類
...
Product p = (Product) context.getBean("p");
System.out.println(p.getName());
System.out.println(p.getCategory().getName());
...