基于Schema-based方式实现
前置通知
创建一个接口
package com.zhouym.schema_based;
public interface ServiceBean {
String doSome();
String say();
}
创建目标实现类
package com.zhouym.schema_based;
public class ServiceBeanImpl implements ServiceBean {
@Override
public String doSome() {
System.out.println("doSome方法");
return "hello";
}
@Override
public String say() {
System.out.println("say方法");
return null;
}
}
创建切面类
package com.zhouym.schema_based;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
//定义一个切面类实现前置通知类
public class MyMethodBeforeAdvice implements MethodBeforeAdvice{
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("前置通知方法!!!");
}
}
xml文件配置,需导入aop的jar包
<?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:p="http://www.springframework.org/schema/p"
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-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
<!-- 注册目标类 -->
<bean class="com.zhouym.schema_based.ServiceBeanImpl" id="serviceBeanImpl"></bean>
<!-- 注册前置通知 -->
<bean class="com.zhouym.schema_based.MyMethodBeforeAdvice" id="myMethodBeforeAdvice"></bean>
<!-- 注册代理类 这里引入的classpath是ProxyFactoryBean,不要引入ProxyFactory,否则会create error bean -->
<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="proxyFactoryBean">
<!-- 指定目标对象 -->
<property name="target" ref="serviceBeanImpl"></property>
<!-- 指定目标类实现的的所有接口 -->
<property name="interfaces" value="com.zhouym.schema_based.ServiceBean"></property>
<!-- 指定切面 -->
<property name="interceptorNames">
<list>
<!-- 对应前置通知的id属性值 -->
<value>myMethodBeforeAdvice</value>
</list>
</property>
</bean>
</beans>
测试类
package com.zhouym.JunitTest;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zhouym.schema_based.ServiceBean;
public class Schema_Based_Test {
@Test
public void test() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
//指定代理类的id以及接口类对象
ServiceBean sb = ac.getBean("proxyFactoryBean",ServiceBean.class);
sb.doSome();
sb.say();
}
}
测试结果
后置通知
接口,以及实现类跟前置通知的一样,后置通知我们需要实现AfterReturningAdvice接口
package com.zhouym.schema_based;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class MyAfterReturningAdvice implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("后置通知方法");
}
}
xml配置文件
测试类
测试结果
环绕通知
接口和实现类跟上面的一样,实现MethodReturningAdvice接口
package com.zhouym.schema_based;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class