JAVA常用自定义的注解使用
@Deprecated 一般表示该方法可能在以后的版本不用了
@Retention 注释类型的注释要保留多久,可配置RetentionPolicy类型的数据,只能配置一种
package java.lang.annotation;
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. * 编译器将把注释记录在类文件中,但在运行时 VM 不需要保留注释 */
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 * 编译器将把注释记录在类文件中,在运行时 VM 将保留注释,因此可以反射性地读取。 */
RUNTIME
}
@Target 限定注解,指示注释类型所适用的程序元素的种类。可配置ElementType类型的数据,可同时配置多个
package java.lang.annotation;
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,
/** Constructor declaration 构造方法 */
CONSTRUCTOR,
/** Local variable declaration 局部变量 */
LOCAL_VARIABLE,
/** Annotation type declaration 注释类型 */
ANNOTATION_TYPE,
/** Package declaration 包 */
PACKAGE,
/** * Type parameter declaration * * @since 1.8 * 类型变量的声明语句 */
TYPE_PARAMETER,
/** * Use of a type * * @since 1.8 * 使用类型的任何语句(例如声明语句、泛型和强制转换语句中的类型) */
TYPE_USE
}
@Documented 可以在制作JavaDoc文件的同时将注解信息加入到API文件中
@ModelAttribute 注释的方法会在此controller每个方法执行前被执行,可以对controller执行数据前添加一些数据
@MethodAnnotation(desc = "在获取requerst之前先设置所有公共参数的值")
@ModelAttribute
public void setReqAndRes(HttpServletRequest request,HttpServletResponse response){
RequestResponseContext.setRequest(request);
RequestResponseContext.setResponse(response);
}
public HttpServletRequest getRequest() {
return RequestResponseContext.getRequest();
}
public HttpServletResponse getResponse() {
return RequestResponseContext.getResponse();
}
文章主要参考
关于在spring 容器初始化 bean 和销毁前所做的操作:
第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
第二种是:通过 在xml中定义init-method 和 destory-method方法
还没有评论,来说两句吧...