美文网首页
Sprint-AOP

Sprint-AOP

作者: 打死你的小白兔 | 来源:发表于2018-03-30 03:29 被阅读0次


public class Hello {
public void save(){
    System.out.println("ssssssssssssssss"); 
}   
}
==================================================================
public class Aspec {
    public void befor(){
        System.out.println("前置+++++++++++++");
    }
}
=================================================================
  
    <bean id="hello" class="com.hw.domain.Hello"></bean>
    <bean id="aspec" class="com.hw.domain.Aspec"></bean>
       <aop:config>
       <aop:pointcut expression="execution(* com.hw.domain.Hello.*(..))" id="perform"/>
       <aop:aspect ref="aspec">
       <aop:before method="befor"  pointcut-ref="perform"/>
       </aop:aspect>
       </aop:config>

注解AOP

 <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<context:component-scan base-package="com"></context:component-scan>
---------------------------------------------------------
@Aspect
@Component(value="aspec")
public class Aspec {
    @Before("execution(* com.hw.domain.Hello.*(..))")
    public void befor(){
        System.out.println("前置+++++++++++++");
    }
}
-----------------------------------------------------------------
@Component(value="hello")
public class Hello {

public void save(){
    System.out.println("ssssssssssssssss");
    
}   
}

AOP的各种通知

public class Transaction {
    /**
     * 前置通知
     * 参数:JoinPoint 连接点:客户端调用哪个方法,这个方法就是连接点
     */
    public void beginTransaction(JoinPoint joinPoint){
        System.out.println("目标类:"+joinPoint.getTarget().getClass());
        System.out.println("目标方法的名称:"+joinPoint.getSignature().getName());
        System.out.println("目标方法的参数:"+joinPoint.getArgs().length);
        System.out.println("begin transaction");
    }
    
    /**
     * 后置通知
     */
    public void commit(JoinPoint joinPoint,Object val){
        System.out.println("返回值:"+val);
        System.out.println("commit");
    }
    
    /**
     * 异常通知
     */
    public void throwingMethod(JoinPoint joinPoint,Throwable ex){
        System.out.println(ex.getMessage());
    }
    
    /**
     * 最终通知
     */
    public void finallyMethod(){
        System.out.println("finally method");
    }
    
    /**
     * 环绕通知
     */
    public Object aroundMethod(ProceedingJoinPoint joinPoint) throws Throwable{
        System.out.println("around method");
        return joinPoint.proceed();//执行目标方法
    }
}

相关文章

网友评论

      本文标题:Sprint-AOP

      本文链接:https://www.haomeiwen.com/subject/nxrmcftx.html