1. 程式人生 > ># Spring+SpringMVC+MyBatis入門實踐(2)

# Spring+SpringMVC+MyBatis入門實踐(2)

注入物件

  1. 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; } }
  1. 修改spring-config.xml
...
<bean name="c" class="com.happycoder.pojo.Product"
>
<property name="name" value="Product" /> <property name="category" ref="c" /> </bean> ...
  1. 單元測試類
...
Product p = (Product) context.getBean("p");
        System.out.println(p.getName());
        System.out.println(p.getCategory().getName());
...

在這裡插入圖片描述