spring aop的两种写法aspect和advisor

时间:2023-03-08 17:00:43

本文转自:https://www.cnblogs.com/leiOOlei/p/3709607.html

首先看个例子,如下

接口代码:

package com.lei.demo.aop.schema;

public interface IHello {
public void sayHello();
}

接口实现:

package com.lei.demo.aop.schema;

public class HelloService implements IHello {

    public void sayHello() {
System.out.println("-----Hello World!-----");
} }

接下来我们要实现AOP,即调用sayHello方法时切入通知。

1.      第一种方法aop:config中配置aop:pointcut和aop:aspect

定义一个切面支持类HelloAspect.java

package com.lei.demo.aop.schema;

/*
* 切面支持类
*/
public class HelloAspect { //前置通知
public void beforeAdvice() {
System.out.println("===========before advice");
} //后置最终通知
public void afterFinallyAdvice() {
System.out.println("===========after finally advice");
}
}

 

Xml配置:Spring-AOP-Schema.xml

<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <!-- 目标类 -->
<bean id="helloService" class="com.lei.demo.aop.schema.HelloService" /> <bean id="helloAspect" class="com.lei.demo.aop.schema.HelloAspect" /> <!-- 配置切面 -->
<!-- aop:advisor,是有顺序的,必须放在aop:pointcut之后 -->
<aop:config>
<aop:pointcut id="helloPointcut" expression="execution(* com.lei.demo.aop.schema..*.*(..))" /> <aop:aspect ref="helloAspect">
<!—以下使用了两种方法定义切入点 pointcut-ref和pointcut-->
<aop:before pointcut-ref="helloPointcut" method="beforeAdvice" />
<aop:after pointcut="execution(* com.lei.demo.aop.schema..*.*(..))" method="afterFinallyAdvice" />
</aop:aspect> </aop:config>
</beans>

以上配置中method="beforeAdvice"method="afterFinallyAdvice"对应helloAspect中的方法

测试,App.java

package com.lei.demo.aop.schema;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { private static ApplicationContext ctx; public static void main(String[] args) { ctx = new ClassPathXmlApplicationContext("Spring-AOP-Schema.xml"); IHello helloService = ctx.getBean("helloService",IHello.class); helloService.sayHello();
} }

运行App.java,结果如下:

===========before advice

-----Hello World!-----

===========after finally advice

2.      第二种方法aop:config中配置aop:pointcut和aop:advisor

实现org.aopalliance.intercept.MethodInvocation接口,

HelloAroundAdvice.java如下:

package com.lei.demo.aop.schema;

import java.lang.reflect.Method;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; public class HelloAroundAdvice implements MethodInterceptor { public Object invoke(MethodInvocation arg0) throws Throwable { System.out.println("++++++before advice");
arg0.proceed();
System.out.println("++++++after advice"); return null;
} }

Xml配置:Spring-AOP-Schema.xml

<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <!-- 目标类 -->
<bean id="helloService" class="com.lei.demo.aop.schema.HelloService" /> <bean id="helloArroundAdvice" class="com.lei.demo.aop.schema.HelloAroundAdvice" /> <!-- 配置切面 -->
<aop:config>
<aop:pointcut id="helloPointcut" expression="execution(* com.lei.demo.aop.schema..*.*(..))" />
<aop:advisor advice-ref="helloArroundAdvice" pointcut-ref="helloPointcut"/> </aop:config>
</beans>

仍然运行App.java,结果如下:

++++++before advice

-----Hello World!-----

++++++after advice