sprint aspectj配合注解使用

    一 自定义注解
    import java.lang.annotation.Documented;
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Target({ElementType.FIELD,ElementType.METHOD})
    @Documented
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Verify {
    
    	
    	public String value() default "";
    	
    }
    
    二 aspectJ 类进行增强
    
    
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Field;
    
    import org.aspectj.lang.JoinPoint;
    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;
    
    @Component
    @Aspect
    public class VerifyAuthority {
    	
    	@Pointcut(value="@annotation(com.souche.aspect.Verify)")
    	public void pointCut() {
    		
    	}
    	// 前置 方法执行前执行
    	@Before(value="pointCut()")
    	public void verifyUser(JoinPoint joinPoint) {
    		Object[] args = joinPoint.getArgs();
    		if (null == args || args.length == 0) {
    			return;
    		}
    		for (Object arg : args) {
    			if(arg ==null)
    			{
    				continue;
    			}
    			// 一 :做方法拦截  
    			// 拿到参数进行处理校验等
    			System.out.println(2);
    //			if(arg instanceof User) {
    //			 业务:	验证用户是否登录,权限等
    //				
    //				// 
    //				
    //				
    //			}
    			
    			// 二 对象属性拦截
    			//通过反射获取class 对象
    			Class<? extends Object> class1 = arg.getClass();
    			// 包含私有字段
    			Field[] fields = class1.getDeclaredFields();
    			for (Field field : fields) {
    				
    //				// 得到的是当前成员所有的注释,不包括继承的
    //				Annotation[] annotations = field.getDeclaredAnnotations();
    //				// 包括继承的所有注释
    //				Annotation[] annotations2 = field.getAnnotations();
    //				// 获取指定注解
    //				Verify annotation = field.getAnnotation(Verify.class);
    //				// 获取指定注解
    //				Verify annotation1 = field.getDeclaredAnnotation(Verify.class);
    //				// 是否是Verify注解
    //				boolean b = field.isAnnotationPresent(Verify.class);
    //				if(annotation !=null) {
    //					String value = annotation.value();
    //					System.out.println(value);
    //				}
    				// 处理自己业务逻辑
    				
    				
    			}
    			
    		}
    		
    		
    		
    	}
    	
    	
    	// 统一异常
    	@AfterThrowing(throwing="ex",pointcut=""//或者value="pointCut()"
    			)
    	public void throwing(Exception ex) {
    		// 业务逻辑
    		
    	}
    	// 后置 方法执行后执行
    	@AfterReturning(value = "pointCut()")
    	public void afterReturning() {
    		// 业务逻辑
    		
    		
    		
    	}
    	//环绕 方法执行前后,都会执行
    	@Around(value="pointCut()")
    	public void around() {
    		// 业务逻辑
    	}
    	// 最终通知
    	@After(value = "pointCut()")
    	public void after() {
    		// 业务逻辑
    	}
    	
    	
    	
    }


三 配置文件形式
<!-- 增强 -->
   <bean id="verifyAuthority" class="cn.aop.VerifyAuthority"></bean>
  <aop:config>
    
     <aop:pointcut expression="execution(public * *(..))" id="allpointcut"/>
    <aop:aspect ref="verifyAuthority">
    <!-- 前置 -->
      <aop:before method="before" pointcut-ref="allpointcut"/>
      <!-- 后置 -->
      <aop:after-returning method="afterReturning" pointcut-ref="allpointcut"/>
      <!-- 环绕 -->
      <aop:around method="around" pointcut-ref="allpointcut"/>
      <!-- 异常抛出 -->
      <aop:after-throwing method="afterThrowing" pointcut-ref="allpointcut"/>
      <!-- 最终增强 -->
      <aop:after method="after" pointcut-ref="allpointcut"/>
      
    </aop:aspect>
  </aop:config>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值