以前写的项目都是些在配置文件里的,现在改用注解了,需要用到一个拦截器,比如,操作某个操作时,需要用户登录,这个拦截器怎么用注解写啊,希望各路大侠拔刀相助,指点下,最好多给点代码,谢谢啊……

解决方案 »

  1.   

     = = 。。百度查被。。一大堆。。不就是spring的aop 的annotation配置方法吗?还需要在这里提问啊。。
      

  2.   

    spring xml加上<aop:aspectj-autoproxy/>import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.After;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.AfterThrowing;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.stereotype.Component;
    //使用注解
    /**
     * 切面
     */
    @Aspect @Component
    public class Interceptor {
    @SuppressWarnings("unused")
    @Pointcut("execution (* com.zhym.dao.impl.UsersDao.*(..))")
    private void method(){};//切入点
    //传参数
    @Before("method() && args(name)") 
    public void doBefore(String name){
    System.out.println("前置通知"+name);
    }
    //获得返回参数
    @AfterReturning(pointcut="method()",returning="result")
    public void doRetruning(String result){
    System.out.println("后置通知"+result);
    }
    @After("method()")
    public void doAfter(){
    System.out.println("最终通知");
    }
    //例外通知传参数获得
    @AfterThrowing(pointcut="method()",throwing="e")
    public void doThrowing(Exception e){
    System.out.print("例外通知");
    }
    @Around("method()")
    public Object doBasi(ProceedingJoinPoint pjp) throws Throwable {
    System.out.println("进入方法!");
    Object result = pjp.proceed();
    System.out.print("退出方法!");
    return result;
    }
    }