Annotation 和Spring Annotation 知识整理

本文介绍了Spring框架中的常用注解,包括@Controller、@RequestMapping等,并通过示例展示了如何使用这些注解进行依赖注入和请求映射。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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

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

[java] view plain copy
  1. packageorg.origin100.example.annotation;
  2. publicclassOverrideExample{
  3. privateStringfield;
  4. privateStringattribute;
  5. @Override
  6. publicinthashCode(){
  7. returnfield.hashCode()+attribute.hashCode();
  8. }
  9. @Override
  10. publicStringtoString(){
  11. returnfield+""+attribute;
  12. }
  13. }

Spring中注解一览:

org.springframework.web.bind.annotation

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

Hierarchy For Package org.springframework.stereotype

[java] view plain copy
  1. AnnotationTypeHierarchy
  2. org.springframework.stereotype.Component(implementsjava.lang.annotation.Annotation)
  3. org.springframework.stereotype.Service(implementsjava.lang.annotation.Annotation)
  4. org.springframework.stereotype.Repository(implementsjava.lang.annotation.Annotation)
  5. org.springframework.stereotype.Controller(implementsjava.lang.annotation.Annotation)
可以看出都实现了java.lang中的Annotation。它们的用法可以参见《 详解Spring 3.0基于Annotation的依赖注入实现》。看看官方文档的代码片
[java] view plain copy
  1. <prename="code"class="html"style="background-color:rgb(255,255,255);text-align:left;">org.springframework.samples.petclinic.web</pre>
  2. <pre></pre>
  3. <prename="code"class="java">@Controller
  4. publicclassHelloWorldController{
  5. @RequestMapping("/helloWorld")
  6. publicModelAndViewhelloWorld(){
  7. ModelAndViewmav=newModelAndView();
  8. mav.setViewName("helloWorld");
  9. mav.addObject("message","HelloWorld!");
  10. returnmav;
  11. }
  12. }</pre>配置文件:
[html] view plain copy
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="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. <context:component-scanbase-package="org.springframework.samples.petclinic.web"/>
  12. //...
  13. </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


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

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

[java] view plain copy
  1. packageorg.origin100.example.annotation;
  2. publicclassOverrideExample{
  3. privateStringfield;
  4. privateStringattribute;
  5. @Override
  6. publicinthashCode(){
  7. returnfield.hashCode()+attribute.hashCode();
  8. }
  9. @Override
  10. publicStringtoString(){
  11. returnfield+""+attribute;
  12. }
  13. }

Spring中注解一览:

org.springframework.web.bind.annotation

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

Hierarchy For Package org.springframework.stereotype

[java] view plain copy
  1. AnnotationTypeHierarchy
  2. org.springframework.stereotype.Component(implementsjava.lang.annotation.Annotation)
  3. org.springframework.stereotype.Service(implementsjava.lang.annotation.Annotation)
  4. org.springframework.stereotype.Repository(implementsjava.lang.annotation.Annotation)
  5. org.springframework.stereotype.Controller(implementsjava.lang.annotation.Annotation)
可以看出都实现了java.lang中的Annotation。它们的用法可以参见《 详解Spring 3.0基于Annotation的依赖注入实现》。看看官方文档的代码片
[java] view plain copy
  1. <prename="code"class="html"style="background-color:rgb(255,255,255);text-align:left;">org.springframework.samples.petclinic.web</pre>
  2. <pre></pre>
  3. <prename="code"class="java">@Controller
  4. publicclassHelloWorldController{
  5. @RequestMapping("/helloWorld")
  6. publicModelAndViewhelloWorld(){
  7. ModelAndViewmav=newModelAndView();
  8. mav.setViewName("helloWorld");
  9. mav.addObject("message","HelloWorld!");
  10. returnmav;
  11. }
  12. }</pre>配置文件:
[html] view plain copy
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <beansxmlns="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. <context:component-scanbase-package="org.springframework.samples.petclinic.web"/>
  12. //...
  13. </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、付费专栏及课程。

余额充值