当前位置: 代码迷 >> J2EE >> Spring <aop:config>疑问解决思路
  详细解决方案

Spring <aop:config>疑问解决思路

热度:483   发布时间:2016-04-22 02:12:54.0
Spring <aop:config>疑问
我想通过Spring AOP实现日志功能,及在每个类的每个方法开始调用和退出时,打印日志。
但是,我发现一个问题,同一个类中的方法A调用方法B时,确不能在方法B调用植入日志。

代码如下:
1) 需要植入日志的类 aop.AopTest
Java code
public class AopTest {    private static final Logger logger = Logger.getRootLogger();    public void methodA() {        logger.debug("AopTest.methodA()");    }        public void methodB() {        logger.debug("AopTest.methodB()");    }        public void methodAB() {        logger.debug("AopTest.methodAB()");        methodA();        methodB();    }}


2) interceptor, aop.InterceptorTest
Java code
public class InterceptorTest {    private static final Logger logger = Logger.getRootLogger();    public void startInvoke(JoinPoint joinPoint) {        String methodName = String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName()                                                                , joinPoint.getSignature().getName() );        logger.debug("startInvoke ----------------->[" + methodName + "]");    }            public void endInvoke(JoinPoint joinPoint) {        String methodName = String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName()                                                                , joinPoint.getSignature().getName() );        logger.debug("endInvoke *******************>[" + methodName + "]");    }}


3) Spring 配置文件 ApplicationContext.xml
XML code
<?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:context="http://www.springframework.org/schema/context"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/tx    http://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"              default-autowire="byName">        <!-- support spring annotation -->    <context:annotation-config/>    <bean id="aopTest" class="aop.AopTest"/>            <bean id="interceptorTest" class="aop.InterceptorTest"/>        <aop:config proxy-target-class="true">        <aop:aspect id="testAspect" ref="interceptorTest">            <aop:pointcut id="testPointcut" expression="execution(* aop..*.*(..))"  />                        <aop:before pointcut-ref="testPointcut" method="startInvoke"/>            <aop:after  pointcut-ref="testPointcut" method="endInvoke"/>        </aop:aspect>    </aop:config></beans>


4) 测试代码 main.Main
Java code
public class Main {    public static final ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");    public static void main(String[] args) {        AopTest aopTest = (AopTest)ctx.getBean("aopTest");        aopTest.methodAB();        aopTest.methodA();        aopTest.methodB();    }}
  相关解决方案