标签:
1).加入jar包:下载地址
spring-beans-4.1.6.RELEASE.jar
commons-logging-1.1.3.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar
spring-aop-4.1.6.RELEASE.jar
spring-aspects-4.1.6.RELEASE.jar
aspectjweaver-1.5.2a.jar
aopalliance-1.0.jar
需要下载aspectjweaver.jar包
2).在配置文件中加入aop的命名空间
3).基于注解的方式
1.在配置文件中加入如下配置:
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
2.把横切关注点的代码抽象到切面的类中
3.在类中声明各种通知:
4.可以再通知方法中声明一个类型为JoinPoint的参数,然后就能访问链接细节,如方法名称和参数值
代码:
package com.cn.spring.aop.impl; //加减乘除的接口类 public interface ArithmeticCalculator { int add(int i, int j); int sub(int i, int j); int mul(int i, int j); int div(int i, int j); }
package com.cn.spring.aop.impl; import org.springframework.stereotype.Component; //实现类 @Component public class ArithmeticCalculatorImpl implements ArithmeticCalculator { @Override public int add(int i, int j) { int result = i + j; return result; } @Override public int sub(int i, int j) { int result = i - j; return result; } @Override public int mul(int i, int j) { int result = i * j; return result; } @Override public int div(int i, int j) { int result = i / j; return result; } }
package com.cn.spring.aop.impl; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; import java.util.Arrays; import java.util.List; //把这个类声明为一个切面:首先需要把该类放入到IOC容器中,在声明为一个切面 @Aspect @Component public class LoggingAspect { //声明该方法是一个前置通知:在目标方法开始之前执行 @Before("execution(public int ArithmeticCalculator.*(int, int))") public void beforeMethod(JoinPoint joinPoint) { String methodName = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("The method " + methodName + " begins with " + args); } }
package com.cn.spring.aop.impl; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { //1.创建Spring的IOC容器 ApplicationContext ctx = new ClassPathXmlApplicationContext("17-1.xml"); //2.从IOC容器中huo获取bean的实例 ArithmeticCalculator arithmeticCalculator = ctx.getBean(ArithmeticCalculator.class); //3.使用bean int result = arithmeticCalculator.add(3, 6); System.out.println("result:" + result); } }
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:component-scan base-package="com.cn.spring.aop.impl"> </context:component-scan> <!--使AspjectJ注解起作用:自动为匹配的类生成代理对象--> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
标签:
原文地址:http://www.cnblogs.com/jecyhw/p/4590444.html