Spring Boot的常用注解
在Spring Boot中,注解(Annotation)是核心特性之一,广泛用于配置和简化开发。以下是Spring Boot中一些常用的注解及其示例:
1. @SpringBootApplication
@SpringBootApplication
是一个组合注解,包括了@Configuration
、@EnableAutoConfiguration
和@ComponentScan
。它通常用在主类上,标识一个Spring Boot应用的入口。
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);
}
}
2. @RestController
@RestController
结合了@Controller
和@ResponseBody
,用于创建RESTful Web服务。
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!";
}
}
3. @RequestMapping
@RequestMapping
用于将HTTP请求映射到处理器方法上。可以用于类或方法级别。
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class ApiController {
@RequestMapping(value = "/users", method = RequestMethod.GET)
public List<String> getUsers() {
return Arrays.asList("User1", "User2", "User3");
}
}
4. @Autowired
@Autowired
用于自动注入依赖关系。它可以应用于构造函数、方法或字段上。
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class MyService {
private final MyRepository myRepository;
@Autowired
public MyService(MyRepository myRepository) {
this.myRepository = myRepository;
}
// 其他业务逻辑
}
5. @Service
@Service
用于标识服务层的组件,Spring会自动将其注册为Bean。
import org.springframework.stereotype.Service;
@Service
public class UserService {
public String getUserById(Long id) {
// 业务逻辑
return "User" + id;
}
}
6. @Repository
@Repository
用于标识数据访问层的组件,通常与数据库交互。
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Long> {
// 自定义查询方法
}
7. @Configuration
@Configuration
用于定义配置类,该类可以包含@Bean
注解的方法,这些方法会返回Spring管理的Bean。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
8. @EnableAutoConfiguration
@EnableAutoConfiguration
告诉Spring Boot基于类路径中的Jar依赖自动配置Spring应用上下文。通常不直接使用,而是通过@SpringBootApplication
间接使用。
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@EnableAutoConfiguration
public class MyApplication {
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
9. @Component
@Component
泛指Spring管理的组件。可以标记为Bean的类,Spring会自动扫描并注册这些类。
import org.springframework.stereotype.Component;
@Component
public class MyComponent {
public void doSomething() {
// 组件逻辑
}
}
10. @Value
@Value
用于注入外部化配置的属性值。
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class MyComponent {
@Value("${app.name}")
private String appName;
public void printAppName() {
System.out.println(appName);
}
}
这些注解极大地简化了Spring Boot应用的配置和开发工作,使得开发者可以专注于业务逻辑。
Spring Boot的常用注解的更多相关文章
- spring boot 的常用注解使用 总结
附:Spring Boot 官方文档学习(一)入门及使用见https://www.cnblogs.com/larryzeal/p/5799195.html @RestController和@Reque ...
- spring boot 的常用注解
SpringBoot用于简化Spring应用的搭建,开发及部署:该框架采用注解的方式进行配置可以很方便的构建Spring应用. 1. @SpringBootApplication @SpringBoo ...
- Spring Boot 二十个注解
Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...
- spring boot @ConditionalOnxxx相关注解总结
Spring boot @ConditionalOnxxx相关注解总结 下面来介绍如何使用@Condition public class TestCondition implements Condit ...
- Spring boot 使用的注解有哪些?
Spring boot 使用的注解有哪些? 注解 作用 @SpringBootApplication 等价于 @Configuration + @EnableAutoConfiguration + @ ...
- spring 以及 spring mvc 中常用注解整理
spring 以及 spring mvc 中常用注解整理 @RequestMapping(映射路径) @Autowired(注入 bean 对象) 例如: @Autowired private Bas ...
- (32)Spring Boot使用@SpringBootApplication注解,从零开始学Spring Boot
[来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论] 如果看了我之前的文章,这个节你就可以忽略了,这个是针对一些刚入门的选手存在的困惑进行写的一篇文章. 很多Spring Boot开发者总是使用 ...
- Spring中的常用注解
Spring中的常用注解 1.@Controller 标识一个该类是Spring MVC controller处理器,用来创建处理http请求的对象.
- spring boot 中@Autowired注解无法自动注入的错误
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...
- Spring Boot中@Scheduled注解的使用方法
Spring Boot中@Scheduled注解的使用方法 一.定时任务注解为@Scheduled,使用方式举例如下 //定义一个按时间执行的定时任务,在每天16:00执行一次. @Scheduled ...
随机推荐
- 优化博客Ⅱ-CDN加速
CDN加速 自从有了第一次博客优化经验,我就越发对优化感兴趣了嘿嘿(✧∇✧). 看着博客首页打开时长为1200ms左右,我又开始琢磨有什么办法能再给网站提提速,让访问时间降低到1000ms以下,这时候 ...
- windows平台下,web与app交互方式探索
前言 web与app孰优孰劣暂且不争论,也许一方永远代替不了另一方.一个系统有可能同时包含web和app:web和app紧密配合下,才能给用户更好的体验.web如何将信息传达给app?这就是本文要探索 ...
- Arcgis加载Geoserver矢量切片
原帖地址 洒家废物 - Arcgis加载Geoserver矢量切片 准备点线面图层并发布图层组 此处我准备了石家庄市的县界名称(点).高速公路(线).县界(面),依次发布geoserver服务,创建图 ...
- w3cschool-Bootstrap 教程
Bootstrap 简介 什么是 Bootstrap? Bootstrap 是一个用于快速开发 Web 应用程序和网站的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的 ...
- ffmpeg简易播放器(1)--了解视频格式
视频帧 对于一份视频,实质上是多张图片高速播放形成的.每一张图片即为该视频的一帧.而每秒钟播放的图片张数便为所谓的帧率(Frame Rate/Frame Per Second).常见的帧率有24fps ...
- 了解GraphRAG
了解GraphRAG 转载:从零实现大模型-GraphRAG,构建LLM中的关系数据库 开源地址:https://github.com/microsoft/graphrag 论文:From Local ...
- DCT实现水印嵌入与提取(带攻击)
问题: 想要用DCT技术,在Matlib上实现水印的隐藏和提取(带GUI界面),且加上一些攻击(噪声.旋转.裁剪),以及用NC值评判! 流程 选择载体 [filename,pathname]=uige ...
- Linux 文件压缩和解压缩命令
Linux 文件压缩和解压缩命令 在Linux操作系统中,文件压缩和解压缩是日常管理和维护任务中的重要一环.通过压缩文件,可以显著减少存储空间的使用,并加快网络传输速度.Linux提供了多种压缩和解压 ...
- [记录点滴] 记录一次用 IntelliJ IDEA遇到scope provided 的坑
0x00 问题 最近在调试一个网上的项目,结果遇到两个问题,特此记录下解决过程. 问题: 某一个jar包有版本冲突 某一个类,居然在IntelliJ IDEA中运行调试时候找不到 0x01 解决途径 ...
- Doris数据库使用
1.表结构设置 [1]建表时指定副本数量:relication_num [2]排序键 明细模型:DUPLICATE KEY(site_id, city_code) 聚合模型:AGGREGATE KEY ...