Spring中AOP最简单实例-XML形式
一、项目结构
二、基于maven(引入Jar包)
在引入pom文件时,需要注意:
第一,因为使用的是结合Spring的,要引入Spring的pom。统一版本为5.1.5.RELEASE。
<properties> <spring.version>5.1.5.RELEASE</spring.version> </properties>
spring相关Jar包引入 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency>
第二,因为要用到aspect的切面包。要引入aspect相关包。
aspect相关Jar包 <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.2</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.2</version> </dependency>
第三,用到测试类,引入junit 测试包。
测试类相关Jar包 <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
三、实体类(需要被切面切的类)
package com.isoftstone.mcb; public class HelloWorld { private String name; public String getName() { System.out.println("我是" + name); return name; } public void setName(String name) { this.name = name; } public void printThrowException(){ System.out.println("我扔出了一个IllegalArgumentException"); throw new IllegalArgumentException(); } }
四、切面类(切入到别人的类)
package com.isoftstone.mcb; import org.aspectj.lang.ProceedingJoinPoint; public class AspactLog { public void logStart(){ System.out.println("--------logStart-------"); } public void logEnd(){ System.out.println("--------logEnd-------"); } public void logReturn(Object returnVal){ System.out.println("把 "+returnVal+" 返回"); } public void logException(IllegalArgumentException ex){ System.out.println("我是切面,我捕获了一个异常"+ex.toString()); } public Object Around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{ System.out.println("@Arount:执行目标方法之前..."); Object obj = proceedingJoinPoint.proceed();//相当于开始调div地 System.out.println("@Arount:执行目标方法之后..."); return obj; } }
五、applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> <aop:config> <aop:aspect id="LogAspact" ref="logbean"> <aop:pointcut id="hello" expression="execution(* com.isoftstone.mcb.*.*(..))" /> <aop:before pointcut-ref="hello" method="logStart" /> <aop:after pointcut-ref="hello" method="logEnd" /> <aop:after-returning pointcut-ref="hello" method="logReturn" returning="returnVal"/> <aop:after-throwing method="logException" throwing="ex" pointcut-ref="hello" /> <aop:around method="Around" pointcut-ref="hello" /> </aop:aspect> </aop:config> <bean id="logbean" class="com.isoftstone.mcb.AspactLog" /> <bean id="helloWorld" class="com.isoftstone.mcb.HelloWorld"> <property name="name" value="张三" /> </bean> </beans>
六、MainTest测试类
import com.isoftstone.mcb.HelloWorld; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class MainTest { @Test public void test01() { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); HelloWorld world = (HelloWorld) applicationContext.getBean("helloWorld"); world.getName(); world.printThrowException(); } }
七、测试结果
Connected to the target VM, address: '127.0.0.1:62241', transport: 'socket' --------logStart------- @Arount:执行目标方法之前... 我是张三 @Arount:执行目标方法之后... 把 张三 返回 --------logEnd------- --------logStart------- @Arount:执行目标方法之前... 我扔出了一个IllegalArgumentException 我是切面,我捕获了一个异常java.lang.IllegalArgumentException --------logEnd------- java.lang.IllegalArgumentException at com.isoftstone.mcb.HelloWorld.printThrowException(HelloWorld.java:31)