无法使用Spring LoadTimeWeaving将建议应用于第三方库(log4g - Logger.getLogger调用)?

时间:2013-11-08 10:52:50

标签: java spring aop

您是否无法使用Spring LoadTimeWeaving向第三方库(log4g-Logger.getLogger调用)提供建议?

1。 SpringConfig.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" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- Spring Application Context holder that is used by code that can not be injected by spring.
    One example is the log4j appender called LogEventAppender.
     -->
     <!-- <context:property-placeholder location="classpath:/hello_${env}.properties"/> -->

    <context:load-time-weaver/>


    <context:spring-configured/>

    <context:component-scan base-package="com.app.svc">
        <!-- In the case the IFA is in the same class path that this spring context is being loaded from exclude this
        generator from including IPA classes. This happens in the fusion-util module. -->
    </context:component-scan>

    <aop:aspectj-autoproxy/>

</beans>

2。 META-INF目录中的aop.xml。

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>

    <weaver>
        <!-- only weave classes in our application-specific packages -->
        <include within="com.app.svc.*" />
        <include within="org.apache.log4j.*" />
    </weaver>

    <aspects>
        <!-- weave in just this aspect -->
        <aspect name="com.app.svc.MyLoggingAspect" />
    </aspects>

</aspectj>

第3。 Aspect类

package com.app.svc;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class MyLoggingAspect {

    @Around("getLoggerPointCut()")
    public Object changeFileAppender(ProceedingJoinPoint pjp) throws Throwable {
        System.err.println("---------------- MyLoggingAspect ##--------------------");
        Object proceed = pjp.proceed();
        return proceed;
    }

    //This is not working - Pointcuts for classes in 3rd party jar. 
    @Pointcut("execution(public org.apache.log4j.Logger org.apache.log4j.Logger.*(..))")
    public void getLoggerPointCut(){}   

    //This works - Pointcuts for classes in application.
    //@Pointcut("execution(public * com.app.svc.NewHello.info(..))")
    //public void getLoggerPointCut(){} 

}

注意:提供以下java VM参数。 -javaagent:D:\devel\spring\spring-instrument-3.2.3.RELEASE.jar

它不是在控制台上打印任何东西。如果我将切入点定义更改为我的应用程序中的某个包,那么它可以正常工作。

所以看起来它无法将方面应用于Log4j.xml

任何建议PLZ。

0 个答案:

没有答案