浅谈Spring(五)简单日志实例

     本文将 以一个简单的记录日志实例来继续AOP的学习,近一步加深对AOP的了解。

1、首先新建个web工程,将spring的所需的jar包加进去,再来看下接口,很简单就两个方法

public interface Print {
    public String print(String name);
    public String sleep(String name);
}
2、接下来是实现类
public class SystemPrint implements Print{
    
    public String print(String name){
        String result="hello " + name;
        System.out.println(result);
        return result;
    }
    
    public String sleep(String name){
        String result=name+" is sleep now";
        System.out.println(result);
        return result;
    }
}
3、下面是所要织入的代码,也就是我们要用来记录日志的
public class GetLog {
    public void getLog(ProceedingJoinPoint joinpoint) throws Throwable {
        String reslut = (String)joinpoint.proceed();
        //这里是记录日志的
        System.out.println("result==="+reslut);
    }
}
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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd">
    
    <!--这个bean是作为切面    -->
    <bean id="log" class="spring3aop.GetLog"></bean>

    <!--
        注意这里:expression="execution(* spring3aop.*.print*(..))" 
        括号里面第一个*号代表返回值 接下来  spring3aop.*. 是你要切入的代码的大概路径,这里为什么用大概路径来形容呢
        因为这里的意思是符合以spring3aop的路径都会作为选择的对象,也不详细介绍,查下就明白了, print*(..)是指
        方法名以print开头的都符合,括号里面的 .. 表示参数是随意的都可以。
    -->
    <aop:config>
        <aop:aspect ref="log">
            <aop:pointcut id="printMethods" expression="execution(* spring3aop.*.print*(..))"/>
            <aop:after-returning method="getLog" pointcut-ref="printMethods" returning="retVal"/>
        </aop:aspect>
    </aop:config>
    
    <aop:config>
        <aop:aspect ref="log">
            <aop:pointcut id="sleepMethods" expression="execution(* spring3aop.*.sle*(..))"/>
            <aop:after-returning method="getLog" pointcut-ref="sleepMethods" returning="retVal"/>
        </aop:aspect>
    </aop:config>
    
    <!--要织入代码的bean-->
    <bean id="print" class="spring3aop.SystemPrint"></bean>

</beans>
5、测试类:
public class Test {

    /**  
     *   @Description 方法实现功能描述  
     *   @param args
     *   void
     *   @throws  抛出异常说明
     */
    public static void main(String[] args) {
        ApplicationContext act = new ClassPathXmlApplicationContext(
        "applicationContext20.xml");
        Print t =(Print)act.getBean("print");
        t.print("ding");
        System.out.println("-----------------");
        t.sleep("laoding");

    }


}
            运行这个类,得到如下结果:
           hello ding
           hello ding
           result===hello ding
          -----------------
           laoding is sleep now
           laoding is sleep now
           result===laoding is sleep now


    这里的hello ding 打印了两次,不用担心,这是因为执行到getLog切面类的  String reslut = (String)joinpoint.proceed();这句代码的时候再执行了一次,这句代码是取回 返回结果的,可以设置个断点来测试下好了这里就输出的result就是记录的日志,当然 这里只是个很简单的实现,但是很简单的实现却很容易说清楚原理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值