Test1Service.java
package com.aop;/** * Created by lion on 17/3/2. */public class Test1Service implements TestServiceInter, TestServiceInter2 { public void sayTest1Service() { System.out.println("this is test1 service"); } public void sayHello() { System.out.println("Hello World 1"); } public void sayHello1() { System.out.println("Hello World 2"); }}MyMethodBeforeAdvice.java
package com.aop;import org.springframework.aop.MethodBeforeAdvice;import java.lang.reflect.Method;/** * Created by lion on 17/3/2. */public class MyMethodBeforeAdvice implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o) throws Throwable { System.out.println("this is before info"); }}TestServiceInter.java
package com.aop;/** * Created by lion on 17/3/2. */public interface TestServiceInter { public void sayHello();}TestServiceInter2.java
package com.aop;/** * Created by lion on 17/3/2. */public interface TestServiceInter2 { public void sayHello1();}测试
import com.aop.Test1Service;import com.aop.TestServiceInter;import com.aop.TestServiceInter2;import org.springframework.context.applicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * Created by lion on 17/3/2. */public class TestAopController { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml"); TestServiceInter testServiceInter = (TestServiceInter) context.getBean("proxyFactoryBean"); testServiceInter.sayHello(); TestServiceInter2 testServiceInter2 = (TestServiceInter2) context.getBean("proxyFactoryBean"); testServiceInter2.sayHello1(); }}置后通知
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="test1Service" class="com.aop.Test1Service"> </bean> <!--配置前置通知--> <bean id="myMethodBeforeAdvice" class="com.aop.MyMethodBeforeAdvice" /> <!--配置置后通知--> <bean id="myAfterReturnAdvice" class="com.aop.MyAfterReturnAdvice"/> <!--配置代理对象--> <bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--代理接口集--> <property name="proxyInterfaces"> <list> <value>com.aop.TestServiceInter</value> <value>com.aop.TestServiceInter2</value> </list> </property> <!--相当于包MyMethodBeforeAdvice前置通知和代理对象关联,我们也可以把通知看出拦截器,struts2核心拦截器--> <property name="interceptorNames"> <list> <value>myMethodBeforeAdvice</value> <value>myAfterReturnAdvice</value> </list> </property> <!--配置代理对象,可以指定--> <property name="target" ref="test1Service"/> </bean></beans>MyAfterReturnAdvice.java
package com.aop;import org.springframework.aop.AfterReturningAdvice;import java.lang.reflect.Method;/** * Created by lion on 17/3/4. */public class MyAfterReturnAdvice implements AfterReturningAdvice { public void afterReturning(Object o, Method method, Object[] objects, Object o1) throws Throwable { System.out.println("置后通知..."); }}环绕通知
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="test1Service" class="com.aop.Test1Service"> </bean> <!--配置前置通知--> <!--<bean id="myMethodBeforeAdvice" class="com.aop.MyMethodBeforeAdvice" />--> <!--配置置后通知--> <!--<bean id="myAfterReturnAdvice" class="com.aop.MyAfterReturnAdvice"/>--> <!--配置环绕通知--> <bean id="myMethodInterceptor" class="com.aop.MyMethodInterceptor"/> <!--配置代理对象--> <bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"> <!--代理接口集--> <property name="proxyInterfaces"> <list> <value>com.aop.TestServiceInter</value> <value>com.aop.TestServiceInter2</value> </list> </property> <!--相当于包MyMethodBeforeAdvice前置通知和代理对象关联,我们也可以把通知看出拦截器,struts2核心拦截器--> <property name="interceptorNames"> <list> <!--<value>myMethodBeforeAdvice</value>--> <!--<value>myAfterReturnAdvice</value>--> <value>myMethodInterceptor</value> </list> </property> <!--配置代理对象,可以指定--> <property name="target" ref="test1Service"/> </bean></beans>MyMethodInterceptor.java
package com.aop;import org.aopalliance.intercept.MethodInterceptor;import org.aopalliance.intercept.MethodInvocation;/** * Created by lion on 17/3/4. */public class MyMethodInterceptor implements MethodInterceptor { public Object invoke(MethodInvocation methodInvocation) throws Throwable { System.out.println("调用环绕通知之前"); Object obj = methodInvocation.proceed(); System.out.println("调用环绕通知之后"); return obj; }}新闻热点
疑难解答