Spring Boot 的核心注解
-
@SpringBootApplication:用于标注主启动类,表示该类是Spring Boot应用的入口。
-
@RestController:用于标注控制器类,表示该类是一个RESTful风格的控制器。
-
@RequestMapping:用于映射请求路径和处理方法。
-
@Autowired:用于自动注入依赖。
-
@Component:通用的组件注解,用于标注一个类为Spring的Bean。
-
@Repository:用于标注数据访问组件,通常用于与数据库交互。
-
@Service:用于标注服务组件,通常用于实现业务逻辑。
-
@Configuration:用于标注配置类,通常用于定义一些配置信息。
-
@Bean:用于在配置类中定义Bean。
-
@Value:用于注入配置文件中的值。
Spring Boot 的核心注解由哪几个注解组成
Spring Boot 的核心注解由以下几个注解组成:
-
@SpringBootApplication:这是一个复合注解,包括了@Configuration、@EnableAutoConfiguration和@ComponentScan三个注解。用于指定该类是Spring Boot 应用的入口类。
-
@Configuration:声明当前类是一个配置类,用于替代传统的XML配置文件。
-
@EnableAutoConfiguration:用于启用Spring Boot的自动配置机制,根据当前项目的依赖情况,自动配置Spring。
-
@ComponentScan:用于指定Spring扫描组件的基础包,可以自动扫描并注册Spring的Bean。
除了以上核心注解,Spring Boot 还有一些其他的常用注解,如:
-
@RestController:用于声明当前类是一个Restful风格的Controller。
-
@RequestMapping:用于映射URL和请求方法。
-
@Autowired:用于依赖注入。
-
@Value:用于注入配置属性值。
-
@Bean:用于声明一个Bean。
-
@ConditionalOnXXX:用于条件化配置,根据条件来决定是否加载某个Bean或配置。
Spring Boot 的核心注解作用
Spring Boot 的核心注解有以下几个作用:
-
@SpringBootApplication:用于启动类上,标注该类是一个 Spring Boot 应用的入口类。它包含了以下三个注解的功能:@Configuration、@EnableAutoConfiguration 和 @ComponentScan。
-
@RestController:用于标注控制器类,表明该类是 Spring MVC 的 Controller,并且类中的方法都会以 JSON 格式返回。
-
@RequestMapping:用于标注处理请求的方法,指定请求的 URL 地址。
-
@Autowired:用于自动装配 Bean,可以在 Spring 容器中自动查找匹配的 Bean,并自动注入到标注了 @Autowired 的字段、构造方法或 setter 方法中。
-
@EnableAutoConfiguration:用于自动配置 Spring Boot 应用,根据 classpath 下的依赖,自动加载相应的配置。
-
@ConfigurationProperties:用于绑定配置文件中的属性值到 Java 对象,通过将属性值设置到对应的字段中,方便获取和使用配置。
-
@ComponentScan:用于扫描指定的包或类,并将被标注了 @Component 或其派生注解的类注册为 Spring Bean。
除了上述核心注解,Spring Boot 还有很多其他的注解,如 @Value、@Order、@ConditionalOnProperty 等,它们在不同的场景下有不同的作用。
示例
- @SpringBootApplication
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}}
- @RestController
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class MyController {@GetMapping("/hello")public String hello() {return "Hello World!";}}
- @RequestMapping
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RequestMapping("/api")
public class MyController {@GetMapping("/hello")public String hello() {return "Hello World!";}}
- @Autowired
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;@Service
public class MyService {private MyRepository myRepository;@Autowiredpublic MyService(MyRepository myRepository) {this.myRepository = myRepository;}// ...
}
- @ComponentScan
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;@SpringBootApplication
@ComponentScan("com.example.myapp")
public class MyApplication {public static void main(String[] args) {SpringApplication.run(MyApplication.class, args);}}
总结
Spring Boot的核心注解可以分为以下几类:
-
@SpringBootApplication:这是一个组合注解,包含了@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan三个注解。通常用于启动类上,表示这是一个Spring Boot应用的入口。
-
@Controller:用于标识一个类是控制器,处理HTTP请求。
-
@RestController:是@Controller和@ResponseBody的组合注解,用于标识一个类是RESTful风格的控制器。
-
@Service:用于标识一个类是服务层组件。
-
@Repository:用于标识一个类是数据访问层组件。
-
@Configuration:用于标识一个类是配置类,通常与@Bean配合使用,用于定义Bean。
-
@RequestMapping:用于映射HTTP请求的URL路径,可以标注在类和方法上。可以指定请求的方式、URI路径等。
-
@PathVariable:用于获取URL路径中的变量值。
-
@RequestParam:用于获取请求参数的值。
-
@RequestBody:用于获取请求体的内容。
-
@ResponseBody:用于将方法的返回值转换为响应体的内容。
-
@Autowired:用于自动装配Bean,可以标注在构造方法、属性、Setter方法等位置。
-
@Value:用于注入配置文件中的属性值。
-
@Valid:用于标注在方法参数上,表示要对参数进行校验。
-
@EnableAutoConfiguration:用于开启自动配置功能,根据classpath下的依赖自动配置Spring Boot的各种功能。