码迷,mamicode.com
首页 > 编程语言 > 详细

spring常用注解总结

时间:2018-11-13 13:04:26      阅读:225      评论:0      收藏:0      [点我收藏+]

标签:required   发送   默认   return   request请求   scan   request   正则表达   扫描   

spring注解使用
@Controller
@Service(“itemService”)(如果没给(“itemService”),则对象名(被扫描的类的对象)默认是类名首字母小写)
@Component
@Repository
以上四个注解作用相同(都是spring框架扫描类,对类进行管理使用)
使用方法:
1.使用<context:component-scanbase-package="XXX"/>扫描被注解的类(xxx就是要扫描的包)
2. 在类上写注解:
@Controller
public class TestController {
}

@Value
为了简化从properties里取配置,可以使用@Value, 可以properties文件中的配置值。
在spring的xml配置文件中引入properties文件。
<context:property-placeholder location="classpath:test.properties" />
在程序里使用@Value:
@Value("${wx_appid}")
public String appid;
即使给变量赋了初值也会以配置文件的值为准。
test.properties中:
key=xiaoming
spring配置文件中:
<context:property-placeholder location="classpath:properties/test.properties" />
controller层:
@Component
public class ValueDemo {
@Value("${key}")
private String value;//value被赋值为xiaoming
public String getValue() {
return value;
}
}


@Resource(先按照名称注入,如果找不到合适的对象则转为按类型注入)
@Autowired(按照类型注入)

Autowired默认按类型注入,如果发现找到多个bean,会报错。(按接口类型查找)
1.可以手动指定按名称方式注入,使用@Qualifier。
//通过此注解完成从spring配置文件中 查找满足Fruit的bean,然后按//@Qualifier指定pean
@Autowired
@Qualifier("pean")
public Fruit fruit;

2.如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false)
public Fruit fruit;

@Resource
默认按 byName自动注入,如果找不到再按byType找bean,如果还是找不到则抛异常,无论按byName还是byType如果找到多个,则抛异常。
可以手动指定bean,它有2个属性分别是name和type,使用name属性,则使用byName的自动注入,而使用type属性时则使用byType自动注入。
@Resource(name=”bean名字”)

@Resource(type=”bean的class”)
这个注解是属于J2EE的,减少了与spring的耦合。
例如:
Service层有两个类都实现了接口GoodService:

@Service(“itemService”)//(如果没给(“itemService”),则对象名(被扫描的类的对象)默认是类名首字母小写)
public class GoodServiceImpl implements GoodService{
}

@Service(“itemService1111”)//(如果没给(“itemService1111”),则对象名(被扫描的类的对象)默认是类名首字母小写)
public class ItemServiceImpl implements GoodService{
}

那么在Controller层使用名称注入时只能够将对象名称定义为itemService:
@Resource
private GoodService itemService;//注入的是 GoodServiceImpl 的对象

@Resource
private GoodService itemService1111;//注入的是 ItemServiceImpl 的对象

 


@RequestBody(把前台页面传输的复杂的数据类型映射到Controller的形参上(这里的形参一般都是比较复杂的pojo))
@RequestBody(required=true):有个默认属性required,默认是true,当body里没内容时抛异常。
例如:
页面发送数据:
{‘goods‘:{goodsId:‘111‘,goodName:‘aaa‘,[{item:{}}]}}
在Controller层形参可以为一个Goods的对象,对象包含goodsId,goodName,List<Item> 参照

@RequestParam
作用是提取和解析请求中的参数。@RequestParam支持类型转换,类型转换目前支持所有的基本Java类型
public String getUser(@RequestParam(value="number", required=false) String number){
}

将请求中参数为number映射到方法的number上。required=false表示该参数不是必需的,请求上可带可不带。

@PathVariable
@PathVariable:处理requet uri部分,当使用@RequestMapping URI template 样式映射时, 即someUrl/{paramId}, 这时的paramId可通过 @Pathvariable注解绑定它传过来的值到方法的参数上
例如:
@RequestMapping("/getUser/{id}")
public String getUser(@PathVariable(value="id") String id){
}
localhost:8080/someUrl/12345

@Scope
配置bean的作用域。
@Controller
@RequestMapping("/test")
@Scope("prototype")
public class TestController {
}
默认是单例模式,即@Scope("singleton"),
singleton:单例,即容器里只有一个实例对象。
prototype:多对象,每一次请求都会产生一个新的bean实例,Spring不无法对一个prototype bean的整个生命周期负责,容器在初始化、配置、装饰或者是装配完一个prototype实例后,将它交给客户端,由程序员负责销毁该对象,不管何种作用域,容器都会调用所有对象的初始化生命周期回调方法,而对prototype而言,任何配置好的析构生命周期回调方法都将不会被调用
request:对每一次HTTP请求都会产生一个新的bean,同时该bean仅在当前HTTP request内有效

@RestController
@RestController = @Controller + @ResponseBody。
是2个注解的合并效果,即指定了该controller是组件,又指定方法返回的是String或json类型数据,不会解决成jsp页面,注定不够灵活,如果一个Controller即有SpringMVC返回视图的方法,又有返回json数据的方法即使用@RestController太死板。
灵活的作法是:定义controller的时候,直接使用@Controller,如果需要返回json可以直接在方法中添加@ResponseBody

@RequestMapping
处理映射请求的注解。用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径。有6个属性。
1、 value, method:
value:指定请求的实际地址,指定的地址可以是URI Template 模式;
method:指定请求的method类型, GET、POST、PUT、DELETE等;
@RequestMapping(value = "/testValid", method = RequestMethod.POST)
@ResponseBody
public Object testValid(@RequestBody @Valid Test test,BindingResult result, HttpServletRequest request, HttpServletResponse response) {
XXX
}
value的uri值为以下三类:
A) 可以指定为普通的具体值;如@RequestMapping(value ="/testValid")
B) 可以指定为含有某变量的一类值;如@RequestMapping(value="/{day}")
C) 可以指定为含正则表达式的一类值;如@RequestMapping(value="/{textualPart:[a-z-]+}.{numericPart:[\\d]+}") 可以匹配../chenyuan122912请求。
consumes,produces:
consumes: 指定处理请求的提交内容类型(Content-Type),例如@RequestMapping(value = "/test", consumes="application/json")处理application/json内容类型
produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回;
params、headers:
params: 指定request中必须包含某些参数值是,才让该方法处理。
例如:
@RequestMapping(value = "/test", method = RequestMethod.GET, params="name=chenyuan")
public void findOrd(String name) {
// implementation omitted
}
仅处理请求中包含了名为“name”,值为“chenyuan”的请求.
headers: 指定request中必须包含某些指定的header值,才能让该方法处理请求。
@RequestMapping(value = "/test", method = RequestMethod.GET, headers="Referer=www.baidu.com")
public void findOrd(String name) {
// implementation omitted
}
仅处理request的header中包含了指定“Refer”请求头和对应值为“www.baidu.com”的请求

@GetMapping和@PostMapping
@GetMapping(value = "page")等价于@RequestMapping(value = "page", method = RequestMethod.GET)
@PostMapping(value = "page")等价于@RequestMapping(value = "page", method = RequestMethod.POST)

spring注解包:
org.springframework.web.bind.annotation下

spring常用注解总结

标签:required   发送   默认   return   request请求   scan   request   正则表达   扫描   

原文地址:https://www.cnblogs.com/megadata/p/9951471.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!