北纬36度
北纬36度

学习、记录、分享


  • 主页

  • 分类

  • 归档

  • 留言

  • 关于我

Spring_AOP.md

发表于 2018-05-29 | 0 | 阅读次数 197

title: Spring AOP date: '2018-05-29 15:59:26' updated: '2018-05-30 00:49:26' tags: [Spring, AOP] permalink: /articles/2018/05/29/1527580766021.html

有关AOP的三个名词

| 名称 | 中文 | 解释 |
| --- | --- | --- | --- | | PointCut | 切入点 | 程序运行的某个阶段点,如某个方法调用 |
| Joint Point | 连接点 | 连接点的集合是切入点 |
| Adivice |通知|如输出日志的代码.| | Advisor |配置器|将通知注入切入点|

Spring AOP实现输出日志

采用Interceptor Around通知

接口 TimeBookInerface

package xyz.log.aop.impl;

public interface TimeBookInterface {
    /**
     *
     * 具体执行业务的代码
     */
    public void doAuditing(String name);
}

FinanceInterface

package xyz.log.aop.impl;

public interface FinanceInterface {
    public void doCheck(String name);
}

impl

package xyz.log.aop.action;

import xyz.log.aop.impl.TimeBookInterface;

public class TimeBook implements TimeBookInterface {
    @Override
    public void doAuditing(String name) {
        //审核数据相关代码
    }
}

package xyz.log.aop.action;

import xyz.log.aop.impl.FinanceInterface;

public class Finance implements FinanceInterface {
    @Override
    public void doCheck(String name) {

    }
}
package xyz.log.aop.log;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

/**
 * interceptor around通知会在joint point 前后运行
 */
public class LogAround implements MethodInterceptor {
    private Logger logger = Logger.getLogger(this.getClass().getName());

    /**
     * 负责输出日志的代码
     * @param methodInvocation 可以获得方法名称、程序传入的参数Object[]
     * @return
     * @throws Throwable
     */
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
            logger.log(Level.INFO, methodInvocation.getArguments()[0] + "开始审核数据");
        try {
            //.proceed()可以执行被调用的方法
            Object result = methodInvocation.proceed();
            //返回值是被调用方法的返回值
            return result;

        } finally {
            logger.log(Level.INFO, methodInvocation.getArguments()[0] + "审核数据结束");
        }


    }
}

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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--使用Spring aop 实现日志输出的Bean-->
    <!--注册日志bean组件-->
    <bean id="log" class="xyz.log.aop.log.LogAround"/>
    <!--审核程序-->
    <bean id="timeBook" class="xyz.log.aop.action.TimeBook"/>
    <!--使用Spring ProxyFactoryBean来实现代理-->
    <bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces" >
            <value>xyz.log.aop.impl.TimeBookInterface</value>
        </property>
        <property name="target" >
            <ref bean="timeBook"/>
        </property>
        <!--指定要代理的类-->
        <property name="interceptorNames" >
            <list>
                <value>log</value>
            </list>
        </property>
    </bean>
    <!--财务审核-->
    <bean id="finance" class="xyz.log.aop.action.Finance"/>
    <bean id="logProxy1" class="org.springframework.aop.framework.ProxyFactoryBean">
        <property name="proxyInterfaces" >
            <value>xyz.log.aop.impl.FinanceInterface</value>
        </property>
        <property name="target" >
            <ref bean="finance"/>
        </property>
        <!--指定要代理的类-->
        <property name="interceptorNames" >
            <list>
                <value>log</value>
            </list>
        </property>
    </bean>
        

</beans>

test

package xyz.log.aop.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import xyz.log.aop.impl.FinanceInterface;
import xyz.log.aop.impl.TimeBookInterface;

public class TestHelloWorld {
    public static void main(String[] args) {
        //通过application context 获取XML
        ApplicationContext actx = new ClassPathXmlApplicationContext("classpath:SpringConfig.xml");
        TimeBookInterface timeBookProxy = (TimeBookInterface) actx.getBean("logProxy");
        FinanceInterface financeProxy = (FinanceInterface) actx.getBean("logProxy1");
        timeBookProxy.doAuditing("张三");
        financeProxy.doCheck("李四");
    }
}

如果对你有帮助,请点击打赏谢谢。
Quinn Tian 微信支付

微信支付

Quinn Tian 支付宝

支付宝

  • 本文作者: Quinn Tian
  • 本文链接: https://quinntian.com/?p=7
  • 版权声明: 本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0 许可协议。转载请注明出处!
Java的代理机制实现日志输出.md
资源分享_Spring4视频教程_.md
  • 文章目录
  • 站点概览
Quinn Tian

Quinn Tian

分享、记录、学习

70 日志
18 分类
97 标签
RSS
Github E-mail
Creative Commons
友情链接
  • 寒山志
  • 凉风有信
  • 阿业战记
  • 团子の小屋
  • 站长博客大全
  • 浮生博客
  • 旅行猫
  • 长野 の BLOG
  • 今今今生
  • 阿殇的博客
  • 开心的DCMickey
  • 无名域
  • 一信笔记
  • LOGI
  • zrxJuly's Blog
  • 栗先森
  • bestsort
  • 家用蒸烤箱
  • 小游
  • Aiden's Blog
  • 牛牛游戏网
  • DYBON
  • FGHRSH 的博客
  • geticsen
  • VVavE
  • 菠萝猫
  • 中国博客联盟
  • iBlog@zihua
  • 卢松松博客
  • 平凡的你我
  • 宇宙湾
  • 村头老杨头
  • 冰洛博客
  • 稻田守望者
  • 爱猫说设计狮网
  • 小维博客
  • 小浣熊的技术小站
© 2018 — 2021 Quinn Tian
由 Java 强力驱动
|
主题 - NexT.Muse-移植版
鲁ICP备1801591号-1
0%