2

嗨,我无法使用 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. 方面类

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

任何建议请。

4

0 回答 0