通過實(shí)例解析Spring組合注解與元注解
這篇文章主要介紹了通過實(shí)例解析Spring組合注解與元注解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
1、概述
1.1、Spring提供了大量的注解,
尤其是相同的注解用到各個(gè)類中,會(huì)相當(dāng)?shù)膯拢?/p>
1.2、元注解:
可以注解到別的注解上的注解;
組合注解:
被注解注解的注解稱為 組合注解;
組合注解 具備 元注解 的功能,Spring的很多注解都可以作為元注解;
1.3、案例
package com.an.config;
import com.an.annotation.MyAnnotation;
/**
* @description:
* @author: anpeiyong
* @date: Created in 2019/11/21 8:57
* @since:
*/
@MyAnnotation(value = "com.an")
public class AnnotationConfig {
}
package com.an.annotation;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @description:
* @author: anpeiyong
* @date: Created in 2019/11/21 8:47
* @since:
*/
@Target(value = ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Configuration
@ComponentScan
public @interface MyAnnotation {
String[] value() default {};
}
package com.an.annotation;
import org.springframework.stereotype.Service;
/**
* @description:
* @author: anpeiyong
* @date: Created in 2019/11/21 8:54
* @since:
*/
@Service
public class AnnotationService {
public void output(){
System.out.println("組合注解成功。。。");
}
}
package com.an.main;
import com.an.annotation.AnnotationService;
import com.an.config.AnnotationConfig;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @description:
* @author: anpeiyong
* @date: Created in 2019/11/21 8:57
* @since:
*/
public class AnnotationMainTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext=new AnnotationConfigApplicationContext(AnnotationConfig.class);
AnnotationService annotationService=annotationConfigApplicationContext.getBean(AnnotationService.class);
annotationService.output();
annotationConfigApplicationContext.close();
}
}
結(jié)果:
組合注解成功。。。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
m1 Mac設(shè)置多jdk版本并動(dòng)態(tài)切換的實(shí)現(xiàn)
本文主要介紹 Mac 下如何安裝 JDK 并且多版本如何切換,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
一文搞懂SpringBoot如何利用@Async實(shí)現(xiàn)異步調(diào)用
異步調(diào)用幾乎是處理高并發(fā),解決性能問題常用的手段,如何開啟異步調(diào)用?SpringBoot中提供了非常簡單的方式,就是一個(gè)注解@Async。今天我們重新認(rèn)識(shí)一下@Async,以及注意事項(xiàng)2022-09-09
Spring技巧之如何動(dòng)態(tài)讀取配置文件
這篇文章主要介紹了Spring技巧之如何動(dòng)態(tài)讀取配置文件的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
SpringSecurity的防Csrf攻擊實(shí)現(xiàn)代碼解析
這篇文章主要介紹了SpringSecurity的防Csrf攻擊實(shí)現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
詳解基于java的Socket聊天程序——客戶端(附demo)
這篇文章主要介紹了詳解基于java的Socket聊天程序——客戶端(附demo),客戶端設(shè)計(jì)主要分成兩個(gè)部分,分別是socket通訊模塊設(shè)計(jì)和UI相關(guān)設(shè)計(jì)。有興趣的可以了解一下。2016-12-12
Mybatis-Plus可能導(dǎo)致死鎖的問題分析及解決辦法
這篇文章給大家主要介紹了Mybatis-Plus可能導(dǎo)致死鎖的問題分析及解決辦法,文中通過代碼示例給大家介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-12-12
使用Java快速將Web中表格轉(zhuǎn)換成Excel的方法
在平時(shí)做系統(tǒng)項(xiàng)目時(shí),經(jīng)常會(huì)需要做導(dǎo)出功能,下面這篇文章主要給大家介紹了關(guān)于使用Java快速將Web中表格轉(zhuǎn)換成Excel的相關(guān)資料,需要的朋友可以參考下2023-06-06

