论坛首页 Java企业应用论坛

SpringAOP:切面中如何访问切点方法上注解的信息

浏览 19697 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-10-09  

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呢?

   发表时间:2013-10-11  
        JoinPoint jp;
        jp.getTarget(); //得到目标对象
        jp.getSignature().getName();//得到方法名
        jp.getArgs(); //得到方法参数
       
        然后反射拿到方法
0 请登录后投票
   发表时间:2013-10-11  
jinnianshilongnian 写道
        JoinPoint jp;
        jp.getTarget(); //得到目标对象
        jp.getSignature().getName();//得到方法名
        jp.getArgs(); //得到方法参数
       
        然后反射拿到方法

理论上应该是可以做到,我今天发现一片在职人员写的博客,涉及到这方面,看到后才恍然明白,实现是这样的:
     @Before("@annotation(auth)")
     public void record(JoinPoint joinPoint, Auth auth) {}
注解com.wsc.yc.Auth auth是可以直接当作参数传进去,类似JoinPoint。
已经实现了我要实现的“最近访客”的功能,验证该办法可行。
遗憾的是参看了很多书籍却没有找到这方面的知识
0 请登录后投票
   发表时间:2013-10-12  
十三月的 写道
jinnianshilongnian 写道
        JoinPoint jp;
        jp.getTarget(); //得到目标对象
        jp.getSignature().getName();//得到方法名
        jp.getArgs(); //得到方法参数
       
        然后反射拿到方法

理论上应该是可以做到,我今天发现一片在职人员写的博客,涉及到这方面,看到后才恍然明白,实现是这样的:
     @Before("@annotation(auth)")
     public void record(JoinPoint joinPoint, Auth auth) {}
注解com.wsc.yc.Auth auth是可以直接当作参数传进去,类似JoinPoint。
已经实现了我要实现的“最近访客”的功能,验证该办法可行。
遗憾的是参看了很多书籍却没有找到这方面的知识

嗯 这种方式简单    语法可以去看AspectJ in action
0 请登录后投票
   发表时间:2013-10-12  
http://jinnianshilongnian.iteye.com/blog/1415606
之前总结过,应该是目前spring支持的aspectj最强的语法了

@Pointcut(value="@annotation(secure)", argNames="secure") 
private void pointcut2(Secure secure){} 
1 请登录后投票
   发表时间:2013-10-12  
jinnianshilongnian 写道
http://jinnianshilongnian.iteye.com/blog/1415606
之前总结过,应该是目前spring支持的aspectj最强的语法了

@Pointcut(value="@annotation(secure)", argNames="secure") 
private void pointcut2(Secure secure){} 

是,我今天看了AspectJ进阶部分,称为“绑定类注解对象”,还有其他“绑定代理对象”,“绑定返回值”,“绑定抛出的异常” AspectJ确实强大!
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics