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;

    @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


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值