在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的常用注解的更多相关文章

  1. spring boot 的常用注解使用 总结

    附:Spring Boot 官方文档学习(一)入门及使用见https://www.cnblogs.com/larryzeal/p/5799195.html @RestController和@Reque ...

  2. spring boot 的常用注解

    SpringBoot用于简化Spring应用的搭建,开发及部署:该框架采用注解的方式进行配置可以很方便的构建Spring应用. 1. @SpringBootApplication @SpringBoo ...

  3. Spring Boot 二十个注解

    Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...

  4. spring boot @ConditionalOnxxx相关注解总结

    Spring boot @ConditionalOnxxx相关注解总结 下面来介绍如何使用@Condition public class TestCondition implements Condit ...

  5. Spring boot 使用的注解有哪些?

    Spring boot 使用的注解有哪些? 注解 作用 @SpringBootApplication 等价于 @Configuration + @EnableAutoConfiguration + @ ...

  6. spring 以及 spring mvc 中常用注解整理

    spring 以及 spring mvc 中常用注解整理 @RequestMapping(映射路径) @Autowired(注入 bean 对象) 例如: @Autowired private Bas ...

  7. (32)Spring Boot使用@SpringBootApplication注解,从零开始学Spring Boot

    [来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论] 如果看了我之前的文章,这个节你就可以忽略了,这个是针对一些刚入门的选手存在的困惑进行写的一篇文章. 很多Spring Boot开发者总是使用 ...

  8. Spring中的常用注解

    Spring中的常用注解 1.@Controller 标识一个该类是Spring MVC controller处理器,用来创建处理http请求的对象.

  9. spring boot 中@Autowired注解无法自动注入的错误

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...

  10. Spring Boot中@Scheduled注解的使用方法

    Spring Boot中@Scheduled注解的使用方法 一.定时任务注解为@Scheduled,使用方式举例如下 //定义一个按时间执行的定时任务,在每天16:00执行一次. @Scheduled ...

随机推荐

  1. OpenMMLab AI实战营 第一课笔记

    OpenMMLab AI实战营 第一课笔记 OpenMMLab AI实战营第一节课由子豪兄讲解,课程主要内容主要围绕计算机视觉和OpenMMLab开源算法体系以及机器学习和神经网络简介进行展开.这里要 ...

  2. 【狂神说Java】Java零基础学习笔记-Java数组

    [狂神说Java]Java零基础学习笔记-Java数组 Java数组01:数组的定义 数组是相同类型数据的有序集合. 数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成. 其中,每一个数 ...

  3. ANOSIM分析

    ANOSIM分析(analysis of similarities)即相似性分析,主要用于分析高维数据组间相似性,为数据间差异显著性评价提供依据.在一些高维数据分析中,需要使用PCA.PCoA.NMD ...

  4. Jetbrains fleet 配置 C++开发环境(基于CMAKE和MinGW)

    Jetbrains fleet 配置 C++开发环境 1. 安装 Jetbrains Fleet 到Fleet下载页面下载Toolbox并安装 Jetbrains-Fleet下载页 安装完成后在任务栏 ...

  5. No match for argument: kde-l10n-Chinese 报错

    背景:安装 kde-l10n-Chinese 软件包报错 原因:该安装包适用于 centos7 系统,若为 centos8 则无法安装 解决办法:更换适配 centos 8 中文安装包 yum ins ...

  6. salesforce零基础学习(一百四十二)在Formula字段中如何通过Datetime字段显示Local Time(适配DST)

    背景: 我们需求是显示Date Time类型的Time信息,比如我们想要在report中基于Hour Of Created Date进行分组,从而想要了解到一段时间内什么时间是数据创建的高峰期,不同的 ...

  7. 一致性hash和普通hash和hash槽

    普通hash Hash函数:一般翻译做散列.杂凑,或音译为哈希,是把任意长度的输入(又叫做预映射pre-image)通过散列算法变换成固定长度的输出,该输出就是散列值.碰撞(冲突):如果两个关键字通过 ...

  8. ORACLE存储过程,函数,包,游标

    1.  PL/SQL语句块PL/SQL语句块只适用于Oracle数据库,使用时临时保存在客户端,而不是保存在数据库.基本语法: declare 变量声明.初始化 begin 业务处理.逻辑代码 exc ...

  9. thewall靶机

    includes.php 内有文件读取漏洞 一开始是想着直接用为协议写入一句话木马但是后来发现不行 因为他的文件读取方式长这样 点击查看代码 <?php include ('/var/www/h ...

  10. .NET Core 中如何构建一个弹性的 HTTP 请求机制?

    1. 理解弹性 HTTP 请求机制 什么是弹性? 弹性是指系统在面对故障或异常情况时,能够保持或快速恢复到正常状态的能力.在 HTTP 请求的上下文中,弹性意味着当请求失败时,系统能够自动采取一系列措 ...