一、 XML风格 = 采用声明形式实现Spring AOP
前置通知:------org.springframework.aop.MethodBeforeAdvice
后置通知:-------org.springframework.aop.AfterReturningAdvice
环绕通知:-------org.aopalliance.intercept.MethodInterceptor
异常通知:-------org.springframework.aop.ThrowsAdvice
底层用的是反射:
说明:四选一
* <pre class="code">public void afterThrowing(Exception ex)</pre>
* <pre class="code">public void afterThrowing(RemoteException)</pre>
* <pre class="code">public void afterThrowing(Method method, Object[] args, Object target, Exception ex)</pre>
* <pre class="code">public void afterThrowing(Method method, Object[] args, Object target, ServletException ex)</pre>
配置方式:
在applicationContext.xml中配置:
<!--配置目标对象 -->
<bean id="factory" class="com.neuedu.demo.Factory"></bean>
<!--配置增强对象 -->
<bean id="before" class="com.neuedu.demo.Before"></bean>
<bean id="after" class="com.neuedu.demo.After"></bean>
<bean id="around" class="com.neuedu.demo.Around"></bean>
<bean id="throws" class="com.neuedu.demo.Throws"></bean>
<!--配置代理对象 -->
<bean id="proxyFactory" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 植入目标对象 -->
<property name="target" ref="factory"></property>
<!--植入增强对象的名字 -->
<property name="interceptorNames" value="before,after,around,throws"></property>
<!-- 如果是面向接口需要配置目标对象的接口 -->
<property name="proxyInterfaces" value="com.neuedu.demo.Ifactory"></property>
<!--如果不是面向接口开发需要配置optimize -->
<!-- <property name="optimize" value="true"></property> -->
</bean>
首先,导入jar包
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
1.创建factory实体类
public class Factory implements Ifactory{
@Override
public void productA() {
int k=10/0;
System.out.println("生产A");
}
@Override
public void procuctB() {
System.out.println("生产B");
}
@Override
public void procuctC() {
System.out.println("生产C");
}
}
2.创建接口类Ifactory
package com.neuedu.demo;
public interface Ifactory {
public void productA();
public void procuctB();
public void procuctC();
}
3.创建代理对象
package com.neuedu.demo;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class ProxyFactory {
@Before("execution(* productA(..))")//*代表所有返回值类型(..)代表所有的参数类型
public void before() {
System.out.println("前置通知");
}
@AfterReturning("execution(* productA(..))")
public void after() {
System.out.println("后置通知");
}
@Around("execution(* productA(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("环绕前");
joinPoint.proceed();
System.out.println("环绕后");
}
}
注意:
@AfterReturning("execution(* productA(..))"):只有在成功返回之后才会执行后置重置
@After("execution(* productA(..))"):无论是否成功返回都会执行后置通知
4.在applicationContext.xml中配置
<!--配置自动代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--配置目标对象 -->
<bean id="factory" class="com.neuedu.demo.Factory"></bean>
<!--配置代理对象 -->
<bean id="proxyFactory" class="com.neuedu.demo.ProxyFactory"></bean>