From 939cf05db27b4d81190ecf1d72bb8d1512a9eb2b Mon Sep 17 00:00:00 2001 From: Cxx0822 <1556464090@qq.com> Date: Tue, 30 Dec 2025 19:03:46 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0=E6=B3=A8=E8=A7=A3?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/.vitepress/theme/router/index.ts | 3 +- .../SpringBoot/SpringBoot-Annotation.md | 72 +++++++++++++++++++ ...ingBoot-Skills.md => SpringBoot-Skills.md} | 0 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 docs/Web-Backend/SpringBoot/SpringBoot-Annotation.md rename docs/Web-Backend/SpringBoot/{SpingBoot-Skills.md => SpringBoot-Skills.md} (100%) diff --git a/docs/.vitepress/theme/router/index.ts b/docs/.vitepress/theme/router/index.ts index f98560e..11e588a 100644 --- a/docs/.vitepress/theme/router/index.ts +++ b/docs/.vitepress/theme/router/index.ts @@ -32,8 +32,9 @@ export const routers = [ { text: 'Spring MVC简介', link: '/Web-Backend/SpringBoot/SpringMVC' }, { text: 'SpringBoot Starter原理', link: '/Web-Backend/SpringBoot/SpringBootStarter' }, { text: 'SpringBoot Bean简介', link: '/Web-Backend/SpringBoot/SpringBootBean' }, + { text: 'SpringBoot注解', link: '/Web-Backend/SpringBoot/SpringBoot-Annotation' }, { text: 'SpringBoot3原生镜像', link: '/Web-Backend/SpringBoot/SpringBoot3-GraalVM' }, - { text: 'SpingBoot技巧', link: '/Web-Backend/SpringBoot/SpingBoot-Skills' }, + { text: 'SpringBoot技巧', link: '/Web-Backend/SpringBoot/SpringBoot-Skills' }, { text: 'SpringBoot Common', link: '/Web-Backend/SpringBoot/SpringBoot-Common' } ] }, diff --git a/docs/Web-Backend/SpringBoot/SpringBoot-Annotation.md b/docs/Web-Backend/SpringBoot/SpringBoot-Annotation.md new file mode 100644 index 0000000..5d9257c --- /dev/null +++ b/docs/Web-Backend/SpringBoot/SpringBoot-Annotation.md @@ -0,0 +1,72 @@ +--- + title: SpingBoot注解 + date: 2025-12-30 +--- + +# 一、注解 +## 1.1 定义 +  注解(Annotation)是 JDK5.0 引入的特性,可以理解为:给代码添加的 “元数据”(描述数据的数据),就像给代码贴标签,**本身不直接影响代码执行,但可以被编译器、框架(如 SpringBoot)读取并做相应处理**。 + +## 1.2 分类 +| 类型 | 核心作用 | 典型示例 | +|----------------|--------------------|------------------| +| 源码注解 | 仅在源码编译阶段生效,编译后注解消失 | `@Override` | +| 编译时注解 | 编译期生效,注解信息保留到class文件,但JVM运行时不加载 | Lombok的`@Data` | +| 运行时注解 | 整个生命周期都存在(源码→编译→运行),可通过反射动态获取注解信息 | SpringBoot的`@RestController`、`@Service`、`@Transactional` | + +## 1.3 示例 +```java +import java.lang.annotation.*; + +// 1. 注解的元注解(描述注解的注解) +@Target(ElementType.METHOD) // 注解作用在方法上 +@Retention(RetentionPolicy.RUNTIME) // 运行时保留,可通过反射获取 +@Documented // 生成Javadoc时包含该注解 +public @interface MyAnnotation { + // 注解的属性(类似方法,可设置默认值) + String value() default "默认描述"; + int num() default 0; +} + +// 2. 使用自定义注解 +public class AnnotationTest { + @MyAnnotation(value = "测试方法", num = 10) + public void test() { + System.out.println("执行测试方法"); + } + + // 3. 通过反射读取注解 + public static void main(String[] args) throws NoSuchMethodException { + // 获取方法对象 + java.lang.reflect.Method method = AnnotationTest.class.getMethod("test"); + // 判断方法是否有该注解 + if (method.isAnnotationPresent(MyAnnotation.class)) { + // 获取注解实例 + MyAnnotation annotation = method.getAnnotation(MyAnnotation.class); + // 读取注解属性 + System.out.println("注解value:" + annotation.value()); // 输出:测试方法 + System.out.println("注解num:" + annotation.num()); // 输出:10 + } + } +} +``` + +  @Target:指定注解能作用的位置(如ElementType.METHOD= 方法、ElementType.TYPE= 类 / 接口、ElementType.FIELD= 字段)。 +  @Retention:指定注解的保留阶段(RUNTIME是 SpringBoot 注解最常用的) +  首先定义注解的@Target和@Retention信息,然后设置注解的属性,类似于方法的参数,比如这里的value和num,需要在使用的时候通过命名参数的形式传递过来。如果是RUNTIME类型的注解,可以通过反射来获取方法和注解的参数,实现自定义逻辑功能。 + +# 二、SpringBoot核心注解 +## 2.1 启动类注解 +  @SpringBootApplication为SpringBoot启动类注解,由以下三个注解组成: +1. @Configuration:标记类为配置类(替代 XML 配置) +2. @EnableAutoConfiguration:开启自动配置(SpringBoot 核心,自动配置 Tomcat、数据库连接等) +3. @ComponentScan:扫描当前包及子包下的 @Component、@Service 等注解的类,纳入 Spring 容器管理 + +## 2.2 组件注册注解 +| 注解 | 作用 | 使用场景 | +|----------------|--------------------------|------------------------------------------------------------------| +| @Component | 通用组件注解 | 通用工具类、非业务层类 | +| @Controller | 控制器注解 | MVC 的控制层(返回页面) | +| @RestController| REST 控制器 | API 接口层(返回 JSON/XML,= @Controller + @ResponseBody) | +| @Service | 服务层注解 | 业务逻辑层 | +| @Repository | 数据访问层注解 | DAO 层 / 持久层(如 MyBatis 的 Mapper 接口) | \ No newline at end of file diff --git a/docs/Web-Backend/SpringBoot/SpingBoot-Skills.md b/docs/Web-Backend/SpringBoot/SpringBoot-Skills.md similarity index 100% rename from docs/Web-Backend/SpringBoot/SpingBoot-Skills.md rename to docs/Web-Backend/SpringBoot/SpringBoot-Skills.md