1、@PathVariable
遵循refule格式:就是uri中就是参数,someUrl、{paramId},可以通过@pathvariable注解绑定它传过来的值到方法的参数上去。
2、@RequestHeader
把Request请求中的Hearder部分的值,绑定到方法的参数上去。
例如:Host localhost:8080
Accept text/html,application/xhtml+xml,application/xml;q=0.9
Accept-Language fr,en-gb;q=0.7,en;q=0.3
Accept-Encoding gzip,deflate
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive 300
@RequestHeader(“Host”) String host,@RequestHeader(“Keep-Alive”) String Keep-Alive
3、@CookieValue
把Request请求中的cookie的值绑定到方法的参数中去。
@cookieValue(‘JESSIONID’) String cookieValue
4、@requestParam(普通参数用)
底层用request.getParameter的方式获取参数。所以可处理get方式中的queryString的值,也可以处理post方式中body data的值
用来处理的Content-Type为application/x-www-form-urlencoded编码的内容。(说白了就是处理url?后面附带的参数,或post中的普通参数)
该注解有两个属性: value、required; value用来指定要传入值的id名称,required用来指示参数是否必须绑定;
@RequestParam("petId") int petId
5、requestBody(json数据用)
该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容,例如application/json, application/xml等;
但是也可以用来处理 application/x-www-form-urlencoded的内容,处理完的结果放在一个MultiValueMap<String, String>里
@RequestBody String body
6、@SessionAttributes:
该注解用来绑定HttpSession中的attribute对象的值,便于在方法中的参数里使用。
该注解有value、types两个属性,可以通过名字和类型指定要使用的attribute 对象;
@SessionAttributes("pet")
7、@ModelAttribute
该注解有两个用法,一个是用于方法上,一个是用于参数上;
用于方法上时: 通常用来在处理@RequestMapping之前,为请求绑定需要从后台查询的model;
用于参数上时: 用来通过名称对应,把相应名称的值绑定到注解的参数bean上;要绑定的值来源于:
A) @SessionAttributes 启用的attribute 对象上;
B) @ModelAttribute 用于方法上时指定的model对象;
C) 上述两种情况都没有时,new一个需要绑定的bean对象,然后把request中按名称对应的方式把值绑定到bean中。
@ModelAttribute Pet pet
首先查询 @SessionAttributes有无绑定的Pet对象,若没有则查询@ModelAttribute方法层面上是否绑定了Pet对象,若没有则将URI template中的值按对应的名称绑定到Pet对象的各属性上
各个层级
1、controller控制层(注入服务)
用于标注控制层 ,相当于struts中的action层
2、@service服务(注入dao)
用于标注服务层,主要用来进行业务的逻辑处理
3、@repository(实现dao访问)
用于标注数据访问层
4、@component(把普通pojo实例化到spring容器中,相当于配置文件中的<bean id="" class="">)
泛指各种组件,当我们的类不属于上面任何一种时,用@component标注
例如:
<context:component-scan base-package=”com.*”>
上面的这个例子是引入Component组件的例子,其中base-package表示为需要扫描的所有子包。
共同点:被@controller 、@service、@repository 、@component 注解的类,都会把这些类纳入进spring容器中进行管理
注入值的注解
1、@value(为某个变量注入配置文件中的值)
第一种写法:
@value("${htoa.abc}")
public String abc;
此方法不需要加载指定的对象,只需要一个关键对象来完成PreferencesPlaceholderConfigurer
第二种写法:@Value("#{configProperties['t1.msgname']}")
此方法加载配置文件中的configProperties对象
Spring 通过注解获取*.porperties文件的内容,除了xml配置外,还可以通过@value方式来获取。
2、@ConfigurationProperties
有三种好处:
第一种:支持”松绑定“,就是例如context-path和contextpath,就可以识别绑定数据。
第二种:支持元数据,可以在resource下建立META-INF文件夹,然后建立文件additional-spring-configuration-metadata.json,里面的数据格式必须满足spring的元数据格式。
第三种(常用):同时注入多个值到java对象中。
例如:myjava对象有a,b,c成员变量需要配置,那么我们只要在yml或properties中定义了a=1,b=2,c=3,然后通过@ConfigurationProperties就能把值注入进myjava对象中
配置文件中有spring.my.a=1,spring.my.b=2,spring.my.c=3
@ConfigurationProperties(prefix="spring.my")
public MyJava myJava;
@value和@ConfigurationProperties比较
Feature @ConfigurationProperties @Value
Relaxed binding(松绑定) Yes No
Meta-data support(元数据) Yes No
SpEL evaluation(表达式语言(SpEL)) No Yes
Spring表达式语言(SpEL),可以像jsp中的el表达式一样,在运行期动态执行,从而得到需要的值
@Controller和@RestController的区别
@Controller默认返回的是view,只有在方法中添加@ResponseBody,才返回具体对象
@RestController的作用就相当于@Controller+@ResponseBody的结合体
否则会报错:Error resolving template "manager/historyCase/list", template might not exist or might not be accessible by any of the configured Template Resolvers(找不到view文件)