SpringBoot常用注解

目录

前言

@SpringBootApplication:

@RestController:

@RequestMapping:

@RequestParam:

@PathVariable:

@RequestBody:

@ResponseBody:

@Autowired:

@Component:

@Configuration:

@EnableAutoConfiguration:

@EnableWebMvc:


前言

        Spring Boot是基于Spring框架的开发工具,使开发人员不再需要定义样板化的配置,能快速搭建Spring项目。通过使用注解能简化和加速应用程序的开发。下面是一些常用的Spring Boot注解:

SpringBoot是什么,可以做什么?,教育,职业教育,好看视频 (baidu.com)icon-default.png?t=N7T8https://haokan.baidu.com/v?pd=wisenatural&vid=6331477699798706616


@SpringBootApplication:

        组合注解,用于标记主要的启动类。包括了@Configuration、@EnableAutoConfiguration和@ComponentScan注解,用于启用自动配置和组件扫描。

实现举例:

@SpringBootApplication

public class DemoApplication {

    public static void main(String[] args) {

        SpringApplication.run(DemoApplication.class, args);

    }

}

@RestController:

        用于标记控制器类,表示该类是一个RESTful风格的控制器,可以处理HTTP请求并返回JSON或XML等格式的响应。

实现举例:

@Retention(RetentionPolicy.RUNTIME)

@Target({ElementType.TYPE})

public @interface RestController {

}

@RequestMapping:

        用于映射HTTP请求URL到控制器的方法上。可以指定URL路径、请求方法、请求参数等信息。

实现举例:

@Retention(RetentionPolicy.RUNTIME)

@Target({ElementType.METHOD})

public @interface RequestMapping {

    String value();

    RequestMethod method() default RequestMethod.GET;

}

@RequestParam:

        用于获取HTTP请求中的请求参数值,并将其绑定到方法的参数上。

实现举例:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.PARAMETER)

public @interface RequestParam {

    String value() default "";

    boolean required() default true;

    String defaultValue() default "\n\t\t\n\t\t\n\uE000\uE001\uE002\n\t\t\t\t\n";

}

@PathVariable:

        用于获取URL路径中的变量值,并将其绑定到方法的参数上。

实现举例:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.PARAMETER)

public @interface PathVariable {

    String value() default "";

}

@RequestBody:

        用于将HTTP请求的请求体内容绑定到方法的参数上,常用于接收JSON格式的请求数据。

实现举例:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.PARAMETER)

public @interface RequestBody {

}

@ResponseBody:

        用于将方法的返回值序列化为HTTP响应的内容,常用于返回JSON格式的响应数据。

实现举例:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

public @interface ResponseBody {

}

@Autowired:

        用于自动装配Spring管理的Bean对象。可以在构造方法、属性、方法参数上使用。

实现举例:

@Retention(RetentionPolicy.RUNTIME)

@Target({ElementType.FIELD, ElementType.METHOD, ElementType.CONSTRUCTOR})

public @interface Autowired {

    boolean required() default true;

}

@Component:

        用于标记一个普通的Spring bean组件,会被自动扫描并注册到Spring容器中

实现举例:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

public @interface Component {

    String value() default "";

}

@Configuration:

        用于标记配置类,相当于传统的XML配置文件。可以在其中定义bean对象、配置数据库连接等。

实现举例:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

public @interface Configuration {

}

@EnableAutoConfiguration:

        用于启用自动配置功能,根据classpath中的依赖和配置来自动配置Spring Boot应用程序。

实现举例:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

public @interface EnableAutoConfiguration {

    Class<?>[] exclude() default {};

}

@EnableWebMvc:

        用于启用Spring MVC框架,开启web相关的配置。

实现举例:

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.TYPE)

@Import(DelegatingWebMvcConfiguration.class)

public @interface EnableWebMvc {

}

        上述注解并不完善仅为本人使用到的,供参考。

                                                        

  • 7
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值