Annotation 和Spring Annotation 知识整理
Annotation 和Spring Annotation 知识整理
话题很大,感觉能力有限,很难把这些东西一次说透,只能稍作一个整理,供参考了。Annotation , 注解。可以粗浅理解是一种方法,这种方法能够帮助我们简化代码或者资源配置文件,提高工作效率。越来越多的框架提供Annotation拓展,帮助我们更好完成任务。
学习中的日用而不知现象:哪怕是一个java 的出学者,都回接触java annotation的,@Override 估计不陌生,@Override 告诉编辑器覆写父类方法,如果不小心hashCode写成hoshcode这个时候IDE会提示错误的;读代码的人也很清楚这个方法覆盖父类的方法。
package org.origin100.example.annotation;public class OverrideExample {private String field;private String attribute;@Overridepublic int hashCode() {return field.hashCode() + attribute.hashCode();}@Overridepublic String toString() {return field + " " + attribute;}
}
Spring中注解一览:
org.springframework.web.bind.annotation
Interface Hierarchyorg.springframework.web.bind.annotation.ValueConstants
Annotation Type Hierarchyorg.springframework.web.bind.annotation.RequestMapping (implements java.lang.annotation.Annotation)
org.springframework.web.bind.annotation.ResponseBody (implements java.lang.annotation.Annotation)
org.springframework.web.bind.annotation.PathVariable (implements java.lang.annotation.Annotation)
org.springframework.web.bind.annotation.SessionAttributes (implements java.lang.annotation.Annotation)
org.springframework.web.bind.annotation.RequestParam (implements java.lang.annotation.Annotation)
org.springframework.web.bind.annotation.RequestBody (implements java.lang.annotation.Annotation)
org.springframework.web.bind.annotation.ResponseStatus (implements java.lang.annotation.Annotation)
org.springframework.web.bind.annotation.ModelAttribute (implements java.lang.annotation.Annotation)
org.springframework.web.bind.annotation.InitBinder (implements java.lang.annotation.Annotation)
org.springframework.web.bind.annotation.ExceptionHandler (implements java.lang.annotation.Annotation)
org.springframework.web.bind.annotation.CookieValue (implements java.lang.annotation.Annotation)
org.springframework.web.bind.annotation.Mapping (implements java.lang.annotation.Annotation)
org.springframework.web.bind.annotation.RequestHeader (implements java.lang.annotation.Annotation)
Enum Hierarchyjava.lang.Object
java.lang.Enum<E> (implements java.lang.Comparable<T>, java.io.Serializable)
org.springframework.web.bind.annotation.RequestMethod
Hierarchy For Package org.springframework.stereotype
Annotation Type Hierarchyorg.springframework.stereotype.Component (implements java.lang.annotation.Annotation)
org.springframework.stereotype.Service (implements java.lang.annotation.Annotation)
org.springframework.stereotype.Repository (implements java.lang.annotation.Annotation)
org.springframework.stereotype.Controller (implements java.lang.annotation.Annotation)
可以看出都实现了java.lang中的Annotation。它们的用法可以参见《 详解Spring 3.0基于Annotation的依赖注入实现》。看看官方文档的代码片
org.springframework.samples.petclinic.web
配置文件:@Controller public class HelloWorldController {@RequestMapping("/helloWorld")public ModelAndView helloWorld() {ModelAndView mav = new ModelAndView();mav.setViewName("helloWorld");mav.addObject("message", "Hello World!");return mav;} }
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="" xmlns:xsi=""xmlns:p="" xmlns:context=""xsi:schemaLocation=" .0.xsd .0.xsd"><context:component-scan base-package="org.springframework.samples.petclinic.web"/>// ...</beans>
编译的时候自动会根据componet-scan 配置扫描相应的package根据@xxx类型生产相应的bean 或者注入方法。建议大家多看官方文档,文档之外任何解释都有点多余。 参考文章列表:
.html
.0.x/reference/mvc.html#mvc-annotation-driven
最新文章
- html5 tooltip,HTML5 教程之CSS 提示工具(Tooltip)
- 关于C语言编程中include的用法
- Linux下重启mysql的正确方法
- spark web ui中的skipped的含义
- 【Yolo】Jetson Orin Nano下部署 YoloV5
- ADFS 概念与基本开发介绍 (1)
- pgpool连接池
- pgpool读写分离,配置设置及调研
- Linux C编程itoa()函数 atoi()函数
- extern 用法小结
- web弹出对话框
- 主引导扇区(MBR),分区表(DPT)及活动分区(DBR)
- Linux直接在通过终端打开图片文件
- python爬虫(网页解析)
- 数据挖掘——机器学习
- 基于直方图的图像增强算法(HE、CLAHE)之(二)
- C语言简单实现通讯录