Spring學(xué)習(xí)筆記(3)- 注解方式 IOC/DI

1.修改spring-config.xml文件

添加<context:annotation-config/>,表示告訴spring要用注解的方式進(jìn)行配置

<context:annotation-config/>
  1. 注釋掉<property name="category" ref="category" />,該行為在后面將使用注解來完成
<?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 id="category" class="com.jd.java.Category">
        <property name="name" value="Spring"></property>
    </bean>
    <!--在創(chuàng)建Product的時候注入一個Category對象, 注意,這里要使用ref來注入另一個對象-->
    <bean name="p" class="com.jd.java.Product">
        <property name="name" value="product" />
        <!--<property name="category" ref="category" />-->
    </bean>
</beans>

3.@Autowired

在Product.java的category屬性前加上@Autowired注解


4.測試運(yùn)行

package com.jd.test;

import com.jd.java.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class testSpring {
    public static void main(String[] args){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        Product p1 = (Product) context.getBean("p");

        System.out.println(p1.getName());
        System.out.println(p1.getCategory().getName());
    }
}
  1. @Autowired的位置

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

  1. @Resource

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

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

7.對Bean的注解
上述例子是對注入對象行為的注解,那么bean對象本身,比如Category,Product可不可以移出spring-config.xml配置文件,也通過注解進(jìn)行呢?接下來是如何對Bean進(jìn)行注解配置
8.修改spring-config.xml文件

新增<context:component-scan base-package="com.jd.java"/>,其作用是告訴Spring,bean都放在com.jd.java這個包下

<?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.jd.java"/>

</beans>

9.@Component

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

@Component("p")
public class Product {

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

@Component("category")
public class Category {

因?yàn)榕渲脧膕pring-config.xml中移出來了,所以屬性初始化放在屬性聲明上進(jìn)行了。
private String name="product";
private String name="spring";
Product.java

package com.jd.java;

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

import javax.annotation.Resource;

@Component("p")
public class Product {

    private int id;
    private String name = "product";

    @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.jd.java;

import org.springframework.stereotype.Component;

@Component("category")
public class Category {
    private int id;
    private String name = "spring";
    public void setName(String name){
        this.name = name;
    }
    public String getName(){
        return name;
    }
    public void setId(int id){
        this.id = id;
    }
    public int getId(){
        return id;
    }
}

10.測試運(yùn)行

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容