设为首页 加入收藏

TOP

深入理解Spring中的各种注解(二)
2015-07-16 12:56:21 来源: 作者: 【 】 浏览:64
Tags:深入 理解 Spring 各种 注解
r 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
}


?


分别表示该注解可以被使用的地方:1)类,接口,注解,enum; 2)属性域;3)方法;4)参数;5)构造函数;6)局部变量;7)注解类型;8)包


所以:


@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Override {
}


表示 @Override 只能使用在方法上,保留在源码级别,被编译器处理,然后抛弃掉。


还有一个经常使用的元注解 @Documented :


?


/**
?* Indicates that annotations with a type are to be documented by javadoc
?* and similar tools by default.? This type should be used to annotate the
?* declarations of types whose annotations affect the use of annotated
?* elements by their clients.? If a type declaration is annotated with
?* Documented, its annotations become part of the public API
?* of the annotated elements.
?*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Documented {
}


?


表示注解是否能被 javadoc 处理并保留在文档中。


2. 使用 元注解 来自定义注解 和 处理自定义注解


有了元注解,那么我就可以使用它来自定义我们需要的注解。结合自定义注解和AOP或者过滤器,是一种十分强大的武器。比如可以使用注解来实现权限的细粒度的控制——在类或者方法上使用权限注解,然后在AOP或者过滤器中进行拦截处理。下面是一个关于登录的权限的注解的实现:


?


/**
?* 不需要登录注解
?*/
@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface NoLogin {
}


?


我们自定义了一个注解 @NoLogin, 可以被用于 方法 和 类 上,注解一直保留到运行期,可以被反射读取到。该注解的含义是:被 @NoLogin 注解的类或者方法,即使用户没有登录,也是可以访问的。下面就是对注解进行处理了:


?


/**
?* 检查登录拦截器
?* 如不需要检查登录可在方法或者controller上加上@NoLogin
?*/
public class CheckLoginInterceptor implements HandlerInterceptor {
? ? private static final Logger logger = Logger.getLogger(CheckLoginInterceptor.class);


? ? @Override
? ? public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
? ? ? ? ? ? ? ? ? ? ? ? ? ? Object handler) throws Exception {
? ? ? ? if (!(handler instanceof HandlerMethod)) {
? ? ? ? ? ? logger.warn("当前操作handler不为HandlerMethod=" + handler.getClass().getName() + ",req="
? ? ? ? ? ? ? ? ? ? ? ? + request.getQueryString());
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? HandlerMethod handlerMethod = (HandlerMethod) handler;
? ? ? ? String methodName = handlerMethod.getMethod().getName();
? ? ? ? // 判断是否需要检查登录
? ? ? ? NoLogin noLogin = handlerMethod.getMethod().getAnnotation(NoLogin.class);
? ? ? ? if (null != noLogin) {
? ? ? ? ? ? if (logger.isDebugEnabled()) {
? ? ? ? ? ? ? ? logger.debug("当前操作methodName=" + methodName + "不需要检查登录情况");
? ? ? ? ? ? }
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? noLogin = handlerMethod.getMethod().getDeclaringClass().getAnnotation(NoLogin.class);
? ? ? ? if (null != noLogin) {
? ? ? ? ? ? if (logger.isDebugEnabled()) {
? ? ? ? ? ? ? ? logger.debug("当前操作methodName=" + methodName + "不需要检查登录情况");
? ? ? ? ? ? }
? ? ? ? ? ? return true;
? ? ? ? }
? ? ? ? if (null == request.getSession().getAttribute(CommonConstants.SESSION_KEY_USER)) {
? ? ? ? ? ? logger.warn("当前操作" + methodName + "用户未登录,ip=" + request.getRemoteAddr());
? ? ? ? ? ? response.getWriter().write(JsonConvertor.convertFailResult(ErrorCodeEnum.NOT_LOGIN).toString()); // 返回错误信息
? ? ? ? ? ? return false;
? ? ? ? }
? ? ? ? return true;
? ? }
? ? @Override
? ? public void postHandle(HttpServletRequest request, HttpServletResponse response,
? ? ? ? ? ? ? ? ? ? ? ? ? Object handler, ModelAndView modelAndView) throws Exception {
? ? }
? ? @Override
? ? public void afterCompletion(HttpServletRequest request, HttpServletResponse respo

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

评论

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