百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

Spring Boot进阶教程——注解大全(建议收藏!)

ztj100 2024-12-15 18:01 36 浏览 0 评论

博客前言

在Spring1.x时代,还没出现注解,需要大量xml配置文件并在内部编写大量bean标签。Java5推出新特性annotation,为spring的更新奠定了基础。从Spring 2.X开始spring将xml配置中的对象ioc过程转化成了注解。Spring Boot之所以能够轻松地实现应用的创建及与其他框架快速集成,最核心的原因就在于它极大地简化了项目的配置,最大化地实现了“约定大于配置”的原则。但是注解种类之繁多,还能容易引起混淆,这才有了本文《SpringBoot进阶教程(六十四)注解大全》。

要想对SpringBoot注解有个更全面更清晰的认识,就需要分个类,分别是Spring注解、Spring Web注解、Spring Boot注解、Spring Scheduling注解和注解集合。大致可以将注解分为5大类,其中前4类是为了便于理解,分别从4个类别中抽取了一些单独介绍。而最后一个为注解集合,即可能会表含前面4种注解。

Spring注解

在Spring Core注解中,主要讨论Spring DI和Spring IOC中使用的Spring核心注释。众所周知,Spring DI和Spring IOC是Spring框架的核心概念。所以介绍 org.springframework.beans.factory.annotation org.springframework.context.annotation 包中的注解。这两个包中注解有很多,就抽取其中的15个注解。

Spring Core Annotations:

  • @Autowired
  • @Qualifier
  • @Bean
  • @Required
  • @Value
  • @DependsOn
  • @Lazy
  • @Lookup
  • @Primary
  • @Scope
  • @Profile
  • @Import
  • @ImportResource
  • @PropertySource
  • @PropertySources

单单 org.springframework.context.annotation 这个包下面,注解就有这老些,所以很难列出所有注解举例,只能抽一些常用的。文末会给出其它注解的作用和定义(尽量给全)。

1.1 @Autowired

@Autowired是一种注解,可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作,@Autowired标注可以放在成员变量上,也可以放在成员变量的set方法上,也可以放在任意方法上表示,自动执行当前方法,如果方法有参数,会在IOC容器中自动寻找同类型参数为其传值。

这里必须明确:@Autowired是根据类型进行自动装配的,如果需要按名称进行装配,则需要配合@Qualifier使用;

1.1.1 构造器注入

@RestController
public class UserController {
    private UserService userService;
 
    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }
}

1.1.2 setter方法注入

@RestController
public class UserController {
    private UserService userService;
 
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}

1.1.3 field反射注入

@RestController
public class UserController {
    @Autowired
    private UserService userService;
}

1.2 @Qualifier

上面已经说到@Autowired按类型装配Spring Bean。如果容器中有多个相同类型的bean,则框架将抛出NoUniqueBeanDefinitionException, 以提示有多个满足条件的bean进行自动装配。程序无法正确做出判断使用哪一个,通过将@Qualifier注解与我们想要使用的特定Spring bean的名称一起进行装配,Spring框架就能从多个相同类型并满足装配要求的bean中找到我们想要的,

@Component("studentInfo")
public class StudentInfo implements UserInfo {
    public String userName() {
        return "student";
    }
}

@Component("teacherInfo")
public class TeacherInfo implements UserInfo {
    public String userName {
        return "teacher";
    }
}

@Component
public class UserService {
    @Autowired
    @Qualifier("studentInfo")
    private UserInfo userInfo;

    //todo 
}

1.3 @Bean

@Bean是一个方法级别上的注解,主要用在@Configuration注解的类里,也可以用在@Component注解的类里。添加的bean的id为方法名。

@Configuration
public class BeanConfig {

    @Bean
    public Person userInfo() {
        return new UserInfo("toutou", 18);
    }
}

这个配置就等同于之前在xml里的配置:

<bean id="userInfo" class="com.test.UserInfo">
    <property name="age" value="18"/>
    <property name="name" value="请叫我头头哥 http://toutou.cnblogs.com"/>
</bean>

1.4 @Required

@Required 注释应用于 bean 属性的 setter 方法,它表明受影响的 bean 属性在配置时必须放在 XML 配置文件中,否则容器就会抛出一个 BeanInitializationException 异常。

@Required
void setUserName(String name) {
    this.name = name;
}
<bean class="com.test.UserInfo">
    <property name="name" value="请叫我头头哥 https://www.cnblogs.com/toutou/" />
</bean>

1.5 @Value

@Value将外部的值动态注入到Bean中。"注入外部的值"可以有很多种,它可以注入普通字符串、注入java 系统变量、注入表达式结果、注入其他Bean属性、将配置文件 *.properties 或 *. yml 李 配置的 属性 注入、注入文件资源和注入url资源等。

1.6 @DependsOn

Spring容器载入bean顺序是不确定的,Spring框架也没有约定特定载入顺序逻辑规范。@DependsOn注解可以定义在类和方法上,比如说A组件要依赖于B组件,那就是B组件需要比A组件先注册到IOC容器中。

public class FirstBean {

    @Autowired
    private SecondBean secondBean;
}

public class SecondBean {
    public SecondBean() {
        System.out.println("SecondBean init");
    }
}
@Configuration
public class BeanConfig {

    @Bean("firstBean")
    @DependsOn(value = {
        "secondBean"
    })
    public FirstBean firstBean() {
        return new FirstBean();
    }

    @Bean("secondBean")
    public SecondBean secondBean() {
        return new SecondBean();
    }
}

1.7 @Lazy

@Lazy注解用于标识bean是否需要延迟加载。Spring IoC容器一般都会在启动的时候实例化所有单实例bean,如果想要Spring在启动的时候延迟加载A,即在调用B的时候再去初始化,则可以使用@Lazy注解。

public class FirstBean {
    public void test() {
        System.out.println("FirstBean Class");
    }
}
public class SecondBean {
    public void test() {
        System.out.println("SecondBean Class");
    }
}
@Configuration
public class AppConfig {

    @Lazy(value = true)
    @Bean
    public FirstBean firstBean() {
        return new FirstBean();
    }

    @Bean
    public SecondBean secondBean() {
        return new SecondBean();
    }
}

1.8 @Lookup

@Lookup的注解是一个作用在方法上的注解,被其标注的方法会被重写,然后根据其返回值的类型,容器调用BeanFactory的getBean()方法来返回一个bean。

1.9 @Primary

@Primary与@Qualifier类似,都是解决@Autowired时容器中有多个相同类型bean的问题,Primary可以理解为默认优先选择,同时不可以同时设置多个。

@Component("studentInfo")
public class StudentInfo implements UserInfo {
    public String userName() {
        return "student";
    }
}

@Component("teacherInfo")
@Primary
public class TeacherInfo implements UserInfo {
    public String userName {
        return "teacher";
    }
}

@Component
public class UserService {
    @Autowired
    @Qualifier("studentInfo")
    private UserInfo userInfo;

    //todo
}

1.10 @Scope

@Scope注解是springIoc容器中的一个作用域,在 Spring IoC 容器中具有以下几种作用域:基本作用域singleton(单例)(默认作用域)、prototype(多例),Web 作用域(reqeust、session、globalsession),自定义作用域

1.11 @Profile

@profile注解的作用是为了应对多环境开发,比如开发环境使用dev, 生产环境使用prod,就可以使用@Profile注解实现不同的开发环境使用不同的数据源。spring3.2之前 @Profile注解用在类上,spring3.2 之后 @Profile注解用在方法上

1.12 @Import

@Import用于注入指定的类,导入组件id默认是组件的全类名。

@Configuration
public class ConfigA {

    @Bean
    public A a() {
        return new A();
    }
}

@Configuration
@Import(ConfigA.class)
public class ConfigB {

    @Bean
    public B b() {
        return new B();
    }
}

1.13 @ImportResource

@ImportResource注解用于导入Spring的配置文件,让配置文件里面的内容生效;(就是以前写的springmvc.xml、applicationContext.xml)

@Configuration
@ImportResource({"classpath*:applicationContext.xml"})
public class XmlConfiguration {
}

1.14 @PropertySource

@PropertySource注解加载指定的配置文件

@Configuration
@PropertySource("classpath:config.properties")
public class ProperySourceDemo implements InitializingBean {

    @Autowired
    Environment env;

    @Override
    public void afterPropertiesSet() throws Exception {
        setDatabaseConfig();
    }

    private void setDatabaseConfig() {
        DataSourceConfig config = new DataSourceConfig();
        config.setDriver(env.getProperty("jdbc.driver"));
        config.setUrl(env.getProperty("jdbc.url"));
        config.setUsername(env.getProperty("jdbc.username"));
        config.setPassword(env.getProperty("jdbc.password"));
        System.out.println(config.toString());
    }
}

1.15 @PropertySources

@PropertySources顾名思义就是可以指定多个@PropertySource来导入配置文件。

@PropertySources({
  @PropertySource("classpath:config.properties"),
  @PropertySource("classpath:db.properties")
 })
 public class AppConfig {
  //todo...
 }

SpringWeb注解

2.1 @RequestBody

主要用来接收前端传递给后端的json字符串中的数据的。

@RestController
@RequestMapping("/api/v1")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/user")
    public UserInfo createUser(@Valid @RequestBody UserInfo user) {
        return userService.save(user);
    }
}

2.2 @RequestMapping

@RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上。也就是通过它来指定控制器可以处理哪些URL请求。

@Controller
class UserController {
    @RequestMapping(value = "/user/index", method = RequestMethod.GET)
    String index() {
        return "index";
    }
}

2.3 @GetMapping

@GetMapping注释将Http GET请求映射到特定的处理程序方法。 它是一个组合的注释,@RequestMapping(method = RequestMethod.GET)的快捷方式。

    @GetMapping("/users")
    public List<user> getAllUser() {
        //...
    }
    @GetMapping("/users/{id}")
    public ResponseEntity<User> getUserById(@PathVariable(value = "id") Long userId)
            throws ResourceNotFoundException {
        user user = userRepository.findById(userId)
                .orElseThrow(() -> new ResourceNotFoundException("user not found for this id :: " + userId));
        return ResponseEntity.ok().body(user);
    }

2.4 @PathVariable

@PathVariable是spring3.0的一个新功能:接收请求路径中占位符的值

2.5 @PostMapping

@PostMapping注释将HTTP POST请求映射到特定的处理程序方法。 它是一个组合的注释,@RequestMapping(method = RequestMethod.POST)的快捷方式。

@PostMapping("/user/add")
public User addUser(@Valid @RequestBody User user) {
 return userRepository.save(user);
}

其它扩展: @PutMapping、@DeleteMapping、@PatchMapping(这三种相对用的比较少,一笔带过。)

2.6 @ControllerAdvice

增强型控制器,对于控制器的全局配置放在同一个位置。可以用于定义@ExceptionHandler、@InitBinder、@ModelAttribute,可处理全局异常处理、全局数据绑定和全局数据预处理。

@ControllerAdvice(basePackages = {"com.toutou.controller"} )
public class GlobalControllerAdvice {
    @InitBinder
    public void dataBinding(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, "dob", new CustomDateEditor(dateFormat, true));
    }
    @ModelAttribute
    public void globalAttributes(Model model) {
        model.addAttribute("msg", "Hello World!");
    }
    @ExceptionHandler(FileNotFoundException.class)
    public ModelAndView myError(Exception exception) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("exception", exception);
        mav.setViewName("error");
        return mav;
    }
}

2.7 @ExceptionHandler

@ExceptionHandler统一处理某一类异常,从而能够减少代码重复率和复杂度。

2.8 @InitBinder

@InitBinder只在@Controller中注解方法来为这个控制器注册一个绑定器初始化方法,方法只对本控制器有效。

2.9 @ModelAttribute

@ModelAttribute注解用于将方法的参数或方法的返回值绑定到指定的模型属性上,并返回给Web视图。

2.10 @ResponseBody

@ResponseBody将controller里方法返回的对象通过适当的转换器转换为Json写入到response对象的body区.

2.11 @Controller

@Controller用于标记在一个类上,使用它标记的类就是一个SpringMvc Controller对象,分发处理器会扫描使用该注解的类的方法,并检测该方法是否使用了@RequestMapping注解。

2.12 @RestController

@RestController在Spring中的作用等同于@Controller + @ResponseBody。

2.13 @RequestParam

@RequestParam将请求参数绑定到你控制器的方法参数上(是springmvc中接收普通参数的注解)

2.14 @CrossOrigin

@CrossOrigin支持跨域,可用于Controller上,也可用于方法上。

@CrossOrigin(origins = "http://toutou.com", maxAge = 3600)
@RequestMapping("/index")
String index() {
    return "Hello World!";
}

SpringBoot注解

3.1 @SpringBootApplication

@SpringBootApplication是Sprnig Boot项目的核心注解,目的是开启自动配置。由于@Configuration,@EnableAutoConfiguration和@ComponentScan三个注解一般都是一起使用,于是spring boot提供了一个统一的注解@SpringBootApplication。即:@SpringBootApplication=@Configuration + @EnableAutoConfiguration + @ComponentScan。

@SpringBootApplication // 等于:@Configuration @EnableAutoConfiguration @ComponentScan
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

如上代码,是不是简洁了不少。

3.2 @EnableAutoConfiguration

可以根据classpath中的jar依赖,自动注册bean,一般用于类或接口上,它尝试根据您添加的jar依赖项自动配置Spring应用程序。自动载入应用程序所需的所有Bean——这依赖于Spring Boot在类路径中的查找。

@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

3.3 @ConditionalOnClass、@ConditionalOnMissingClass

3.4 @ConditionalOnBean、@ConditionalOnMissingBean

@ConditionalOnBean         //    当给定的在bean存在时,则实例化当前Bean
@ConditionalOnMissingBean  //    当给定的在bean不存在时,则实例化当前Bean
@ConditionalOnClass        //    当给定的类名在类路径上存在,则实例化当前Bean
@ConditionalOnMissingClass //    当给定的类名在类路径上不存在,则实例化当前Bean

3.5 @ConditionalOnProperty

@ConditionalOnProperty可以通过配置文件中的属性值来判定configuration是否被注入。

3.6 @ConditionalOnResource

@ConditionalOnResource是注解在Configuration bean上,在其加载之前对指定资源进行校验,是否存在,如果不存在,抛出异常;该注解支持传入多个变量,

3.7 @ConditionalOnWebApplication、@ConditionalOnNotWebApplication

@ConditionalOnWebApplication主要的用处是: 当Spring为web服务时,才使注解的类生效;通常是配置类;@ConditionalOnNotWebApplication不是web应用。

3.8 @Conditional

@Conditional的作用是按照一定的条件进行判断,满足条件给容器注册bean。

回到顶部

SpringScheduling注解

4.1 @Scheduled

@Scheduled可以作为一个触发源添加到一个方法中。

@Scheduled(fixedDelay=1000)
public void doSomething() {
    //...
}

4.2 @EnableScheduling

@EnableScheduling 在配置类上使用,开启计划任务的支持(类上)。

@Configuration
@EnableScheduling //通过@EnableScheduling注解开启对计划任务的支持
public class TaskScheduleConfig {
    //...
}

4.3 @Async

@Async标注的方法,称之为异步方法;这些方法将在执行的时候,将会在独立的线程中被执行,调用者无需等待它的完成,即可继续其他的操作。有时候我们会调用一些特殊的任务,任务会比较耗时,重要的是,我们不管他返回的后果。这时候我们就需要用这类的异步任务啦。

4.4 @EnableAsync

@EnableAsync注解启用了Spring异步方法执行功能

@Schedules

@Schedules作用跟@Scheduled一样,@Schedules内部包含多个@Scheduled注解,可以表示一个方法可以存在多个调度设置。

注解集合

@ComponentScan:表示将该类自动发现扫描组件。个人理解相当于,如果扫描到有@Component、@Controller、@Service等这些注解的类,并注册为Bean,可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。可以自动收集所有的Spring组件,包括@Configuration类。我们经常使用@ComponentScan注解搜索beans,并结合@Autowired注解导入。如果没有配置的话,Spring Boot会扫描启动类所在包下以及子包下的使用了@Service,@Repository等注解的类。

@Repository:使用@Repository注解可以确保DAO或者repositories提供异常转译,这个注解修饰的DAO或者repositories类会被ComponetScan发现并配置,同时也不需要为它们提供XML配置项。

@Inject:等价于默认的@Autowired,只是没有required属性;

@Component:泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

@JsonBackReference:解决嵌套外链问题。

@JsonIgnore:作用是json序列化时将Java bean中的一些属性忽略掉,序列化和反序列化都受影响。

@ConfigurationProperties:Spring Boot可使用注解的方式将自定义的properties文件映射到实体bean中,比如config.properties文件。

@ConditionalOnSingleCandidate:组合@Conditional注解,当指定的class在容器中只有一个Bean,或者同时有多个但为首选时才开启配置。

@ConditionalOnCloudPlatform:组合 @Conditional 注解,当指定的云平台激活时才开启配置。

@ConditionalOnJndi:组合 @Conditional 注解,当指定的 JNDI 存在时才开启配置。

@ConditionalOnJava:组合@Conditional 注解,当运行的 Java JVM 在指定的版本范围时才开启配置。

@ConditionalOnExpression:组合 @Conditional 注解,当 SpEL 表达式为 true 时才开启配置。

@WiselyConfiguration: 组合注解可以替代@Configuration和@ComponentScan

@Transcational: 事务处理

@Target (ElementType.TYPE):元注解,用来指定注解修饰类的那个成员 -->指定拦截规则

@Cacheable: 数据缓存

@ActiveProfiles: 用来声明活动的 profile

@RunWith: 运行器

作者:头头哥

原文链接:https://www.cnblogs.com/toutou/p/spring_boot_annotations.html

如果觉得本文对你有帮助,可以转发关注支持一下

相关推荐

SpringBoot如何实现优雅的参数校验
SpringBoot如何实现优雅的参数校验

平常业务中肯定少不了校验,如果我们把大量的校验代码夹杂到业务中,肯定是不优雅的,对于一些简单的校验,我们可以使用java为我们提供的api进行处理,同时对于一些...

2025-05-11 19:46 ztj100

Java中的空指针怎么处理?

#暑期创作大赛#Java程序员工作中遇到最多的错误就是空指针异常,无论你多么细心,一不留神就从代码的某个地方冒出NullPointerException,令人头疼。...

一坨一坨 if/else 参数校验,被 SpringBoot 参数校验组件整干净了

来源:https://mp.weixin.qq.com/s/ZVOiT-_C3f-g7aj3760Q-g...

用了这两款插件,同事再也不说我代码写的烂了

同事:你的代码写的不行啊,不够规范啊。我:我写的代码怎么可能不规范,不要胡说。于是同事打开我的IDEA,安装了一个插件,然后执行了一下,规范不规范,看报告吧。这可怎么是好,这玩意竟然给我挑出来这么...

SpringBoot中6种拦截器使用场景

SpringBoot中6种拦截器使用场景,下面是思维导图详细总结一、拦截器基础...

用注解进行参数校验,spring validation介绍、使用、实现原理分析

springvalidation是什么在平时的需求开发中,经常会有参数校验的需求,比如一个接收用户注册请求的接口,要校验用户传入的用户名不能为空、用户名长度不超过20个字符、传入的手机号是合法的手机...

快速上手:SpringBoot自定义请求参数校验

作者:UncleChen来源:http://unclechen.github.io/最近在工作中遇到写一些API,这些API的请求参数非常多,嵌套也非常复杂,如果参数的校验代码全部都手动去实现,写起来...

分布式微服务架构组件

1、服务发现-Nacos服务发现、配置管理、服务治理及管理,同类产品还有ZooKeeper、Eureka、Consulhttps://nacos.io/zh-cn/docs/what-is-nacos...

优雅的参数校验,告别冗余if-else

一、参数校验简介...

Spring Boot断言深度指南:用断言机制为代码构筑健壮防线

在SpringBoot开发中,断言(Assert)如同代码的"体检医生",能在上线前精准捕捉业务逻辑漏洞。本文将结合企业级实践,解析如何通过断言机制实现代码自检、异常预警与性能优化三...

如何在项目中优雅的校验参数

本文看点前言验证数据是贯穿所有应用程序层(从表示层到持久层)的常见任务。通常在每一层实现相同的验证逻辑,这既费时又容易出错。为了避免重复这些验证,开发人员经常将验证逻辑直接捆绑到域模型中,将域类与验证...

SpingBoot项目使用@Validated和@Valid参数校验

一、什么是参数校验?我们在后端开发中,经常遇到的一个问题就是入参校验。简单来说就是对一个方法入参的参数进行校验,看是否符合我们的要求。比如入参要求是一个金额,你前端没做限制,用户随便过来一个负数,或者...

28个验证注解,通过业务案例让你精通Java数据校验(收藏篇)

在现代软件开发中,数据验证是确保应用程序健壮性和可靠性的关键环节。JavaBeanValidation(JSR380)作为一个功能强大的规范,为我们提供了一套全面的注解工具集,这些注解能够帮...

Springboot @NotBlank参数校验失效汇总

有时候明明一个微服务里的@Validated和@NotBlank用的好好的,但就是另一个里不能用,这时候问题是最不好排查的,下面列举了各种失效情况的汇总,供各位参考:1、版本问题springbo...

这可能是最全面的Spring面试八股文了

Spring是什么?Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。...

取消回复欢迎 发表评论: