1.配置spring容器
导入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.3.jar
spring-aop-4.2.2.RELEASE.jar
spring-beans-4.2.2.RELEASE.jar
spring-context-4.2.2.RELEASE.jar
spring-core-4.2.2.RELEASE.jar
spring-expression-4.2.2.RELEASE.jar
配置xml文件

1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:context="http://www.springframework.org/schema/context" 7 xsi:schemaLocation="http://www.springframework.org/schema/beans 8 http://www.springframework.org/schema/beans/spring-beans.xsd 9 http://www.springframework.org/schema/context 10 http://www.springframework.org/schema/context/spring-context.xsd 11 http://www.springframework.org/schema/aop 12 http://www.springframework.org/schema/aop/spring-aop.xsd"> 13 14 <bean id="arithmeticCaludaImpXML" class="com.eduask.liusheng.aop.xml.ArithmeticCaludaImp"></bean> 15 <bean id="logXML" class="com.eduask.liusheng.aop.xml.Log"></bean> 16 17 <aop:config> 18 <!-- 配置切点 --> 19 <aop:pointcut expression="execution(* com.eduask.liusheng.aop.xml.*.*(..))" id="pointcut"/> 20 <!-- 配置切面 --> 21 <aop:aspect id="logAspect" ref="logXML"> 22 <aop:before method="before" pointcut-ref="pointcut"/> 23 <aop:after method="after" pointcut-ref="pointcut"/> 24 <aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/> 25 <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/> 26 </aop:aspect> 27 </aop:config> 28 29 </beans>
2.ArithmeticCaluda.java

1 public interface ArithmeticCaluda {
2
3 /**
4 * 加
5 * @param a
6 * @param b
7 * @return 和
8 */
9 double add(double a, double b);
10
11 /**
12 * 减
13 * @param a
14 * @param b
15 * @return 差
16 */
17 double sub(double a, double b);
18
19 /**
20 * 乘
21 * @param a
22 * @param b
23 * @return 积
24 */
25 double mul(double a, double b);
26
27 /**
28 * 除
29 * @param a
30 * @param b
31 * @return 商
32 */
33 double div(double a, double b);
34
35 }
3.ArithmeticCaludaImp.java

1 public class ArithmeticCaludaImp implements ArithmeticCaluda {
2 /**
3 * 加
4 * @param a
5 * @param b
6 * @return 和
7 */
8 public double add(double a,double b){
9 double result=a+b;
10 return result;
11 }
12 /**
13 * 减
14 * @param a
15 * @param b
16 * @return 差
17 */
18 public double sub(double a,double b){
19 double result=a-b;
20 return result;
21 }
22 /**
23 * 乘
24 * @param a
25 * @param b
26 * @return 积
27 */
28 public double mul(double a,double b){
29 double result=a*b;
30 return result;
31 }
32 /**
33 * 除
34 * @param a
35 * @param b
36 * @return 商
37 */
38 public double div(double a,double b){
39 double result=a/b;
40 // int i=10/0;
41 return result;
42 }
43 }
4.Log.java

1 import java.util.Arrays;
2 import org.aspectj.lang.JoinPoint;
3
4 public class Log {
5
6 public void before(JoinPoint jp){
7 System.out.println("the method "+jp.getSignature().getName()+"() begin with "+Arrays.asList(jp.getArgs()));
8 }
9
10 public void after(){
11 System.out.println("the method after ");
12 }
13
14 public void afterReturning(JoinPoint jp,Object result){
15 System.out.println("the method "+jp.getSignature().getName()+"() end with ["+result+"]");
16 }
17
18 public void afterThrowing(Exception e){
19 System.out.println("afterThrowing"+e.getMessage());
20 }
21 }
5.Test.java

1 import org.springframework.context.ApplicationContext;
2 import org.springframework.context.support.ClassPathXmlApplicationContext;
3 /**
4 * aop之xml结合JoinPoint实现动态代理
5 * @author Administrator
6 *
7 */
8 public class Test {
9 public static void main(String[] args) {
10 ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext-aop-xml.xml");
11 ArithmeticCaluda arithmeticCaluda=(ArithmeticCaluda) app.getBean("arithmeticCaludaImpXML");
12 arithmeticCaluda.div(10, 2);
13 }
14 }
运行效果与注解的形式一致
需要注意的是:
配置切点<aop:pointcut />要在配置切面<aop:aspect />之前,否则报错;
配置<aop:after />与<aop:after-returning />时,顺序不同,执行结果也不同,顺序在前先执行
来源:https://www.cnblogs.com/qq634571685/p/7168885.html
