自定义拦截器
自定义的拦截器类需要实现三个接口 AfterReturningAdvice,MethodBeforeAdvice,ThrowsAdvice
在自定义拦截器中需要重写AfterReturningAdvice的afterReturning(Object value,Method method,Object[] args,Object instance)方法。
此方法是在完成指定方法后调用,四个参数分别表示被拦截方法的返回值,被调用的方法,执行时被传入的参数,被拦截的bean。
需要重写MehtodBeforeAdvice的before(Method method,Object[] orgs,Object instance)方法。
三个参数分别表示 被调用的方法,传入的参数,被拦截的bean
然后需要把自定义的拦截器和被拦截的方法分别注入到NameMatchMethodPointcutAdvisor类的advice和mappedName属性中,再把NameMathMethodPointcutAdvisor类和需要被拦截的类分别注入到ProxyFactoryBean中interceptorName和target属性中
附上相关代码
package basicAop;
public interface IAOPServices {
public String withAopMethod() throws Exception;
public String withNoAopMethod() throws Exception;
}
package basicAop;
public class AOPServices implements IAOPServices{
private String description;
public void setDescription(String des) {
this.description=des;
}
public String getDescription() {
return this.description;
}
public String withAopMethod() throws Exception {
System.out.println("有withAOPMethod");
if(description.trim().length()==0) {
throw new Exception("属性为空");
}
return description;
}
public String withNoAopMethod() throws Exception {
System.out.println("无withNoAopMethod");
return description;
}
}
package basicAop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
public class AOPInterceptor implements AfterReturningAdvice,MethodBeforeAdvice,ThrowsAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("即将执行:"+method.getName());
if(target instanceof AOPServices) {
String desc=((AOPServices)target).getDescription();
if(desc==null) {
throw new NullPointerException("属性值为null");
}
}
}
public void afterReturning(Object value, Method method, Object[] args, Object target) throws Throwable {
System.out.println(method.getName()+"end:value:"+value);
}
public void afterThrowing(Exception ex) {
System.out.println(ex.getMessage());
}
public void afterThrowing(Method method,Object[] args,Object target,Exception ex) {
System.out.println(method.getName()+"异常:"+ex.getMessage());
}
}
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:context="http://www.springframework.org/schema/context"
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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"
>
<context:component-scan base-package="AOP"/>
<context:component-scan base-package="spring"/>
<context:component-scan base-package="basicAop"/>
<bean id="aopInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice">
<bean class="basicAop.AOPInterceptor"/>
</property>
<property name="mappedName" value="withNoAopMethod"/>
</bean>
<bean id="aopService" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="interceptorNames">
<list>
<value>aopInterceptor</value>
</list>
</property>
<property name="target">
<bean class="basicAop.AOPServices">
<property name="description" value="basicAop"></property>
</bean>
</property>
</bean>
<bean id="CleanAir" class="spring.CleanAir">
<qualifier value="cleanair"/>
</bean>
<bean id="DirtyAir" class="spring.DirtyAir">
<qualifier value="dirtyair"/>
</bean>
<bean id="AnnotationInstance" class="AOP.AnnotationInstance"></bean>
<bean id="person" class="spring.Person"></bean>
</beans>
需要在pom文件中引入spring-aop依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring</groupId>
<artifactId>spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo1</name>
<properties>
<maven.compiler.encoding>UTF-8</maven.compiler.encoding>
<java.version>1.8</java.version>
<spring.version>5.0.0.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>