【Spring三】使用註解配置bean

來源:互聯網
上載者:User
一.註解形式實現DI,給引用屬性賦值:@Resource(name="xx")
1、在spring的設定檔中,加入命名空間
        xmlns:context="http://www.springframework.org/schema/context"
        http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
2、在spring的設定檔中,開啟註解解析器
       <context:annotation-config></context:annotation-config>

3、把person和student放入到spring容器中
       <bean id="person" class="cn.itheima03.spring.di.annotation.Person"></bean>
       <bean id="student" class="cn.itheima03.spring.di.annotation.Student"></bean>           設定檔:
<? xml  version= "1.0" encoding ="UTF-8"?> < beans  xmlns="http://www.springframework.org/schema/beans"        xmlns:context="http://www.springframework.org/schema/context"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd            http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-2.5.xsd" >     <bean id="person" class="cn.itheima03.spring.di.annotation.Person" ></bean>     <bean id="student" class="cn.itheima03.spring.di.annotation.Student" ></bean>     <!--            1 引入命名空間            2、啟動註解解析器     -->      <context:annotation-config></ context:annotation-config> </ beans >

4、在Person類中的student不需要set和get方法,直接在屬性上配置@Resource註解
public  class Person {       private  Long pid; //   @Autowired//按照類型匹配 //   @Qualifier("student")//id匹配             @Resource (name=  "student")       private  Student student;             public  void show(){             this.student .show();      } }



原理:
     1、當啟動spring容器的時候,給spring容器的bean建立對象
     2、當spring容器解析到<context:annotation-config></context:annotation-config>的時候,
         spring容器掃描在spring容器中的bean
     3、查看bean中的屬性或者方法上有沒有@Resource
            (1)、有
                ①、如果該註解的屬性name的值為"",則會按照屬性的名稱和spring中的ID值進行匹配,如果匹配
                  成功,則賦值,如果匹配不成功,則按照類型進行匹配,如果類型匹配不成功,則報錯
                ②、如果該註解的屬性name的值不為"",則按照name屬性的值和spring中的ID值進行匹配,
                   如果匹配成功,則賦值,匹配不成功,則報錯
            (2)、沒有

說明:
   基本類型不能用註解賦值 
   註解的效率沒有xml檔案的高,但是書寫比較簡單
   @Autowired按照類型進行匹配 
   @Qualifier("studen")按照ID進行匹配
   按照類型匹配比較危險

通過@Resource(name="student")對引用屬性進行賦值,等效於在<bean>中配置:
        <property name= "student">                 <ref bean= "student" />        </ property >
只不過通過property標籤來給屬性設定值時,需要get/set方法,而是用註解則不需要。
二.類的掃描機制:@Component(value="xx")
為了在設定檔中不寫<bean>標籤,可以在類上使用component註解來解決。步驟:    1、在spring的設定檔中,引入命名空間
        xmlns:context="http://www.springframework.org/schema/context"
        http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd

   2、添加類的掃描解析器
     <context:component-scan base-package="cn.itheima03.spring.scan"></context:component-scan>
        既包含了依賴注入的註解解析器,還包含了類掃描的
   3、在一個類上標註註解                         @Component
                        public class Person {}
                        等效於
                        <bean id="person" class="..Person">
            配置註解屬性:
                        @Component(value="aa")
                        public class Person {}
                        等效於
                        <bean id="aa" class="..Person">
        spring容器會為該bean建立對象,id值為:
                  ①如果該註解沒有value屬性,則值為類的第一個字母變成小寫,其他的不變
                  ②如果有value屬性,則用value屬性的值作為ID的值
   4、執行@Resource註解的流程

使用的註解越多,在xml檔案中寫的內容越少,效率越低
更多典型化註解:
     @Controller
     @Service
     @Repository
     其作用和用法與@Component保持一致,用法見下例子二。

注意:
     在一個類中有一個基本類型的屬性,如果該類是以註解的形式放入到spring容器中的,基本屬性在這裡是沒有辦法賦值的
     利用註解的方式實現繼承,不需要使用Parent
示範代碼:
設定檔:
<? xml  version= "1.0" encoding ="UTF-8"?> < beans  xmlns="http://www.springframework.org/schema/beans"        xmlns:context="http://www.springframework.org/schema/context"         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xsi:schemaLocation="http://www.springframework.org/schema/beans            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd            http://www.springframework.org/schema/context            http://www.springframework.org/schema/context/spring-context-2.5.xsd" >      <context:component-scan base-package="cn.itheima03.spring.scan" ></context:component-scan> </ beans >

例子一.Document:   
public  interface Document {    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.