Spring Boot 注解的使用

本文详细介绍了SpringBoot中常用的注解及其应用场景,包括@Autowired、@RestController、@RequestMapping及其衍生注解,如@GetMapping、@PostMapping等,并解释了这些注解如何简化Web应用的开发。

 Spring Boot 优于Spring mvc ,SSM,SSH 的一个亮点就是他使用了好多的注解。

1. @Autowired 

这个注解的作用是将其他的类,接口引入,类似于之前的类的初始化等,用这个注解,类中或接口的方法就可以直接调用了。

这个注解和@Inject,@Resource 作用类似,都能注入类, 接口,但是区别我就不知道了。

2. @RestController

 这个注解的作用是告诉Servlet 这个类是一个控制器,当前台调用后台的时候,根据名称就能找到这个控制类,然后去执行里面的方法。他类似于Spring mvc 中的@Controller,他继承自@Controller。

3. @RequestMapping 和他的衍生品 @GetMapping,@PostMapping,@PutMapping,@DeleteMapping,@PutMapping

这个注解的作用是当前台界面调用Controller处理数据时候告诉控制器怎么操作。get 对应查询,put 对应更新,post 对应增加, delete 对应删除。

4. @RequestParam,@PathParm,@PathVariable和@@RequestBody

这四个注解都是用来传参数的,第一个是用来传递http://localhost:8080/page1?id=1 这种用的。第二个和第三个用来处理http://localhost:8080/page1/1这种传参数的,后面这个是用来传对象用的。

  • @GetMapping = @RequestMapping(method = RequestMethod.GET)
  • @PostMapping = @RequestMapping(method = RequestMethod.POST)
  • @PutMapping = @RequestMapping(method = RequestMethod.PUT)
  • @DeleteMapping = @RequestMapping(method = RequestMethod.DELETE)
### Spring Boot 注解使用指南 在 Spring Boot 中,注解是实现自动配置和组件管理的核心机制。通过合理的注解使用,可以简化配置、提高代码可读性,并提升开发效率。以下是一些关键注解及其用途。 #### 核心组件注解 Spring Boot 提供了一系列注解,用于标记类为 Spring 容器中的组件,并赋予其特定的语义角色。这些注解包括: - `@Component`:通用的组件注解,用于标记一个类为 Spring 的组件,使其能够被自动扫描并注册为 Spring 容器中的 Bean。 - `@Service`:用于标记业务逻辑层的服务类,通常用于服务层组件。 - `@Repository`:用于标记数据访问层的 DAO 类,通常用于与数据库交互的组件。 - `@Controller`:用于标记 Web 层的控制器类,通常用于返回视图名称。 - `@RestController`:结合了 `@Controller` 和 `@ResponseBody`,用于直接返回 JSON 数据的 REST 控制器[^3]。 ```java import org.springframework.stereotype.Component; @Component public class MyComponent { // 业务逻辑 } ``` ```java import org.springframework.stereotype.Service; @Service public class MyService { // 业务逻辑 } ``` ```java import org.springframework.stereotype.Repository; @Repository public class MyRepository { // 数据访问逻辑 } ``` ```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/hello") public String sayHello() { return "Hello, Spring Boot!"; } } ``` #### 自动配置相关注解 Spring Boot 的自动配置机制依赖于条件注解来决定是否应用某个配置。常见的条件注解包括: - `@ConditionalOnClass`:只有在类路径上存在指定类时,才启用该配置。 - `@ConditionalOnProperty`:只有在指定的属性存在并具有特定值时,才启用该配置[^2]。 ```java import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.context.annotation.Configuration; @Configuration @ConditionalOnClass(name = "com.example.SomeClass") public class MyAutoConfiguration { // 自动配置逻辑 } ``` ```java import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Configuration; @Configuration @ConditionalOnProperty(name = "my.feature.enabled", havingValue = "true") public class MyFeatureConfiguration { // 特性启用时的配置逻辑 } ``` #### 应用启动注解 `@SpringBootApplication` 是 Spring Boot 应用的主注解,它结合了 `@EnableAutoConfiguration` 和 `@ComponentScan`。如果不想使用 `@SpringBootApplication`,可以分别使用这两个注解来定义相同的行为[^1]。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration @ComponentScan public class MyApplication { public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } } ``` #### 调试自动配置 Spring Boot 的自动配置定义在 `spring-boot-autoconfigure` 包下的 `META-INF/spring.factories` 文件中。可以通过查看该文件了解 Spring Boot 是如何加载自动配置类的[^2]。 ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值