spring 动态代理实现打印日志功能

1.applicationContext.xml中配置切入点和切面

<!-- 定义AOP切面处理器 -->
  <bean id="dataSourceAspect"  class="controler.ServiceAspect"/>
  <!-- 如果设置为true默认使用CGLB加载 pom.xml中需引入cglib的包-->
  <!--<aop:aspectj-autoproxy proxy-target-class="true"/>-->
  <aop:config>
    <!--pointcut元素定义一个切入点,execution中的第一个星号 用以匹配方法的返回类型, 这里星号表明匹配所有返回类型。
    com.lxg.service.*.*(..)表明匹配com.lxg.service包下的所有类的所有
        方法 -->
    <aop:pointcut id="myPointcut" expression="execution(* service.impl.*.*(..))" />

    <!-- 将切面应用到自定义的切面处理器上,-9999保证该切面优先级最高执行 -->
    <aop:aspect ref="dataSourceAspect" order="-9999">
      <aop:before method="before" pointcut-ref="myPointcut" />
    </aop:aspect>

  </aop:config>

2.新建serviceAspect类

public class ServiceAspect {

    public void before(JoinPoint point) {
        // 获取到当前执行的方法名
        String methodName = point.getSignature().getName();
        System.out.println("<<<<<<<<<<<<<<<service method>>>>>>>>>>>>>>:" + methodName);
    }

}

猜你喜欢

转载自blog.csdn.net/liushuiziyouliu/article/details/79807204
今日推荐