手记

java spring aop 简易版

java spring aop
1、新建java 项目
2、新建lib 文件夹
3、jar 包
com.springsource.net.sf.cglib-2.2.0.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-logging-1.1.1.jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
4、类路径下新建spring配置文件
5、新建一个接口,有四个方法:加减乘除;
6、实现类,实现5 中生命的方法
7、新建一个类记录日志;作用:在方法调用前打印日志,方法结束后打印日志,方法返回值进行接收,如果方法执行抛出异常要对异常信息进行输出
8、先不通过配置文件的方式,aspectj 表达式通过配置文件的方式一下子不太容易理解

代码:
package com.curtis.spring.aop.calculate;
public interface ICalculate {
    int add(int x, int y);
    int sub(int x, int y);
    int mul(int x, int y);
    int div(int x, int y);
}

package com.curtis.spring.aop.calculate;
import org.springframework.stereotype.Component;
@Component(value = "calculate")
public class CalculateImpl implements ICalculate {
    @Override
    public int add(int x, int y) {
        int result = x + y;
        return result;
    }
    @Override
    public int sub(int x, int y) {
        int result = x - y;
        return result;
    }
    @Override
    public int mul(int x, int y) {
        int result = x * y;
        return result;
    }
    @Override
    public int div(int x, int y) {
        int result = x / y;
        return result;
    }
}

package com.curtis.spring.aop.calculate;
import java.util.Arrays;
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.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class CalculateLogging {    
    // 前置通知
    @Before(value = "execution(public int com.curtis.spring.aop.calculate.ICalculate.*(int,int))")
    public void beforeMethod(JoinPoint joinPoint) {
        System.out.println(
                joinPoint.getSignature().getName() + " method start with " + Arrays.asList(joinPoint.getArgs()));
    }
    // 后置通知
    @After(value = "execution(public int com.curtis.spring.aop.calculate.ICalculate.*(int,int))")
    public void afterMethod(JoinPoint joinPoint) {
        System.out.println(joinPoint.getSignature().getName() + " method end");
    }
    // 返回通知,如果执行主体方法有返回值,对返回值进行接收
    @AfterReturning(value = "execution(* com.curtis.spring.aop.calculate.ICalculate.*(..))", returning = "result")
    public void afterReturn(JoinPoint joinPoint, Object result) {
        System.out.println(joinPoint.getSignature().getName() + " method result is " + result);
    }
    // 异常通知,当方法执行时如果有异常,则将异常信息进行捕获
    @AfterThrowing(value = "execution(* com.curtis.spring.aop.calculate.ICalculate.*(..))", throwing = "e")
    public void afterThrowing(JoinPoint joinPoint, Exception e) {
        System.out.println(joinPoint.getSignature().getName() + " method exception is " + e);
    }
    // 任意修饰符+任意返回值
    // 接口类中的所有方法
    // 两点:任意参数
}

package com.curtis.spring.aop.calculate;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestCalulate {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        ICalculate calculate = null;
        calculate = ctx.getBean("calculate", ICalculate.class);
        int result = calculate.add(2, 3);
        result = calculate.div(10, 5);
    }
}

<?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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
    <context:component-scan base-package="com.curtis.spring.aop.calculate" />
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
4人推荐
随时随地看视频
慕课网APP

热门评论

<bean id="calculateImpl" class="com.curtis.spring.aop.calculate2.CalculateImpl"></bean>

<bean id="calculateLogging" class="com.curtis.spring.aop.calculate2.CalculateLogging"></bean>

<aop:config>

<!-- 接口中的方法 -->

<aop:pointcut expression="execution(* com.curtis.spring.aop.calculate2.ICalculate.*(..))" id="pointcut"/>

<aop:aspect ref="calculateLogging" order="1">

<aop:before method="beforeMethod" pointcut-ref="pointcut"/>

<aop:after method="afterMethod" pointcut-ref="pointcut"/>

<aop:after-returning method="afterReturnMethod" returning="result" pointcut-ref="pointcut"/>

</aop:aspect>

</aop:config>


查看全部评论