zoukankan      html  css  js  c++  java
  • Spring注解; Spring Bean

    处理器方法和方法参数相关:

    @Controller,类注解:标记一个类是控制器,控制器上也可以加@RequestMapping,表示类内部的handler method方法的url前共有一部分路径。

    @RequestMapping 类注解,方法注解:把一个handler method方法和对应的url匹配,表示这个url的请求使用该方法处理逻辑。该注解支持通配符*,method属性表示支持的方法种类。如:

    @RequestMapping (value= "*/testMethod" , method={RequestMethod. GET , RequestMethod. DELETE })  
    

    @PathVariable 方法参数注解:把url模板中对应的变量传给相同形参名字的函数参数

    @RequestHeader(key)方法参数注解:把请求头中key对应的值传给形参

    @CookieValue(key)方法参数注解:把cookie中key对应的值传给形参

    @RequestParam(key)方法参数注解:get或post方法传参的时把key对应的值传给形参

    @RequestBody

    @Autowired 变量注解,构造函数或set函数注解:从容器中获取对应的bean赋值给变量,传给对应形参,按类型注入

    @Resource 变量注解:按名字注入

    @Require setter方法注解:检查该属性是否在配置文件中注入。 https://www.tutorialspoint.com/spring/spring_required_annotation.htm

    @Scope :表明作用域

    配置需要扫描目录下的注解

    <context:component-scan base-package="test.springmvc"/>
    

    Spring Bean的 作用域:singleton、prototype、request、session、global-session

    singleton表示在spring ioc中只存在一份bean实例

    prototype表示每次getBean都获取一个新的对象

    request表示每次http请求都会创建一个Bean、即bean的生命周期是和request请求一致的。

    session表示一个session会话中创建一个bean,bean的生命周期和session一致。

    global-session

    使用Spring注解定义Bean:@Controller、@Service、@Repository、@Component,一般都是singleton作用域

    使用bean注入:@Resource(按名字) 、 @Autowired(按类型)

  • 相关阅读:
    jQuery.ajax()方法笔记
    Docker安装
    Linux下Nginx+keepalived实现高可用
    Linux安装Nginx
    Redis主从、哨兵、Cluster特性
    Linux搭建redis集群
    Linux搭建redis单机
    HashMap知识总结(jdk1.8)
    如何避免form提交进行页面跳转
    异步上传文件,jquery+ajax,显示进度条
  • 原文地址:https://www.cnblogs.com/afraidToForget/p/10124350.html
Copyright © 2011-2022 走看看