Spring组合注解与元注解
2024-10-19 10:58:42
目录
注解说明
- 元注解:可以注解到别的注解上的注解,所以元注解首先基于条件@Target({ElementType.TYPE}) ,目标使用在类文件上 。
- 组合注解:连个元注解组合在一起的注解,注解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();
}
最新文章
- C++内联函数
- Egret Wing3 商城插件下载和使用
- MSSQL 2005数据库与SP4补丁安装
- 网站安全通用防护代码(C#版本源码提供)
- 高性能Java Web 页面静态化技术(原创)
- Eclipse 将projectBuild Path中引用的jar包自己主动复制到WEB-INF下的lib目录下
- node.js 入门(一)安装
- Spring技术内幕:Spring AOP的实现原理(一)
- FORM提交请求后自动打开输出EDITOR_PKG.REPORT
- 图片首尾平滑轮播(JS原生方法—节流)<;原创>;
- Java多线程窥探
- 在web工程中设置首页的页面
- Jenkins job之间依赖关系配置(联动构建)
- 调研一类软件的发展演变( 1000-2000 words, in Chinese)
- 如何验证代理ip的正确性
- 【CF472G】Design Tutorial 压位
- Eclipse编写ExtJS5卡死问题
- Canvas---clearRect()清除圆形区域
- n阶楼梯,一次走1,2,3步,求多少种不同走法
- linux各种系统下载地址
热门文章
- map2bean &; bean2map
- 第一课 Dubbo背景及原理
- Part 1 to 10 Basic in C#
- 记录线上APP一个排序比较引发的崩溃 Comparison method violates its general contract!
- Django 小实例S1 简易学生选课管理系统 总目录
- 力扣 - 剑指 Offer 22. 链表中倒数第k个节点
- 重写(Override)与重载(Overload)区别
- [bzoj4553]序列
- java及python调用RabbitMQ
- Codeforces 679E - Bear and Bad Powers of 42(线段树+势能分析)