设为首页 加入收藏

TOP

深入理解Spring中的各种注解(一)
2015-07-16 12:56:21 来源: 作者: 【 】 浏览:65
Tags:深入 理解 Spring 各种 注解

Spring中的注解大概可以分为两大类:


1)spring的bean容器相关的注解,或者说bean工厂相关的注解;


2)springmvc相关的注解。


spring的bean容器相关的注解,先后有:@Required, @Autowired, @PostConstruct, @PreDestory,还有Spring3.0开始支持的JSR-330标准javax.inject.*中的注解(@Inject, @Named, @Qualifier, @Provider, @Scope, @Singleton).


springmvc相关的注解有:@Controller, @RequestMapping, @RequestParam, @ResponseBody等等。


要理解Spring中的注解,先要理解Java中的注解。


1. Java中的注解


?Java中1.5中开始引入注解,我们最熟悉的应该是:@Override, 它的定义如下:



/**
?* Indicates that a method declaration is intended to override a
?* method declaration in a supertype. If a method is annotated with
?* this annotation type compilers are required to generate an error
?* message unless at least one of the following conditions hold:
?* The method does override or implement a method declared in a
?* supertype.
?* The method has a signature that is override-equivalent to that of
?* any public method declared in Object.
?*
?* @author? Peter von der Ahé
?* @author? Joshua Bloch
?* @jls 9.6.1.4 @Override
?* @since 1.5
?*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}


?


从注释,我们可以看出,@Override的作用是,提示编译器,使用了@Override注解的方法必须override父类或者java.lang.Object中的一个同名方法。我们看到@Override的定义中使用到了 @Target, @Retention,它们就是所谓的“元注解”——就是定义注解的注解,或者说注解注解的注解(晕了...)。我们看下@Retention


?


/**
?* Indicates how long annotations with the annotated type are to
?* be retained.? If no Retention annotation is present on
?* an annotation type declaration, the retention policy defaults to
?* RetentionPolicy.CLASS.
?*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
? ? /**
? ? * Returns the retention policy.
? ? * @return the retention policy
? ? */
? ? RetentionPolicy value();
}


?


@Retention用于提示注解被保留多长时间,有三种取值:


?


public enum RetentionPolicy {
? ? /**
? ? * Annotations are to be discarded by the compiler.
? ? */
? ? SOURCE,
? ? /**
? ? * Annotations are to be recorded in the class file by the compiler
? ? * but need not be retained by the VM at run time.? This is the default
? ? * behavior.
? ? */
? ? CLASS,
? ? /**
? ? * Annotations are to be recorded in the class file by the compiler and
? ? * retained by the VM at run time, so they may be read reflectively.
? ? *
? ? * @see java.lang.reflect.AnnotatedElement
? ? */
? ? RUNTIME
}



RetentionPolicy.SOURCE 保留在源码级别,被编译器抛弃(@Override就是此类); RetentionPolicy.CLASS被编译器保留在编译后的类文件级别,但是被虚拟机丢弃;


RetentionPolicy.RUNTIME保留至运行时,可以被反射读取。


再看 @Target:


?


package java.lang.annotation;


/**
?* Indicates the contexts in which an annotation type is applicable. The
?* declaration contexts and type contexts in which an annotation type may be
?* applicable are specified in JLS 9.6.4.1, and denoted in source code by enum
?* constants of java.lang.annotation.ElementType
?* @since 1.5
?* @jls 9.6.4.1 @Target
?* @jls 9.7.4 Where Annotations May Appear
?*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
? ? /**
? ? * Returns an array of the kinds of elements an annotation type
? ? * can be applied to.
? ? * @return an array of the kinds of elements an annotation type
? ? * can be applied to
? ? */
? ? ElementType[] value();
}


?


?@Target用于提示该注解使用的地方,取值有:


?


public enum ElementType {
? ? /** Class, interface (including annotation type), or enum declaration */
? ? TYPE,
? ? /** Field declaration (includes enum constants) */
? ? FIELD,
? ? /** Method declaration */
? ? METHOD,
? ? /** Formal parameter declaration */
? ? PARAMETER,
? ? /** Constructo

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Python 3.5将支持Async/Await异步.. 下一篇深入掌握Java中的enum

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: