分享

Spring AOP实现日志输出

 樱花梦_张艺馨 2018-12-10

================接口类TimeBookInterface ==============

package com.gc.action.springaop;

/**

 * 接口类

 * @author yltd

 *

 */

public interface TimeBookInterface {

    public void doSameing(String name);

}

==============接口实现类TimeBookImpl ======================

package com.gc.action.springaop;

/**

 * 接口实现类

 * @author yltd

 *

 */

public class TimeBookImpl implements TimeBookInterface {

    @Override

    public void doSameing(String name) {

       System.out.println(name);

    }

}

===============日志输出类LogAround ==================

package com.gc.action.springaop;

import org.aopalliance.intercept.MethodInterceptor;

import org.aopalliance.intercept.MethodInvocation;

import org.apache.log4j.Level;

import org.apache.log4j.Logger;

/**

 * AOP实现输出日志

 * @author yltd

 *

 */

public class LogAround implements MethodInterceptor {

    Logger logger =Logger.getLogger(this.getClass().getName());

    @Override

    public Object invoke(MethodInvocation arg) throws Throwable {

       logger.log(Level.INFO, "开始。。。");

       Object obj = arg.proceed();

      

       logger.log(Level.INFO, "结束。。。");

       return obj;

    }

}

=============XML配置文件config.xml==================

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

 "http://www./dtd/spring-beans.dtd">

 <beans>

 <!-- 负责国际化支持 -->

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">

    <!-- 国际化支持的定义在文件名为messages的文件中 -->

    <property name="basename">

       <value>messages</value>

    </property>

</bean>

    <bean id="log" class="com.gc.action.springaop.LogAround"></bean>

    <bean id="timeBook" class="com.gc.action.springaop.TimeBookImpl"></bean>

    <bean id="logProxy" class="org.springframework.aop.framework.ProxyFactoryBean">

       <property name="proxyInterfaces">

           <value>com.gc.action.springaop.TimeBookInterface</value>

       </property>

       <property name="target">

           <ref bean="timeBook"/>

       </property>

       <!-- 指定要代理的类 -->

       <property name="interceptorNames">

           <list>

              <value>log</value>

           </list>

       </property>

    </bean>

 </beans>

====================测试类TestMain ================

package com.gc.action.springaop;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

/**

 * 测试类

 * @author yltd

 *

 */

public class TestMain {

    public static void main(String[] args) {

       ApplicationContext ac =new FileSystemXmlApplicationContext("classpath:/SpringConfig.xml");

       TimeBookInterface tbi=(TimeBookInterface) ac.getBean("logProxy");

       tbi.doSameing("张艺馨");

    }

}

====================结果======================

- Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@4e515669: startup date [Fri Oct 19 16:13:27 CST 2018]; root of context hierarchy

- Loading XML bean definitions from class path resource [SpringConfig.xml]

- 开始。。。

张艺馨

- 结束。。。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多