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 ...
随机推荐
- WebSocket一篇就够了-copy
WebSocket介绍与原理 WebSocket protocol 是HTML5一种新的协议.它实现了浏览器与服务器全双工通信(full-duplex).一开始的握手需要借助HTTP请求完成. --百 ...
- Java接口-详解
一.基本概念 接口(Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合.接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法. 如果一个类只由 ...
- 经典算法的Java实现
1.快速排序描述 1.每一轮排序选择一个基准点(pivot)进行分区 1.让小于基准点的元素进入一个分区,大于基准点的元素进入另一个分区 2.当分区完成时,基准点元素的位置就是其最终位置 2.在子分区 ...
- biancheng-MySQL教程
目录http://c.biancheng.net/mysql/ 1数据库入门2MySQL的安装和配置3MySQL数据库的基本操作4数据库设计5MySQL数据类型和存储引擎6MySQL数据表的基本操作7 ...
- shell脚本输出带文本颜色背景颜色自定义样式格式内容
shell脚本中 echo 和 printf 都可以输出内容.示例1: echo -e "\033[43;35m david use echo say Hello World \033[0m ...
- superset 1.3版本WIN10安装实录
首先说下,为什么要这么做,因为二开需要,二开要有源码,然后对源码修改,编译,所以不能通过类似https://zhuanlan.zhihu.com/p/271695878这种方式,直接安装: 1.去Gi ...
- 从源码解析golang Timer定时器体系的来龙去脉
大家好,我是思无邪,某go中厂开发工程师,也是OSPP2024的学生参与者! 如果你觉得我的文章有帮助,记得三连支持一下哦! 目前正在深入研究源码,与你们一起进步,共同攻克编程难关! 欢迎关注我的公众 ...
- ubuntu通过tar包安装mysql5.7.21
作者:zuoguohui 一.场景:最近想搞mysql主从复制,需要在两台服务器上安装mysql,之前有一台已经装好了mysql5.7.21,于是在另外一台上也装mysql5.7.21,安装过程中碰到 ...
- Java SPI机制及实现
一.简介 SPI 的全称为 (Service Provider Interface),是 JDK 内置的一种服务提供发现机制.主要由工具类 java.util.ServiceLoader 提供相应的支 ...
- [POI2014] HOT-Hotels 加强版题解
好好好,太好了这题,太好了. 首先有一点是很明显的: 对于一个合法的答案 \((i,j,k)\),必有一点 \(p\),使 \(dis(i,p)=dis(j,p)=dis(k,p)\) 且三点到 \( ...