Annotation 和Spring Annotation 知识整理


        话题很大,感觉能力有限,很难把这些东西一次说透,只能稍作一个整理,供参考了。Annotation , 注解。可以粗浅理解是一种方法,这种方法能够帮助我们简化代码或者资源配置文件,提高工作效率。越来越多的框架提供Annotation拓展,帮助我们更好完成任务。

        学习中的日用而不知现象:哪怕是一个java 的出学者,都回接触java annotation的,@Override 估计不陌生,@Override 告诉编辑器覆写父类方法,如果不小心hashCode写成hoshcode这个时候IDE会提示错误的;读代码的人也很清楚这个方法覆盖父类的方法。

[java]   view plain copy
  1. package org.origin100.example.annotation;  
  2.   
  3. public class OverrideExample {  
  4.     private String field;  
  5.     private String attribute;  
  6.   
  7.     @Override  
  8.     public int hashCode() {  
  9.         return field.hashCode() + attribute.hashCode();  
  10.     }  
  11.   
  12.     @Override  
  13.     public String toString() {  
  14.         return field + " " + attribute;  
  15.     }  
  16. }  

Spring中注解一览:

org.springframework.web.bind.annotation

[java]   view plain copy
  1. Interface Hierarchy  
  2.   
  3. org.springframework.web.bind.annotation.ValueConstants  
  4. Annotation Type Hierarchy  
  5.   
  6. org.springframework.web.bind.annotation.RequestMapping (implements java.lang.annotation.Annotation)  
  7. org.springframework.web.bind.annotation.ResponseBody (implements java.lang.annotation.Annotation)  
  8. org.springframework.web.bind.annotation.PathVariable (implements java.lang.annotation.Annotation)  
  9. org.springframework.web.bind.annotation.SessionAttributes (implements java.lang.annotation.Annotation)  
  10. org.springframework.web.bind.annotation.RequestParam (implements java.lang.annotation.Annotation)  
  11. org.springframework.web.bind.annotation.RequestBody (implements java.lang.annotation.Annotation)  
  12. org.springframework.web.bind.annotation.ResponseStatus (implements java.lang.annotation.Annotation)  
  13. org.springframework.web.bind.annotation.ModelAttribute (implements java.lang.annotation.Annotation)  
  14. org.springframework.web.bind.annotation.InitBinder (implements java.lang.annotation.Annotation)  
  15. org.springframework.web.bind.annotation.ExceptionHandler (implements java.lang.annotation.Annotation)  
  16. org.springframework.web.bind.annotation.CookieValue (implements java.lang.annotation.Annotation)  
  17. org.springframework.web.bind.annotation.Mapping (implements java.lang.annotation.Annotation)  
  18. org.springframework.web.bind.annotation.RequestHeader (implements java.lang.annotation.Annotation)  
  19. Enum Hierarchy  
  20.   
  21. java.lang.Object  
  22. java.lang.Enum<E> (implements java.lang.Comparable<T>, java.io.Serializable)  
  23. org.springframework.web.bind.annotation.RequestMethod  

Hierarchy For Package org.springframework.stereotype

[java]   view plain copy
  1. Annotation Type Hierarchy  
  2.   
  3. org.springframework.stereotype.Component (implements java.lang.annotation.Annotation)  
  4. org.springframework.stereotype.Service (implements java.lang.annotation.Annotation)  
  5. org.springframework.stereotype.Repository (implements java.lang.annotation.Annotation)  
  6. org.springframework.stereotype.Controller (implements java.lang.annotation.Annotation)  
可以看出都实现了java.lang中的Annotation。它们的用法可以参见《 详解Spring 3.0基于Annotation的依赖注入实现》。看看官方文档的代码片
[java]   view plain copy
  1. <pre name="code" class="html" style="background-color: rgb(255, 255, 255); text-align: left; ">org.springframework.samples.petclinic.web</pre>  
  2. <pre></pre>  
  3. <pre name="code" class="java">@Controller  
  4. public class HelloWorldController {  
  5.   
  6.     @RequestMapping("/helloWorld")  
  7.     public ModelAndView helloWorld() {  
  8.         ModelAndView mav = new ModelAndView();  
  9.         mav.setViewName("helloWorld");  
  10.         mav.addObject("message""Hello World!");  
  11.         return mav;  
  12.     }  
  13. }</pre>配置文件:  
[html]   view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"   
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:p="http://www.springframework.org/schema/p"   
  5.     xmlns:context="http://www.springframework.org/schema/context"  
  6.     xsi:schemaLocation="  
  7.         http://www.springframework.org/schema/beans   
  8.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
  9.         http://www.springframework.org/schema/context   
  10.         http://www.springframework.org/schema/context/spring-context-3.0.xsd">  
  11.   
  12.     <context:component-scan base-package="org.springframework.samples.petclinic.web"/>  
  13.   
  14.     // ...  
  15.   
  16. </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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值