Spring的組合注解和元注解原理與用法詳解
本文實(shí)例講述了Spring的組合注解和元注解原理與用法。分享給大家供大家參考,具體如下:
一 點(diǎn)睛
從Spring 2開始,為了相應(yīng)JDK 1.5推出的注解功能,Spring開始加入注解來替代xml配置。Spring的注解主要用來配置和注入Bean,以及AOP相關(guān)配置。隨著注解的大量使用,尤其相同的多個(gè)注解用到各個(gè)類或方法中,會(huì)相當(dāng)繁瑣。出現(xiàn)了所謂的樣本代碼,這是Spring設(shè)計(jì)要消除的代碼。
元注解:可以注解到別的注解上去的注解。
組合注解:被注解的注解,組合注解具備其上的元注解的功能。
Spring的很多注解都可以作為元注解,而且Spring本身已經(jīng)有很多組合注解,如@Configuration就是一個(gè)組合了@Component的注解,表明被注解的類其實(shí)也是一個(gè)Bean。
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Configuration {
String value() default "";
}
二 實(shí)戰(zhàn)項(xiàng)目
自定義一個(gè)組合注解,它的元注解是@Configuration和@ConfigurationScan
三 實(shí)戰(zhàn)
1 自定義組合注解
package com.wisely.highlight_spring4.ch3.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration //組合@Configuration元注解
@ComponentScan //組合@ComponentScan元注解
public @interface WiselyConfiguration {
String[] value() default {}; //覆蓋value參數(shù)
}
2 編寫服務(wù)類
package com.wisely.highlight_spring4.ch3.annotation;
import org.springframework.stereotype.Service;
@Service
public class DemoService {
public void outputResult(){
System.out.println("從組合注解配置照樣獲得的bean");
}
}
3 編寫配置類
package com.wisely.highlight_spring4.ch3.annotation;
@WiselyConfiguration("com.wisely.highlight_spring4.ch3.annotation")
public class DemoConfig {
}
4 編寫主類
package com.wisely.highlight_spring4.ch3.annotation;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(DemoConfig.class);
DemoService demoService = context.getBean(DemoService.class);
demoService.outputResult();
context.close();
}
}
四 運(yùn)行
從組合注解配置照樣獲得的bean
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
@Resource和@Autowired兩個(gè)注解的區(qū)別及說明
這篇文章主要介紹了@Resource和@Autowired兩個(gè)注解的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Java解決線程的不安全問題之volatile關(guān)鍵字詳解
這篇文章主要介紹了Java解決線程的不安全問題之volatile關(guān)鍵字詳解,可見性指一個(gè)線程對(duì)共享變量值的修改,能夠及時(shí)地被其他線程看到,而 volatile 關(guān)鍵字就保證內(nèi)存的可見性,需要的朋友可以參考下2023-08-08
詳解Java編程中統(tǒng)一資源定位符URL的相關(guān)使用
這篇文章主要介紹了Java編程中統(tǒng)一資源定位符URL的相關(guān)使用,是Java網(wǎng)絡(luò)編程中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10
POST方法給@RequestBody傳參數(shù)失敗的解決及原因分析
這篇文章主要介紹了POST方法給@RequestBody傳參數(shù)失敗的解決及原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
JavaWeb實(shí)現(xiàn)用戶登錄與注冊(cè)功能(服務(wù)器)
這篇文章主要介紹了JavaWeb實(shí)現(xiàn)用戶登錄與注冊(cè)功能,服務(wù)器部分的關(guān)鍵代碼實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08

