通过实例解析Spring组合注解与元注解
这篇文章主要介绍了通过实例解析Spring组合注解与元注解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
1、概述
1.1、Spring提供了大量的注解,
尤其是相同的注解用到各个类中,会相当的啰嗦;
1.2、元注解:
可以注解到别的注解上的注解;
组合注解:
被注解注解的注解称为组合注解;
组合注解具备元注解的功能,Spring的很多注解都可以作为元注解;
1.3、案例
packagecom.an.config;
importcom.an.annotation.MyAnnotation;
/**
*@description:
*@author:anpeiyong
*@date:Createdin2019/11/218:57
*@since:
*/
@MyAnnotation(value="com.an")
publicclassAnnotationConfig{
}
packagecom.an.annotation;
importorg.springframework.context.annotation.ComponentScan;
importorg.springframework.context.annotation.Configuration;
importjava.lang.annotation.ElementType;
importjava.lang.annotation.Retention;
importjava.lang.annotation.RetentionPolicy;
importjava.lang.annotation.Target;
/**
*@description:
*@author:anpeiyong
*@date:Createdin2019/11/218:47
*@since:
*/
@Target(value=ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Configuration
@ComponentScan
public@interfaceMyAnnotation{
String[]value()default{};
}
packagecom.an.annotation;
importorg.springframework.stereotype.Service;
/**
*@description:
*@author:anpeiyong
*@date:Createdin2019/11/218:54
*@since:
*/
@Service
publicclassAnnotationService{
publicvoidoutput(){
System.out.println("组合注解成功。。。");
}
}
packagecom.an.main;
importcom.an.annotation.AnnotationService;
importcom.an.config.AnnotationConfig;
importorg.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
*@description:
*@author:anpeiyong
*@date:Createdin2019/11/218:57
*@since:
*/
publicclassAnnotationMainTest{
publicstaticvoidmain(String[]args){
AnnotationConfigApplicationContextannotationConfigApplicationContext=newAnnotationConfigApplicationContext(AnnotationConfig.class);
AnnotationServiceannotationService=annotationConfigApplicationContext.getBean(AnnotationService.class);
annotationService.output();
annotationConfigApplicationContext.close();
}
}
结果:
组合注解成功。。。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。