欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

通过实例解析Spring组合注解与元注解

时间:2021-12-31 09:07:56|栏目:JAVA代码|点击:

这篇文章主要介绍了通过实例解析Spring组合注解与元注解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

1、概述

1.1、Spring提供了大量的注解,

尤其是相同的注解用到各个类中,会相当的??嗦;

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();
  }
}

结果:

组合注解成功。。。

上一篇:Java过滤器Filter详解

栏    目:JAVA代码

下一篇:微信公众号支付(二)实现统一下单接口

本文标题:通过实例解析Spring组合注解与元注解

本文地址:http://www.codeinn.net/misctech/188660.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有