当在另一个项目中用作已编译的jar时,Spring AOP无法正常工作 [英] Spring AOP not working when using as a compiled jar in a different project

查看:51
本文介绍了当在另一个项目中用作已编译的jar时,Spring AOP无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有效的AOP(在项目内部使用时被写入),但是当我构建该项目(maven安装),并在另一个项目中使用该JAR,并尝试使用@TimedLog批注时,什么也没发生.我试图在其中断点,但是它没有到​​达那里.

I have a working AOP (when using inside the project it is written in) but when I build this project (maven install), and use that JAR in another project, and try to use the @TimedLog annotation, nothing happens. I try to breakpoint into it, but it doesn't reach there.

它看起来像这样:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface TimedLog {
    boolean shouldAttachMethodArgs() default false;
    boolean shouldAttachReturnValue() default false;
    String message() default "";
}

这是实际的方面:

@Aspect
@Configuration
@Slf4j
public class MethodExecutionAspect {

    @Pointcut("@annotation(timedLogVar)")
    public void annotationPointCutDefinition(TimedLog timedLogVar) {}

    @Pointcut("execution(* *(..))")
    public void atExecution() {}

    @Around(value = "annotationPointCutDefinition(timedLogVar) && atExecution()", argNames = "joinPoint,timedLogVar")
    public Object around(ProceedingJoinPoint joinPoint, TimedLog timedLogVar) throws Throwable {
        Stopwatch stopwatch = Stopwatch.createStarted();
        Object returnValue = joinPoint.proceed();
        stopwatch.stop();

        log.info(String.format("test message %s", stopwatch.elapsed(TimeUnit.MILLISECONDS)));

        return returnValue;
    }
}

它的实现是:

@TimedLog
void testDefaultValues() throws InterruptedException {
    int sleepTimeInMillis = 200;
    log.info("Resting for {} millis", value("sleepTimeInMillis", sleepTimeInMillis));
    Thread.sleep(sleepTimeInMillis);
}

pom.xml

<!-- AOP -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.0.2.RELEASE</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.13</version>
    <scope>compile</scope>
</dependency>

从这里您可以看到,这是一个装饰方法并记录其运行时的AOP.

From what you can see here, this is an AOP that decorates a method and logs its runtime.

我已经为此苦苦挣扎了一段时间了,非常感谢您的帮助.

I've been struggling with it for a while now, and would really appreciate your help.

谢谢

根据要求,使用该AOP的项目的完整pom.xml(位于foo.bar.utils:utils-common)

As requested, the full pom.xml of the project that's supposed to use that AOP (it lives foo.bar.utils:utils-common)

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>backend</artifactId>
        <groupId>foo.bar.backend</groupId>
        <version>2.0.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>backend-logic</artifactId>
    <repositories>
        <repository>
            <id>maven-s3-release-repo</id>
            <name>S3 Release Repository</name>
            <url>s3://repository.foobar.com/releases</url>
        </repository>
        <repository>
            <id>maven-s3-snapshot-repo</id>
            <name>S3 Snapshot Repository</name>
            <url>s3://repository.foobar.com/snapshots</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>foo.bar.backend</groupId>
            <artifactId>backend-contract</artifactId>
            <version>2.0.0-SNAPSHOT</version>
        </dependency>

        <!-- Spring boot actuator to expose metrics endpoint -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- Micormeter core dependecy  -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-core</artifactId>
        </dependency>
        <!-- Micrometer Prometheus registry  -->
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-registry-prometheus</artifactId>
        </dependency>

        <!-- Common -->
        <dependency>
            <groupId>foo.bar.utils</groupId>
            <artifactId>utils-common</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>

        <!-- Auth -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-security</artifactId>
            <version>2.0.0.M1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <!-- Utils -->
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>18.0</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.4</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-joda</artifactId>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.4</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <build>
        <extensions>
            <extension>
                <groupId>org.springframework.build</groupId>
                <artifactId>aws-maven</artifactId>
                <version>5.0.0.RELEASE</version>
            </extension>
        </extensions>
    </build>

</project>

无效的实现(在应该使用AOP的其他项目中)

The implementation that doesn't work (in the different project that's supposed to use the AOP)

@Slf4j
@Configuration
@EnableAspectJAutoProxy
public class TestingSomething {

    @TimedLog(message = "test something")
    public void testingSomething() {
        log.info("ololol");
    }

}

然后是测试,对其进行测试:

And the test, testing it:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = SomeSpringConfiguration.class,
        properties = {"server.port=0", "enable.security=true"})
@WebAppConfiguration
public class testingSomethingTest {
    @Autowired
    TestingSomething testingSomething;

    @Test
    public void testingLol() {
        testingSomething.testingSomething();
    }
}

推荐答案

要使各个方面正常工作,您需要启用它们.要启用该功能,您需要通过xml或注释来配置它们:

For the aspects to work you need to enable them. To enable you need to either configure them through xml or through annotation:

@Configuration
@EnableAspectJAutoProxy

通过xml:

<beans …>
      <!– Enable AspectJ auto-wiring –>
      <aop:aspectj-autoproxy />
</beans>

将jar包含在另一个应用程序中时,该另一个应用程序具有其自己的配置和上下文.即使为原始上下文启用了方面自动装配,您仍然需要通过上述两种方式之一对新应用程序执行相同的操作.

When you include your jar into another application, this other application has its own configuration and context. Even if you have enabled the aspect autowiring for your original context you still need to do the same for your new application by one of the two ways pointed above.

如果使用注释,请确保它在组件扫描范围之内,并且确实包含在上下文中.

If you use the annotation make sure it is within the component scan scope and that it is actualy included into your context.

更新: 在您的testingSomethingTest上执行@Import (MethodExecutionAspect.class),以确保对其进行了组件扫描.

UPDATE: do @Import (MethodExecutionAspect.class) on your testingSomethingTest to ensure it is component scanned.

这篇关于当在另一个项目中用作已编译的jar时,Spring AOP无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆