当前位置: 移动技术网 > IT编程>开发语言>Java > Spring AOP前置通知实例说明AOP相关概念

Spring AOP前置通知实例说明AOP相关概念

2018年12月26日  | 移动技术网IT编程  | 我要评论

今天又看了下韩顺平的springaop的讲解,讲解的很透彻。仿照视频自己使用下前置通知。

一、引出问题

  有个接口testserviceinter,有两个实现方法testservice和test2service。他们都有sayhello();我们的需求是在调用这两个方法之前,要先完成写日志的功能;

二、菜鸟的想法

  我在各个实现类的sayhello()方法里面写上写日志的功能就是了。

  这样实现存在的问题:代码冗余。当所有实现类都要加上日志功能的时候,需要写很多重复代码

三、利用aop前置通知实现此功能

3.1 第一步:我们需要定义一个接口  

package com.jdc.aop;

/**
 * @author dellcomputer
 * @title: testservice
 * @projectname springaop
 * @description:
 * @date 2018/12/251:38 pm
 */
public interface testservice {

    /**
      * @description:
      * @param ${tags}
      * @return ${return_type}
      * @throws
      * @author jdc
      * @date 2018/12/25 1:39 pm
      */
    void sayhi(string name);

}

3.2 第二步:实现接口

public class testserviceimpl implements testservice {

    private string name;

    /**
      * @description: say hi
      * @param ${tags}
      * @return ${return_type}
      * @throws
      * @author jdc
      * @date 2018/12/25 1:40 pm
      */
    @override
    public void sayhi(string name ) {
        system.out.println("hi:" + name);
    }

    public string getname() {
        return name;
    }

    public void setname(string name) {
        this.name = name;
    }
}

3.3 第三步:实现前置通知(aop中通知的概念就是实现增强代码逻辑的,比如这里的记录日志)

/**
 * @author dellcomputer
 * @title: mybeforeadvice
 * @projectname springaop
 * @description: 写日志前置通知
 * @date 2018/12/251:42 pm
 */
public class mybeforeadvice implements methodbeforeadvice {
   
    /**
      * @description: 写日志的功能
      * @param ${tags} 
      * @return ${return_type} 
      * @throws
      * @author jdc
      * @date 2018/12/25 1:44 pm 
      */
    @override
    public void before(method method, object[] objects, object o) throws throwable {
        system.out.println("我是写日志的功能。");
    }
}

前置通知需要实现methodbeforeadvice接口,前置通知是在目标方法调用之前调用;

3.4 第四步:在spring的配置文件中配置代理对象,通知,目标对象

<?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="mybeforeadvice" class="com.jdc.aop.advice.mybeforeadvice"/>

    <!-- 配置被代理对象 -->
    <bean id="logtestserviceimpl" class="com.jdc.aop.testserviceimpl">
        <property name="name" value="test"></property>
    </bean>

    <!-- 配置代理对象 -->
    <bean id="proxyfactorybean"   class="org.springframework.aop.framework.proxyfactorybean">
        <!-- 代理接口集 配置哪些接口要被代理 -->
        <property name="proxyinterfaces">
            <list>
                <value>com.jdc.aop.testservice</value>
            </list>
        </property>

        <!-- 把通知织入代理对象 -->
        <property name="interceptornames">
            <value>mybeforeadvice</value>
        </property>

        <!-- 配置被代理的对象 -->
        <property name="target" ref="logtestserviceimpl"/>


    </bean>


</beans>
proxyfactorybean是一个代理对象,如果我们被代理的对象实现了接口,spring使用的是jdk动态代理技术实现的动态代理;所以我们要告诉代理对象,我们的哪些接口需要被代理,然后哪个对象需要被代理,我的增强实现应该怎么被织入到代理对象(前置,后置,环绕....)

3.5 第五步:写测试代码测试

public class testmain {

    public static void main(string[] args) {
        applicationcontext ac = new classpathxmlapplicationcontext("beans.xml");
        //获取代理对象
        testservice log = (testservice) ac.getbean("proxyfactorybean");
        log.sayhi("哈哈");

    }
}

注意这里,我们需要获取代理对象,而不是目标对象,不然不能调用前置通知的代码。

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网