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

Spring+SpringMVC+MyBatis入門實踐(3)

註解方式IOC/DI

  1. 修改spring-config.xml
...

    <context:annotation-config/>
    <bean name="c" class="com.happycoder.pojo.Category">
        <property name="name" value="category 1" />
    </bean>
    <bean name="p" class="com.happycoder.pojo.Product">
        <property name=
"name"
value="product 1" />
<!--<property name="category" ref="c" />--> </bean> </beans>
  1. @Autowired

在Product.java的category屬性前加上@Autowire註解

@Autowired

private Category category;

...
    @Autowired
    private Category category;
...
  1. @Autowired的位置

除了前面的這種方式外,也可以在setCategory方法前加上@Autowired,這樣來達到相同的效果

@Autowired

public void setCategory(Category category)

  1. @Resource

除了@Autuwired之外,@Resource也是常用的手段

@Resource(name=“c”)

private Category category;

  1. 對Bean的註解

上述例子是對注入物件行為的註解,那麼bean物件本身,比如Category,Product可不可以移出applicationContext.xml配置檔案,也通過註解進行呢? 接下來就講解如何對Bean進行註解配置

  1. 修改spring-config.xml

什麼都去掉,只新增:

<context:component-scan base-package="com.happycoder.pojo" />
<?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">

    <!--<context:annotation-config/>
    <bean name="c" class="com.happycoder.pojo.Category">
        <property name="name" value="category 1" />
    </bean>
    <bean name="p" class="com.happycoder.pojo.Product">
        <property name="name" value="product 1" />
        &lt;!&ndash;<property name="category" ref="c" />&ndash;&gt;
    </bean>-->
    <context:component-scan base-package="com.happycoder.pojo" />
</beans>
  1. @Component

為Product類加上@Component註解,即表明此類是bean

@Component(“p”)

public class Product {

為Category 類加上@Component註解,即表明此類是bean

@Component(“c”)

public class Category {

另外,因為配置從applicationContext.xml中移出來了,所以屬性初始化放在屬性宣告上進行了。

private String name=“product 1”;

private String name=“category 1”;

Product.java

package com.happycoder.pojo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component("p")
public class Product {
    private int id;
    private String name = "product 1";
    @Autowired
    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;
    }
}

Category.java

package com.happycoder.pojo;

import org.springframework.stereotype.Component;

@Component("c")
public class Category {
    private int id;
    private String name="category 1";

    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;
    }
}

執行測試類

在這裡插入圖片描述