ProxyInter借口:
package com.aspect;
public interface ProxyInter {
//租房
public void rent();
//退房
public void cancelHouse();
}
Owner类实现ProxyInter接口
package com.aspect;
public class Owner implements ProxyInter {
public void rent() {
System.out.println("租房");
}
public void cancelHouse()
{
System.out.println("退房");
}
}
代理类Proxy也实现ProxyInter接口
package com.aspect;
public class Proxy implements ProxyInter {
public void rent() {
ProxyInter proxy = new Owner();
proxy.rent();
}
public void cancelHouse()
{
ProxyInter proxy = new Owner();
proxy.cancelHouse();
}
}
客户Client
package com.aspect;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Client {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//给ProxyInter接口的所有方法+通知
ProxyInter proxy = (ProxyInter)context.getBean("proxyFactoryBean");
//只给rent方法+通知
// ProxyInter proxy = (ProxyInter)context.getBean("owner");
proxy.rent();
// proxy.cancelHouse();
}
}
环绕通知SurroundAdvice
package com.aspect;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
public class SurroundAdvice implements MethodInterceptor{
public Object invoke(MethodInvocation invocation) throws Throwable {
System.out.println("欢迎您,请喝茶");
Object object = invocation.proceed();
System.out.println("加钱");
return object;
}
}
前置通知BeforeAdvice
package com.aspect;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvice implements MethodBeforeAdvice{
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("租房之前");
}
}
后置通知AfterAdvice
package com.aspect;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
public class AfterAdvice implements AfterReturningAdvice{
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("租房之后");
}
}
web.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLoaction</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
applicationContext.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="surroundAdvice" class="com.aspect.SurroundAdvice"/>
<bean id="beforeAdvice" class="com.aspect.BeforeAdvice"/>
<bean id="afterAdvice" class="com.aspect.AfterAdvice"/>
<bean id="owner" class="com.aspect.Owner"/>
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="com.aspect.ProxyInter"/>
<property name="target" ref="owner"/>
<property name="interceptorNames">
<list>
<value>surroundAdvice</value>
<value>beforeAdvice</value>
<value>afterAdvice</value>
</list>
</property>
</bean>
</beans>
在Client中
ProxyInter proxy = (ProxyInter)context.getBean("proxyFactoryBean");得到代理对象执行
结果如下:
欢迎您,请喝茶
租房之前
租房
租房之后
加钱
如果想要只给接口的rent方法加环绕通知,如下操作:
修改applicationContext.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="surroundAdvice" class="com.aspect.SurroundAdvice"/>
<bean id="beforeAdvice" class="com.aspect.BeforeAdvice"/>
<bean id="afterAdvice" class="com.aspect.AfterAdvice"/>
<bean id="owner" class="com.aspect.Owner"/>
<aop:config>
<aop:pointcut id="serviceMethod"
expression="execution(* com.aspect.ProxyInter.rent(..))"/>
<aop:advisor advice-ref="surroundAdvice" pointcut-ref="serviceMethod"/>
</aop:config>
</beans>
在Client中
ProxyInter proxy = (ProxyInter)context.getBean("owner");得到代理对象执行即可
结果如下在Client中
ProxyInter proxy = (ProxyInter)context.getBean("proxyFactoryBean");得到代理对象执行
结果如下:
欢迎您,请喝茶
租房
加钱
而执行proxy.cancelHouse()方法则没有环绕通知
上面这种配置需要导入的4个jar包如下:
aspectjrt.jar
aspectjweaver.jar
commons-logging-1.0.4.jar
spring.jar