Spring中Aop的2种实现方式

什么都不说,直接上代码:

  在配置文件中设置aop通知,当程序运行到相应的方法时,通过代理触发通知,使程序员能通过通知了解对程序运行。实现面向方面编程:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    ">

<bean id="fbi" class="com.aop.service.FBI"></bean>
<bean id="man" class="com.aop.service.Man"></bean>

<aop:config>
    <aop:aspect ref="fbi">
        <aop:pointcut id="pointcutA" expression="execution(* com.aop.service.Man.*())"/>
        <aop:around method="round" pointcut-ref="pointcutA"/>
        <aop:before method="before" pointcut-ref="pointcutA"/>
        <aop:after method="after" pointcut-ref="pointcutA"/>
        <aop:after-returning method="afterReturn" pointcut-ref="pointcutA"/>
        <aop:after-throwing method="afterThrowing" pointcut-ref="pointcutA"/>
    </aop:aspect>
</aop:config>
</beans>

使用自动代理的方式,在特定的java类中,通过Spring的注解实现Aop的功能:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
       http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
       http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
       http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean id="fbi" class="com.aop.aspect.FBI"></bean>
<bean id="man" class="com.aop.aspect.Man"></bean>
</beans>

对第二种方式的切面类:

package com.aop.aspect;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
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;

/**
 * 基于注解的切面类
 * @author liujingzhai
 *
 */
@Aspect
public class FBI {

    private static final Logger logger=Logger.getLogger(FBI.class);
    
    /**
     * 通过applicatonContext.xml配置的环绕通知
     * @param joinPoint 切点
     * @return
     * @throws Throwable
     */
    @Pointcut("execution(* com.aop.aspect.*(..))")
    @Around("around()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable{
        String methodName=joinPoint.getSignature().getName();
        if(methodName.equals("mm")){
            logger.info("stop man's mm!");
            System.out.println("stop man's mm!");
        }else if(methodName.equals("qq")){
            logger.info("find qq....");
            joinPoint.proceed();
            logger.info("stop man's qq!");
            System.out.println("stop man's qq!");
        }else if(methodName.equals("flee")){
            logger.info("find man's flee");
            System.out.println("find man's flee");
            return "want to flee??? no way........";
        }
        return null;
    }
    
    /**
     * 前置通知
     * @param joinPoint 切点
     */
    @Before("execution(* com.aop.aspect.Man.*(..))")
    public void before(JoinPoint joinPoint){
        //获取目标类名
        String className=joinPoint.getTarget().getClass().getName();
        //获取目标对象的方法名
        String methodName=joinPoint.getSignature().getName();
        System.out.println("前置通知:当前正在执行--->"+className+",执行的方法名--->"+methodName);
    }
    
    @AfterReturning("execution(* com.aop.aspect.Man.*(..))")
    public void afterReturn(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();
        System.out.println("后置通知:当前正在执行--->"+"after方法"+methodName+"执行完了");
    }
    
    @After("execution(* com.aop.aspect.Man.*(..))")
    public void after(JoinPoint joinPoint){
        System.out.println("最终通知:不管方法有没有正常执行完成,一定会返回");
    }
    
    @AfterThrowing("execution(* com.aop.aspect.Man.*(..))")
    public void afterThrowing(){
        System.out.println("异常抛出后通知:方法执行时出异常了");  
    }
}

接口:
package com.aop.aspect;

public interface IMan {

    void qq();
    void mm();
    Object flee();
}

切面的目标类:

package com.aop.aspect;

import org.apache.log4j.Logger;

/**
 * 要被切面的目标类
 * @author tfq
 *
 */
public class Man implements IMan{
    //Logger logger = Logger.getLogger(Man.class);
    private static final Logger logger=Logger.getLogger(Man.class);
    
    public Object flee() {
        return "found fbi,i want to flee.....";
    }

    public void mm() {
        logger.info("I am doing mm!");
    }

    public void qq() {
        logger.info("I am doing qq!");
    }
}

切面的测试类:

package com.aop.aspect;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAop {

    public static void main(String[] args) {
        ApplicationContext acx=new ClassPathXmlApplicationContext("com/applicationContext2.xml");
        IMan iman=(IMan)acx.getBean("man");
        iman.qq();
        iman.mm();
        System.out.println(iman.flee());
    }
}

第一种方式:
package com.aop.service;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

/**
 * 切面类
 * @author liujingzhai
 *
 */
public class FBI {

    private static final Logger logger=Logger.getLogger(FBI.class);
    
    /**
     * 通过applicatonContext.xml配置的环绕通知
     * @param joinPoint 切点
     * @return
     * @throws Throwable
     */
    public Object round(ProceedingJoinPoint joinPoint) throws Throwable{
        String methodName=joinPoint.getSignature().getName();
        if(methodName.equals("mm")){
            logger.info("stop man's mm!");
        }else if(methodName.equals("qq")){
            logger.info("find qq....");
            joinPoint.proceed();
            logger.info("stop man's qq!");
        }else if(methodName.equals("flee")){
            logger.info("find man's flee");
            return "want to flee??? no way........";
        }
        return null;
    }
    
    /**
     * 前置通知
     * @param joinPoint 切点
     */
    public void before(JoinPoint joinPoint){
        //获取目标类名
        String className=joinPoint.getTarget().getClass().getName();
        //获取目标对象的方法名
        String methodName=joinPoint.getSignature().getName();
        System.out.println("前置通知:当前正在执行--->"+className+",执行的方法名--->"+methodName);
    }
    
    public void afterReturn(JoinPoint joinPoint){
        System.out.println("最终通知:不管方法有没有正常执行完成,一定会返回");
    }
    
    public void after(JoinPoint joinPoint){
        String methodName=joinPoint.getSignature().getName();
        System.out.println("after方法"+methodName+"执行完了");
    }
    
    public void afterThrowing(){
        System.out.println("异常抛出后通知:方法执行时出异常了");  
    }
}

接口

package com.aop.service;

public interface IMan {

    void qq();
    void mm();
    Object flee();
}

目标类:

package com.aop.service;

import org.apache.log4j.Logger;

/**
 * 要被切面的目标类
 * @author tfq
 *
 */
public class Man implements IMan{
    //Logger logger = Logger.getLogger(Man.class);
    private static final Logger logger=Logger.getLogger(Man.class);
    
    public Object flee() {
        return "found fbi,i want to flee.....";
    }

    public void mm() {
        logger.info("I am doing mm!");
    }

    public void qq() {
        logger.info("I am doing qq!");
    }

}

测试:

package com.aop.service;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestAop {

    public static void main(String[] args) {
        ApplicationContext acx=new ClassPathXmlApplicationContext("com/applicationContext1.xml");
        IMan iman=(IMan)acx.getBean("man");
        iman.qq();
        iman.mm();
        System.out.println(iman.flee());
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值