建立spring自訂註解進行自動裝配

來源:互聯網
上載者:User

標籤:color   display   tin   ota   lock   run   XML   open   support   

1、建立自訂註解

 1 import org.springframework.beans.factory.annotation.Qualifier; 2 import java.lang.annotation.ElementType; 3 import java.lang.annotation.Retention; 4 import java.lang.annotation.RetentionPolicy; 5 import java.lang.annotation.Target; 6  7  8 @Target({ElementType.FIELD,ElementType.PARAMETER,ElementType.TYPE}) 9 @Retention(RetentionPolicy.RUNTIME)10 @Qualifier11 public @interface StringedInstrument {12 13 }
View Code

Retention註解有一個屬性value,是RetentionPolicy類型的,Enum RetentionPolicy是一個枚舉類型,

用@Retention(RetentionPolicy.CLASS)修飾的註解,表示註解的資訊被保留在class檔案(位元組碼檔案)中當程式編譯時間,但不會被虛擬機器讀取在啟動並執行時候;
用@Retention(RetentionPolicy.SOURCE )修飾的註解,表示註解的資訊會被編譯器拋棄,不會留在class檔案中,註解的資訊只會留在源檔案中;
用@Retention(RetentionPolicy.RUNTIME )修飾的註解,表示註解的資訊被保留在class檔案(位元組碼檔案)中當程式編譯時間,會被虛擬機器保留在運行時,

2、建立instrument ,並使用註解

 

1 @StringedInstrument2 public class Instrument {3 4     public void play() {5         System.out.println("playing...");6     }7 }

 

3、建立表演者kenny,進行自動裝配

 1 import org.springframework.beans.factory.annotation.Autowired; 2  3 public class Kenny { 4  5     @Autowired 6     @StringedInstrument 7     private Instrument instrument; 8  9     public Instrument getInstrument() {10         return instrument;11     }12 13     public void setInstrument(Instrument instrument) {14         this.instrument = instrument;15     }16 17 18     public Kenny(Instrument instrument) {19         this.instrument = instrument;20     }21 22     public void  perform(){23         instrument.play();24     }25 }

4、配置spring

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4        xmlns:context="http://www.springframework.org/schema/context" 5        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"> 6  7     <context:annotation-config></context:annotation-config> 8  9     <bean id="kenny" class="Kenny">10     </bean>11 12     <bean id="instrument" class="Instrument">13 14     </bean>15 16 </beans>

5、測試

 1 import org.springframework.context.ApplicationContext; 2 import org.springframework.context.support.ClassPathXmlApplicationContext; 3  4  5 public class Test { 6     public static void main(String[] args) { 7  8         ApplicationContext context = new ClassPathXmlApplicationContext("context.xml"); 9         Kenny kenny = (Kenny) context.getBean("kenny");10         kenny.perform();11     }12 }

 

建立spring自訂註解進行自動裝配

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.