版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/AustinBoris/article/details/56289833
19、spring AOP+AspectJ
本文主要介绍AspectJ注解和spring AOP的结合
AspectJ主要有以下五种方式:
- @Before——方法运行前执行
- @After——方法运行后执行
- @AfterReturning——方法返回value后执行
- @AfterThrowing——抛出异常后执行
- @Around——环绕型,结合前面四种
说明:spring中并没有继承Aspect的jar包
需要导入两个jar包:
- aspectjwear.jar
- aspectjrt.jar
maven依赖如下:
<!-- aspectjrt -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<!-- aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.5.4</version>
</dependency>
例子
目录结构如下:
第一步:添加类代码:
HelloWorld.java
public interface HelloWorld {
public void printHelloWorld();
public String printAndReturn();
public void throwException() throws Exception;
}
HelloWorldImpl.java
public class HelloWorldImpl implements HelloWorld{
public void printHelloWorld() {
System.out.println("printHelloWorld方法执行了");
}
public String printAndReturn() {
System.out.println("printAndReturn方法执行了");
return "你好啊";
}
public void throwException() throws Exception{
System.out.println("throwException抛出异常了");
throw new Exception("Generic Error");
}
}
AspectJService.java
@Aspect
public class AspectJService {
@Before("execution(* com.main.AOP.AspectJ.HelloWorld.printHelloWorld(..))")
public void printBefore(){
System.out.println("AspectJService : before");
}
@After("execution(* com.main.AOP.AspectJ.HelloWorld.printHelloWorld(..))")
public void printAfter(){
System.out.println("AspectJService : after");
}
@AfterReturning(pointcut = "execution(* com.main.AOP.AspectJ.HelloWorld.printAndReturn(..))",
returning= "result")
public void printAfterReturning(String result){
//获取返回值
System.out.println(result);
System.out.println("AspectJService : AfterReturning");
}
@AfterThrowing(
pointcut = "execution(* com.main.AOP.AspectJ.HelloWorld.throwException(..))",
throwing= "error")
public void printAfterThrowing(Throwable error){
System.out.println("Exception : " + error);
System.out.println("AspectJService : printAfterThrowing");
}
}
第二步:配置Aspect
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<aop:aspectj-autoproxy />
<bean id="helloWorld" class="com.main.AOP.AspectJ.HelloWorldImpl"/>
<!-- AspectJService -->
<bean id="aspectJService" class="com.main.AOP.AspectJ.AspectJService" />
</beans>
测试运行:
@Test
public void test(){
ApplicationContext context =
new ClassPathXmlApplicationContext("com/main/AOP/AspectJ/bean.xml");
HelloWorld helloworld = (HelloWorld)context.getBean("helloWorld");
helloworld.printHelloWorld();
System.out.println("----------------------------------");
helloworld.printAndReturn();
System.out.println("----------------------------------");
try {
helloworld.throwException();
} catch (Exception e) {
e.printStackTrace();
}
}
结果:
同样的,可以用xml的形式来配置
修改你的bean.xml配置文件如下
<!-- Aspect -->
<bean id="logAspect" class="com.main.AOP.AspectJ.AspectJService" />
<aop:config>
<aop:aspect id="aspectLoggging" ref="logAspect" >
<!-- @Before -->
<aop:pointcut id="pointCutBefore"
expression="execution(* com.main.AOP.AspectJ.HelloWorld.printHelloWorld(..))" />
<aop:before method="printBefore" pointcut-ref="pointCutBefore" />
<!-- @Before -->
<!-- @After -->
<aop:pointcut id="pointCutAfter"
expression="execution(* com.main.AOP.AspectJ.HelloWorld.printHelloWorld(..))" />
<aop:after method="printAfter" pointcut-ref="pointCutAfter" />
<!-- @After -->
` <!-- @AfterReturning -->
<aop:pointcut id="pointCutAfterReturning"
expression="execution(* com.main.AOP.AspectJ.HelloWorld.printAndReturn(..))" />
<aop:after-returning method="printAfterReturning" returning="result"
pointcut-ref="pointCutAfterReturning" />
<!-- @AfterReturning -->
<!-- @AfterThrowing -->
<aop:pointcut id="pointCutAfterThrowing"
expression="execution(* com.main.AOP.AspectJ.HelloWorld.throwException(..))" />
<aop:after-throwing method="printAfterThrowing" throwing="error"
pointcut-ref="pointCutAfterThrowing" />
</aop:aspect>
</aop:config>
这时你的@注解可以统统删掉了,测试方法一样不变