Spring Boot 常用注解说明

Spring boot 常用注解说明

@SpringBootApplication

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

@SpringBootApplication看作是 @Configuration@EnableAutoConfiguration@ComponentScan 注解的集合

  • @EnableAutoConfiguration:启用 SpringBoot 的自动配置机制
  • @ComponentScan: 扫描被@Component (@Service,@Controller)注解的 bean,注解默认会扫描该类所在的包下所有的类。
  • @Configuration:允许在 Spring 上下文中注册额外的 bean 或导入其他配置类

@Autowired

自动导入对象到类中,被注入进的类同样要被 Spring 容器管理。比如:Service 类注入到 Controller 类中。我们一般使用 @Autowired 注解让 Spring 容器帮我们自动装配 bean

@RestController
@RequestMapping("/material")
public class MaterialController {

    @Autowired
    private MaterialService materialService;
    @Autowired
    private RedisUtils redisUtil ;

    @RequestMapping(path="/getMaterialByPkgId", method = RequestMethod.POST)
    @ResponseBody
    public String getMaterialByPkgId(@RequestBody JSONObject jsondata){
        JSONObject jsonObject = jsondata;
        MaterialObject material = jsonObject.toJavaObject(MaterialObject.class);
        String jsonString = "";
        if(material.isResultIsExpiredList()) {
            ResponseResult data = materialService.getMaterialListByPkgId(material);
            jsonString= JSONObject.toJSONString(data);
        }else{
            ResponseResult data = materialService.getMaterialByPkgId(material);
            jsonString= JSONObject.toJSONString(data);
        }
        return jsonString;
    }
}

Component

@Repository@Service, @Controller

要想把类标识成可用于 @Autowired 注解自动装配的 bean 的类,可以采用以下注解实现:

  • @Component :通用的注解,可标注任意类为 Spring 组件。如果一个 Bean 不知道属于哪个层,可以使用@Component 注解标注。
  • @Repository : 对应持久层即 Dao 层,主要用于数据库相关操作。
  • @Service :      对应服务层,主要涉及一些复杂的逻辑,需要用到 Dao 层。
  • @Controller : 对应 Spring MVC 控制层,主要用户接受用户请求并调用 Service 层返回数据给前端页面。

@Configuration

一般用来声明配置类,可以使用 @Component注解替代,不过使用Configuration注解声明配置类更加语义化。

@Configuration
public class RedisConfiguration extends CachingConfigurerSupport {
    @Bean
    public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate){
        return redisTemplate.opsForZSet();
    }
}

@RestController

@RestController注解是@Controller和@ResponseBody的合集,表示这是个控制器 bean,并且是将函数的返回值直 接填入 HTTP 响应体中,是 REST 风格的控制器。

单独使用 @Controller 不加 @ResponseBody的话一般使用在要返回一个视图的情况,这种情况属于比较传统的 Spring MVC 的应用,对应于前后端不分离的情况。

@Controller +@ResponseBody 返回 JSON 或 XML 形式数据

@Scope

声明 Spring Bean 的作用域,使用方法:

@Bean
@Scope("singleton")
public Person personSingleton() {
    return new Person();
}

四种常见的 Spring Bean 的作用域:

  • singleton : 唯一 bean 实例,Spring 中的 bean 默认都是单例的。
  • prototype : 每次请求都会创建一个新的 bean 实例。
  • request : 每一次 HTTP 请求都会产生一个新的 bean,该 bean 仅在当前 HTTP request 内有效。
  • session : 每一次 HTTP 请求都会产生一个新的 bean,该 bean 仅在当前 HTTP session 内有效。

 

常见的 HTTP 请求类型

  • @RequestMapping
  • @GetMapping: @GetMapping("users") 等价于@RequestMapping(value="/users",method=RequestMethod.GET)
  • @PostMapping@PostMapping("users") 等价于@RequestMapping(value="/users",method=RequestMethod.POST)
  • @DeleteMapping@DeleteMapping("/users/{userId}")等价于@RequestMapping(value="/users/{userId}",method=RequestMethod.DELETE)
  • @PutMapping: @PutMapping("/users/{userId}") 等价于@RequestMapping(value="/users/{userId}",method=RequestMethod.PUT) 

@PathVariable @RequestParam

@PathVariable用于获取路径参数,@RequestParam用于获取查询参数。

@RequestBody

用于读取 Request 请求(可能是 POST,PUT,DELETE,GET 请求)的 body 部分并且Content-Type 为 application/json 格式的数据,接收到数据之后会自动将数据绑定到 Java 对象上去。系统会使用HttpMessageConverter或者自定义的HttpMessageConverter将请求的 body 中的 json 字符串转换为 java 对象。

需要注意的是:一个请求方法只可以有一个@RequestBody,但是可以有多个@RequestParam@PathVariable

 

读取配置信息

从配置文件中读取这些配置信息

@value()  

  使用 @Value("${property}") 读取比较简单的配置信息:

@ConfigurationProperties

读取配置信息并与 bean 绑定。

@Configuration
@ConfigurationProperties(prefix = "jwt")
public class SecurityProperties {

}

以上记录,参考了以下博文,用于个人学习和知识积累

https://zhuanlan.zhihu.com/p/135987318

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值