通過實例解析Spring組合注解與元注解
這篇文章主要介紹了通過實例解析Spring組合注解與元注解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
1、概述
1.1、Spring提供了大量的注解,
尤其是相同的注解用到各個類中,會相當?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();
}
}
結果:
組合注解成功。。。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
m1 Mac設置多jdk版本并動態(tài)切換的實現(xiàn)
本文主要介紹 Mac 下如何安裝 JDK 并且多版本如何切換,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-08-08
一文搞懂SpringBoot如何利用@Async實現(xiàn)異步調(diào)用
異步調(diào)用幾乎是處理高并發(fā),解決性能問題常用的手段,如何開啟異步調(diào)用?SpringBoot中提供了非常簡單的方式,就是一個注解@Async。今天我們重新認識一下@Async,以及注意事項2022-09-09
SpringSecurity的防Csrf攻擊實現(xiàn)代碼解析
這篇文章主要介紹了SpringSecurity的防Csrf攻擊實現(xiàn)代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-03-03
詳解基于java的Socket聊天程序——客戶端(附demo)
這篇文章主要介紹了詳解基于java的Socket聊天程序——客戶端(附demo),客戶端設計主要分成兩個部分,分別是socket通訊模塊設計和UI相關設計。有興趣的可以了解一下。2016-12-12

