话题很大,感觉能力有限,很难把这些东西一次说透,只能稍作一个整理,供参考了。Annotation , 注解。可以粗浅理解是一种方法,这种方法能够帮助我们简化代码或者资源配置文件,提高工作效率。越来越多的框架提供Annotation拓展,帮助我们更好完成任务。
学习中的日用而不知现象:哪怕是一个java 的出学者,都回接触java annotation的,@Override 估计不陌生,@Override 告诉编辑器覆写父类方法,如果不小心hashCode写成hoshcode这个时候IDE会提示错误的;读代码的人也很清楚这个方法覆盖父类的方法。
package org.origin100.example.annotation;
public class OverrideExample {
private String field;
private String attribute;
@Override
public int hashCode() {
return field.hashCode() + attribute.hashCode();
}
@Override
public String toString() {
return field + " " + attribute;
}
}
Spring中注解一览:
org.springframework.web.bind.annotation
Interface Hierarchy
org.springframework.web.bind.annotation.ValueConstants
Annotation Type Hierarchy
org.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 Hierarchy
java.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 Hierarchy
org.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="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="org.springframework.samples.petclinic.web"/>
// ...
</beans>
编译的时候自动会根据componet-scan 配置扫描相应的package根据@xxx类型生产相应的bean 或者注入方法。建议大家多看官方文档,文档之外任何解释都有点多余。
参考文章列表:
http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-iocannt/index.html
http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html#mvc-annotation-driven