Spring AOP实现方式二【附源码】

自动代理模式【和我们说的方式一 配置 和 测试调用不一样哦~~~】  纯POJO切面

源码结构:

image

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   主要是 自动工厂模式 配置不一样哦~~~~ 注意

这里还把之前的 advisor改成了 RegexpMethodPointcutAdvisor 哦~~~

直接进行了匹配,之前的pointcut配置去掉了哦~

<?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">


    <!--  配置bean -->
    <bean id="person" class="com.spring.aop.Person">
    </bean>

    <!-- 配置通知方法类 -->
    <bean id="loveHelper" class="com.spring.aop.LoveHelper">
    </bean>
    
    <!-- 声明advisor 指定哪些方法通知哪个类 -->
    <bean id="loveHelperAdvisor"
        class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
        <property name="advice" ref="loveHelper" />
        <property name="pattern" value=".*fallInLove" />
    </bean>

    <!-- 自动代理创建模式 -->
    <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" />

</beans>

5、写上我们的测试类 测试下效果 这里是person哦 注意~~~ 嘿嘿~~~

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");
        //得到person对象
        Love love = (Love) appCtx.getBean("person");
        //调用谈恋爱方法
        love.fallInLove();
    }
}

6、看控制台输出结果 调用了我们定义的aop拦截方法~~~ ok了

image_thumb[3]

7、源码下载地址:源码下载

 
 
 
 
原文地址:https://www.cnblogs.com/luwenbin/p/4438347.html