JAVAEE框架之Spring註解

四.註解

​ 通過註解來實現依賴注入,爲什麼要學這個呢???之前的bean的配置都在哪裏呢?都放在了beans.xml這個文件裏面。當項目有很多個bean需要配置的時候,假設有30張表,需要配置每個表對應的dao實現類、service實現類,會導致配置文件比較臃腫。今天通過使用註解來簡化bean文件的配置。

​ 在項目開發中,使用xml文件和註解都可以來實現依賴注入。

4.1 pom.xml

<!--導入jar依賴-->
<dependencies>
    <!--Spring:jar依賴-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.7.RELEASE</version>
    </dependency>
    <!--jUnit:jar依賴-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>
</dependencies>

4.2 beans.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-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
        
      <!--需要注意:掃描包,來找到需要注入的Bean的包位置;
        base-package:基本掃描路徑,去哪個包下進行掃描;
    -->
    <context:component-scan base-package="com.aaa.pojo"/>
        
</beans>

4.3 分層構建

在之前的JAVA WEB項目有dao層、service層、controller層。

丁磊–》163郵箱起家–>大話西遊 等遊戲,養豬;

張朝陽–>搜狐老闆–》養狐狸

馬化騰–》騰訊老闆–>養企鵝

馬雲–>養貓

劉強東–》養狗

4.4 註解標籤

@Component:將類註冊爲Spring的Bean組件
		value="原來的id值"
        value也可以省略,“id的值"
        如果不寫("id值"),會怎麼樣???默認爲:首字母小寫的類名
        
---------------------------------------------
自動裝配:
@Autowired:按照Bean的類型進行裝配;byType
	按類型裝配,只能裝配一個類型實現類;
	如果存在多個相同類型的實現類,需要針對每個實現類,增加一個註解
	@Qualifer("實現類的id名")
	注:@Qualifer不能單獨使用,需要和@Autowird一起使用
@Resource(name = "fox")
		:按照Bean的name來進行裝配注入;byName

實現類:

@Component
public class PigImpl implements IAnimal{
    //這些私有屬性,任意發揮,這裏不是重點;
    private double weight;
    private String color;
    //在方法的實現;
    public void run() {
        System.out.println("小豬在跑...");
    }
}

Person類:注意這時候沒有Qualifier

@Component
public class Person {
    //這時候要養豬,理解一下;注入值;
    //自動裝配的方式;
    //Qualifier(value="值")不是name;
    @Autowired
    private IAnimal animal;

    public void feed(){
        animal.run();
        System.out.println("開始餵養");
    }
    @Override
    public String toString() {
        return "Person{" +
                "animal=" + animal +
                '}';
    }
}

不管是哪種裝配Bean的方式,都需要增加裝配Bean的包路徑。

<!--需要注意:掃描包,來找到需要注入的Bean的包位置;
    base-package:基本掃描路徑,去哪個包下進行掃描;
-->
<context:component-scan base-package="com.aaa.pojo,com.aaa.dao"/>

4.5

總結:

1.爲什麼要使用註解注入和裝配;

2.掌握@Component註解,理解@Autowired 按類型裝配和@Resource按name裝配;

3.使用@Component註解的時候,切記要增加掃描包的配置,註解哪個Bean,就要增加哪個掃描包路徑。

l.run();
System.out.println(“開始餵養”);
}
@Override
public String toString() {
return “Person{” +
“animal=” + animal +
‘}’;
}
}


不管是哪種裝配Bean的方式,都需要增加裝配Bean的包路徑。

```xml
<!--需要注意:掃描包,來找到需要注入的Bean的包位置;
    base-package:基本掃描路徑,去哪個包下進行掃描;
-->
<context:component-scan base-package="com.aaa.pojo,com.aaa.dao"/>

4.5

注意的問題:

[外鏈圖片轉存中…(img-CcHERyds-1592274645404)]

有兩個IAnimal接口的實現類的時候,報錯了。

[外鏈圖片轉存中…(img-QmgktRb9-1592274645407)]

[外鏈圖片轉存中…(img-YET1Q6DV-1592274645409)]

[外鏈圖片轉存中…(img-MKslce4f-1592274645414)]

總結:

1.爲什麼要使用註解注入和裝配;

2.掌握@Component註解,理解@Autowired 按類型裝配和@Resource按name裝配;

3.使用@Component註解的時候,切記要增加掃描包的配置,註解哪個Bean,就要增加哪個掃描包路徑。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章