1:自定义pojo,通过@Aspect注解,将该类声明成一个切面。代码如下
@Aspect public class VisitHistory { @Resource private UserService userService; @Resource private VisitorService visitorService; @Resource private BlogService blogService; /** * 浏览历史 * * @param joinPoint */ @Before("@annotation(com.wsc.yc.annotation.Auth)") public void record(JoinPoint joinPoint) { Class s = joinPoint.getSignature().getDeclaringType(); System.out.println(s); Auth auth = (Auth) joinPoint.getSignature().getDeclaringType() .getAnnotation(Auth.class); RequestMapping rm = (RequestMapping) joinPoint.getSignature() .getDeclaringType().getAnnotation(RequestMapping.class); if (auth.verifyLogin()) {
在advice中,record方法切入到方法上有com.wsc.yc.annotation.Auth注解的切点
2:com.wsc.yc.annotation.Auth是自己定义的注解
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) @Documented @Inherited public @interface Auth { /** * 是否验证登录 * * @return */ public boolean verifyLogin() default false; }
3:我想实现一个“最近访客”的功能,类似Iteye,在每一个验证了用户登录的方法上织入一个切面,该切面用来将这个记录信息写入到数据库。问题是如何在该切面中record方法访问注解信息?
上面的方法是错误的,如何才能访问到该切点上注解Auth呢?
我的切点例如:
* @return */ @Auth(verifyLogin = true) @RequestMapping("/homepage") public ModelAndView home(HttpServletRequest request) { // 判断当前用户是否登录 User host = SessionUtil.getUser(request); boolean isLogin = false; if (host != null) { isLogin = true; } // 获取用户昵称 String nickname = Encode.encode(request.getParameter("nickname")); boolean isHost = false; if (nickname.equals(host.getNickname())) { isHost = true; }
如何才能判断某个JoinPoint上的Auth的verifyLogin是true还是false呢?