Spring入門學習手冊 2:怎麼用註解來DI/IOC

目錄php

1、若是使用註解的話,在配置文件中應該作什麼?

在beans標籤後加上一個java

<context:annotation-config/>
複製代碼

標籤來聲明將要使用註解就能夠了。spring

<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!--在此處加上此標籤-->
    <context:annotation-config/>
    <bean name="c" class="com.learn.springDemo.Category">
        <property name="name" value="Eather"></property>
        <property name="id" value="12345"></property>
    </bean>
    <bean name="p" class="com.learn.springDemo.Product">
        <property name="name" value="a product"></property>
        <property name="id" value="1008611"></property>

        <!--將下面這行註釋掉,由於代碼中將使用自動裝配來將categroy實例注入product-->
        <!--<property name="category" ref="c"></property>-->

    </bean>

</beans>
複製代碼

2、註解應該怎麼用?

若是是使用@Autowired註解的話,能夠加在兩個地方前面。bash

屬性前面 以及 setter方法前面。app

以下代碼:post

package com.learn.springDemo;

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

public class Product {
    private int id;
    private String name;

    //加在這個地方
    @Autowired
    private Category category;

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    //加在這個地方
    @Autowired
    public void setCategory(Category category) {
        this.category = category;
    }

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public Category getCategory() {
        return this.category;
    }
}

複製代碼

在這裏必需要import org.springframework.beans.factory.annotation.Autowired;測試

運行結果:this

a product's id is 1008611 and its category name is Eather 複製代碼

3、注入同一個類的不一樣實例應該怎麼辦?

上一篇筆記 裏提到了 xml裏面能夠寫多個相同的bean嗎 這個問題,也就是注入同一個類的不一樣實例應該怎麼辦?在使用xml配置時很簡單,改一下name就行。spa

此時就能夠使用@Resource註解code

先改一下xml配置文件

<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--在此處加上此標籤-->
    <context:annotation-config/>
    <bean name="c" class="com.learn.springDemo.Category">
        <property name="name" value="Eather"></property>
        <property name="id" value="12345"></property>
    </bean>
    <bean name="cc" class="com.learn.springDemo.Category">
        <property name="name" value="David"></property>
        <property name="id" value="10086"></property>
    </bean>
    <bean name="p" class="com.learn.springDemo.Product">
        <property name="name" value="a product"></property>
        <property name="id" value="1008611"></property>

        <!--將下面這行註釋掉-->
        <!--<property name="category" ref="c"></property>-->

    </bean>

</beans>
複製代碼

註解應該加在屬性的前面

package com.learn.springDemo;

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

import javax.annotation.Resource;

public class Product {
    private int id;
    private String name;

    @Resource(name = "c")
    private Category category;

    @Resource(name = "cc")
    private Category category1;

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCategory(Category category) {
        this.category = category;
    }

    public void setCategory1(Category category1) {
        this.category1 = category1;
    }

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public Category getCategory() {
        return this.category;
    }

    public Category getCategory1() {
        return category1;
    }
}
複製代碼

測試代碼:

package com.learn.springTest;

import com.learn.springDemo.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String args[]) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});


        Product p = (Product) ac.getBean("p");

        System.out.println(p.getName() + "'s id is " + p.getId() + " and its category name is "  + p.getCategory().getName() + " and " + p.getCategory1().getName());

    }

}
複製代碼

運行結果:

a product's id is 1008611 and its category name is Eather and David 複製代碼

4、xml配置文件裏可不能夠 bean 也不寫?

上述例子都是對 注入對象行爲 的註解,對於bean對象自己的使用可不能夠也用註解的方式進行,而不寫到配置文件中呢?

答案固然是能夠,修改一下配置文件,去掉全部beansbean標籤,加上

<context:component-scan base-package="com.learn.springDemo"/>
複製代碼

base-package 用來指定要掃描的包。

<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!--在此處加上此標籤-->
    <context:component-scan base-package="com.learn.springDemo" />

</beans>
複製代碼

類定義代碼是:

package com.learn.springDemo;

import org.springframework.stereotype.Component;

@Component("c")
public class Category {
    private int id;
    private String name = "我是一個Category";

    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }
}



package com.learn.springDemo;

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


@Component("p")
public class Product {
    private int id;
    private String name = "我是一個product";

    @Autowired
    private Category category;

    public void setId(int id) {
        this.id = id;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCategory(Category category) {
        this.category = category;
    }


    public int getId() {
        return this.id;
    }

    public String getName() {
        return this.name;
    }

    public Category getCategory() {
        return this.category;
    }
}
複製代碼

測試代碼是:

package com.learn.springTest;

import com.learn.springDemo.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
    public static void main(String args[]) {
        ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});


        Product p = (Product) ac.getBean("p");

        System.out.println("product 名字是 " + p.getName() + " category 名字是 "  + p.getCategory().getName());

    }

}
複製代碼

運行結果是:

product 名字是 我是一個product category 名字是 我是一個Category
複製代碼

至於前面的第三個問題應該怎麼解決,暫時我也不知道,可能這個問題沒有太大的意義吧!

相關文章
相關標籤/搜索