基本代理模式 纯POJO切面
源码结构:
1、首先我们新建一个接口,love 谈恋爱接口。
package com.spring.aop;
/**
* 谈恋爱接口
*
* @author Administrator
*
*/
public interface Love
{
/*
* 谈恋爱方法
*/
void fallInLove();
}
2、我们写一个Person类实现Love接口
package com.spring.aop;
/**
* 人对象
* @author luwenbin006@163.com
*
*/
public class Person implements Love
{
/*
* 重写谈恋爱方法
* @see com.spring.aop.Love#fallInLove()
*/
public void fallInLove()
{
System.out.println("谈恋爱了...");
}
}
3、下面我们来写aop通知类
package com.spring.aop;
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
/**
* aop通知类
* @author luwenbin006@163.com
*
*/
public class LoveHelper implements MethodBeforeAdvice, AfterReturningAdvice
{
//调用方法之前执行
public void before(Method mtd, Object[] arg1, Object arg2) throws Throwable
{
System.out.println("谈恋爱之前必须要彼此了解!");
}
//调用方法之后执行
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable
{
System.out.println("我们已经谈了5年了,最终还是分手了!");
// System.out.println("我们已经谈了5年了,最终步入了结婚的殿堂!");
}
}
4、配置好application.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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 配置person对象 -->
<bean id="person" class="com.spring.aop.Person">
</bean>
<!-- 配置通知方法类 -->
<bean id="loveHelper" class="com.spring.aop.LoveHelper">
</bean>
<!-- 配置接口方法通知 也就是符合哪个条件的方法才进行通知 -->
<bean id="lovePointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*fallInLove" />
</bean>
<!-- 声明advisor 指定接口和通知类 -->
<bean id="loveHelperAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="loveHelper" />
<property name="pointcut" ref="lovePointcut" />
</bean>
<!-- 配置代理工厂 -->
<bean id="loveProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="person" />
<property name="interceptorNames" value="loveHelperAdvisor" />
<property name="proxyInterfaces" value="com.spring.aop.Love" />
</bean>
</beans>
5、写上我们的测试类 测试下效果 嘿嘿~~~
package com.spring.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.aop.Love;
/**
* aop测试方法类
* @author luwenbin006@163.com
*
*/
public class LoveTest
{
public static void main(String[] args)
{
//加载spring配置文件
ApplicationContext appCtx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
//得到loveProxy对象
Love love = (Love) appCtx.getBean("loveProxy");
//调用谈恋爱方法
love.fallInLove();
}
}
6、看控制台输出结果 调用了我们定义的aop拦截方法~~~ ok了
7、源码下载地址:源码下载