目录

注解说明

  1. 元注解:可以注解到别的注解上的注解,所以元注解首先基于条件@Target({ElementType.TYPE}) ,目标使用在类文件上 。
  2. 组合注解:连个元注解组合在一起的注解,注解A使用了注解B,那么注解A就叫组合注解,注解A会继承注解B的功能。

源代码

  springBoot的入口注解@SpringBootApplication是一个组合注解,由注解@EnableAutoConfiguration、@SpringBootConfiguration,@ComponentScan,三个注解组成,功能继承了三个注解的功能。SpringBootApplication源码

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package org.springframework.boot.autoconfigure; import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurationExcludeFilter;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.TypeExcludeFilter;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan.Filter;
import org.springframework.core.annotation.AliasFor; @Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration // 元注解
@EnableAutoConfiguration // 元注解
@ComponentScan( // 元注解
excludeFilters = {@Filter(
type = FilterType.CUSTOM,
classes = {TypeExcludeFilter.class}
), @Filter(
type = FilterType.CUSTOM,
classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
@AliasFor(
annotation = EnableAutoConfiguration.class
)
Class<?>[] exclude() default {}; @AliasFor(
annotation = EnableAutoConfiguration.class
)
String[] excludeName() default {}; @AliasFor(
annotation = ComponentScan.class,
attribute = "basePackages"
)
String[] scanBasePackages() default {}; @AliasFor(
annotation = ComponentScan.class,
attribute = "basePackageClasses"
)
Class<?>[] scanBasePackageClasses() default {};
}

使用范例

  组合注解可以将多个元注解的功能集中到一个注解上 ,方便使用,简化代码。 写一个配置文件MyConfiguration将@Configuration 、@ComponentScan 两个注解组合在一起

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration //1元注解
@ComponentScan //2元注解
public @interface MyConfiguration {
String[] value() default {}; //3
}

  配置类使用注解

@MyConfiguration("springboot.annotation")//1使用自定义注解扫描文件夹,加载文件
public class DemoConfig {
}

  写一个TestBean放在 springboot.annotation下

@Service
public class DemoService {
public void outputResult(){
System.out.println("从组合注解配置照样获得的bean");
}
}

  Main方法测试

public class Main {public static void main(String[] args) {
  AnnotationConfigApplicationContext context =
    new AnnotationConfigApplicationContext(DemoConfig.class);
  DemoService demoService = context.getBean(DemoService.class);
  demoService.outputResult();//输出:从组合注解配置照样获得的bean
  context.close();
}

最新文章

  1. C++内联函数
  2. Egret Wing3 商城插件下载和使用
  3. MSSQL 2005数据库与SP4补丁安装
  4. 网站安全通用防护代码(C#版本源码提供)
  5. 高性能Java Web 页面静态化技术(原创)
  6. Eclipse 将projectBuild Path中引用的jar包自己主动复制到WEB-INF下的lib目录下
  7. node.js 入门(一)安装
  8. Spring技术内幕:Spring AOP的实现原理(一)
  9. FORM提交请求后自动打开输出EDITOR_PKG.REPORT
  10. 图片首尾平滑轮播(JS原生方法—节流)&lt;原创&gt;
  11. Java多线程窥探
  12. 在web工程中设置首页的页面
  13. Jenkins job之间依赖关系配置(联动构建)
  14. 调研一类软件的发展演变( 1000-2000 words, in Chinese)
  15. 如何验证代理ip的正确性
  16. 【CF472G】Design Tutorial 压位
  17. Eclipse编写ExtJS5卡死问题
  18. Canvas---clearRect()清除圆形区域
  19. n阶楼梯,一次走1,2,3步,求多少种不同走法
  20. linux各种系统下载地址

热门文章

  1. map2bean &amp; bean2map
  2. 第一课 Dubbo背景及原理
  3. Part 1 to 10 Basic in C#
  4. 记录线上APP一个排序比较引发的崩溃 Comparison method violates its general contract!
  5. Django 小实例S1 简易学生选课管理系统 总目录
  6. 力扣 - 剑指 Offer 22. 链表中倒数第k个节点
  7. 重写(Override)与重载(Overload)区别
  8. [bzoj4553]序列
  9. java及python调用RabbitMQ
  10. Codeforces 679E - Bear and Bad Powers of 42(线段树+势能分析)