@Controller
• @Service
• @Autowired
• @RequestMapping
• @RequestParam
• @ModelAttribute
• @Cacheable
• @CacheFlush
• @Resource
• @PostConstruct
• @PreDestroy
• @Repository
• @Component (不推荐使用)
• @Scope
• @SessionAttributes
• @InitBinder
• @Required
• @Qualifier
......
springmvc常用注解图
0.@Controller,@Service,@Repository,@Component,@Resource,@Autowired,@Qualifier
0.1.@Controller,@Service,@Repository,@Component,标记在类上,意思将该类放入bean工厂,由spring管理。
0.2.@Component通用注解,spring为了体现分层思想,分别用@Repository、@Service、@Controller表示dao、service、controller层的bean。@Scope相当于配置中<bean>的scope属性。
0.3.@Resource,@Autowired,@Qualifier,用来注入bean,相当于配置里的set注入和构造器注入。@Resource默认按名称自动查找bean,没有就按类型。@Autowired按类型自动注入,查找到的bean类型有且仅有一个,否则抛异常。
0.4.@Qualifier不能单独存在,通常和@Autowired组合使用,当@Autowired查找bean类型结果有多个bean时,可以用@Qualifier将自动注入类型由类型查找改为由名称查找。
0.5.@Resource是j2ee提供的注解,@Autowired是spring提供的注解,为了减少项目对spring的依赖,强烈建议使用@Resource
1.@RequestMapping
可以标记在类和方法上。
*类
@Controller
@RequestMapping("/login")
public class LoginController {
}
意思是可以通过"项目路径/login"访问这个controller,如http://localhost:8080/ssmdemo2/login
*方法
@RequestMapping("/name")
意思和类一样,访问路径是"项目路径/controller定义的逻辑路径/name"
@RequestMapping(value="/name",method=RequestMethod.POST)
声明只接受post请求
@RequestMapping(
(value="/name"
,params="method=test",method=RequestMethod.POST)
访问该方法还要加参数"
method=test",
访问路径是"项目路径/controller定义的逻辑路径/name?method=test"
@RequestMapping(
(value="/name"
,params={"method=test","id=1"},method=RequestMethod.POST)
访问该方法还需要两个参数,
访问路径是"项目路径/controller定义的逻辑路径/name?
method=test&id=1
"
2.@RequestParam
参数绑定用,只能用在参数上不能用在方法上。
@RequestParam(value="abc")
public String test5(@RequestParam(value="abc",required=false) String english) {
将url里“abc”参数的值封装入 "english",required代表这个参数是否必要,默认true,true代表必要。
value:参数名字,即入参的请求参数名字,如username表示请求的参数区中的名字为username的参数的值将传入;
required:是否必须,默认是true,表示请求中一定要有相应的参数,否则将报404错误码;
defaultValue:默认值,表示如果请求中没有同名参数时的默认值
3.可选返回值类型
3.1.void
此时逻辑视图名由请求处理方法对应的 URL 确定,如以下的方法:
@RequestMapping("/hello.do")
public void hello() {}
对应的逻辑视图名为 “定义controller逻辑路径/hello” ,如"login/hello"
3.2.String
此时逻辑视图名为返回的字符,如以下的方法:
@RequestMapping("/hello.do")
public String hello() {
return "login/hello"
}
对应的逻辑视图名为 "login/hello"
3.3.ModelMap
返回逻辑视图名和void一样,如以下的方法:
@RequestMapping("/hello.do")
public ModelMap hello() {
return new ModelMap();
}
对应的逻辑视图名为 “定义controller逻辑路径/hello” ,如"login/hello"
3.4.ModelAndView
当然还有传统的ModelAndView啦。
4.@ModelAttribute
• 作用域:request
• 例如
@RequestMapping("/base/userManageCooper/init.do")
public String handleInit(@ModelAttribute("queryBean") ManagedUser sUser,Model model,){
• 或者
@ModelAttribute("coopMap")// 将coopMap 返回到页 面
public Map<Long,CooperatorInfo> coopMapItems(){}
• 说明
@ModelAttribute 声明在属性上,表示该属性的value 来源于model 里"queryBean" ,并被保存到model 里@ModelAttribute 声明在方法上,表示该方法的返回值被保存到model 里 。
5.@PostConstruct与@PreDestroy
在方法上加上注解@PostConstruct ,这个方法就会在Bean 初始化之后被Spring 容器执 行
(注:Bean 初始化包括,实例化Bean ,并装配Bean 的属性(依赖注入))。
在方法上加上注解@PreDestroy ,这个方法就会在Bean 被销毁前被Spring 容器执行。
6.@SessionAttributes
• 说明
Spring 允许我们有选择地指定 ModelMap 中的哪些属性需要转存到 session 中,
以便下一个请求属对应的 ModelMap 的属性列表中还能访问到这些属性。
这一功能是通过类定义处标注 @SessionAttributes 注解来实现的。
@SessionAttributes 只能声明在类上,而不能声明在方法上。
• 例如
@SessionAttributes("currUser") // 将ModelMap 中属性名为currUser 的属性
@SessionAttributes({"attr1","attr2"})
@SessionAttributes(types = User.class)
@SessionAttributes(types = {User.class,Dept.class})
@SessionAttributes(types = {User.class,Dept.class},value={"attr1","attr2"})
7.@Required
• 例如
@required
public setName(String name){}
• 说明
@ required 负责检查一个bean在初始化时其声明的 set方法是否被执行, 当某个被标注了 @Required 的 Setter 方法没有被调用,则 Spring 在解析的时候会抛出异常,以提醒开发者对相应属性进行设置。 @Required 注解只能标注在 Setter 方法之上。因为依赖注入的本质是检查 Setter 方法是否被调用了,而不是真的去检查属性是否赋值了以及赋了什么样的值。如果将该注解标注在非 setXxxx() 类型的方法则被忽略。
8.@InitBinder
• 说明
如果希望某个属性编辑器仅作用于特定的 Controller ,
可以在 Controller 中定义一个标注 @InitBinder 注解的方法,
可以在该方法中向 Controller 了注册若干个属性编辑器
• 例如
@InitBinder
public void initBinder(WebDataBinder binder) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
dateFormat.setLenient(false);
binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
}
9.@Cacheable 和@CacheFlush
• @Cacheable :声明一个方法的返回值应该被缓 存
例如:@Cacheable(modelId = "testCaching")
• @CacheFlush :声明一个方法是清空缓存的触发器
例如:@CacheFlush(modelId = "testCaching")
• 说明
要配合缓存处理器使用,参考: http://hanqunfeng.iteye.com/blog/603719
10.@ResponseBody
作用: 标记在方法上,该注解用于将Controller的方法返回的对象,通过适当的HttpMessageConverter转换为指定格式后,写入到Response对象的body数据区。通常用来发送json。
使用时机:返回的数据不是html标签的页面,而是其他某种格式的数据时(如json、xml等)使用;
11.@PathVariable
用法和@RequestParam类似,唯一的区别是它请求路径上有个id的变量值,可以通过@PathVariable来获取,如: @RequestMapping(value = "/page/{id}", method = RequestMethod.GET)
@RequestParam用来获得静态的URL请求入参 spring注解时action里用到。
一个可以设置?之前的路径,一个只能获取?之后的参数。
12.@RequestBody
标记在参数上,该注解常用来处理Content-Type: 不是application/x-www-form-urlencoded编码的内容,例如application/json, application/xml等; 通常用来接收json。
13.@RequestHeader、@CookieValue
@RequestHeader 注解,可以把Request请求header部分的值绑定到方法的参数上。
示例代码:
这是一个Request 的header部分:
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
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@RequestHeader("Accept-Encoding") String encoding,
@RequestHeader("Keep-Alive") long keepAlive) {
}
上面的代码,把request header部分的 Accept-Encoding的值,绑定到参数encoding上了, Keep-Alive header的值绑定到参数keepAlive上。
@CookieValue 可以把Request header中关于cookie的值绑定到方法的参数上。
例如有如下Cookie值:
JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84
@RequestMapping("/displayHeaderInfo.do")
public void displayHeaderInfo(@CookieValue("JSESSIONID") String cookie) {
}
即把JSESSIONID的值绑定到参数cookie上。