春季应用中非春季豆的加载时间编织 [英] Load time weaving for non-spring beans in a spring application

查看:98
本文介绍了春季应用中非春季豆的加载时间编织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些REST控制器,服务类和帮助器类的spring boot应用程序.控制器和服务类是Spring管理的,而辅助类不是Spring管理的,并且大多数包含静态方法.

I have a spring boot application with some REST controllers, service classes and helper classes. The controllers and service classes are spring managed while helper classes are not spring managed and mostly contain static methods.

AspectJ配置在Java配置中的显示方式如下

The AspectJ configuration is present in java configuration as follows

@Configuration
@EnableLoadTimeWeaving(aspectjWeaving = AspectJWeaving.ENABLED)
public class AspectConfig {

    @Bean
    public LoggingAspect loggingAspect() {
        return new LoggingAspect();
    }
}

对应的LoggingAspect类如下,

The corresponding LoggingAspect class is as follows,

@Aspect
public class LoggingAspect {

    @Before("allMethodsPointcut()")
    public void logBeforeMethod(JoinPoint joinPoint) {
        System.out.println("Entering Method - " + joinPoint.getSignature().getDeclaringType() + "::" + joinPoint.getSignature().getName());
    }

    @After("allMethodsPointcut()")
    public void logAfterMethod(JoinPoint joinPoint) {
        System.out.println("Exiting Method - " + joinPoint.getSignature().getDeclaringType() + "::" + joinPoint.getSignature().getName());
    }

    @Pointcut("execution(* com.test.controller..*(..)) || execution(* com.test.service..*(..)) || execution(* com.test.helper..*(..))")
    public void allMethodsPointcut() {
    }
}

  • 调用控制器时,启用了Aspect的日志记录适用于控制器和服务功能,但不适用于辅助功能.
  • 如果我们自动连接控制器中的帮助程序类,则非静态帮助程序方法将开始显示Aspectj日志.但是,静态帮助器方法仍然无法显示AspectJ日志
  • 问题, 1.我们如何为非Spring管理的类(即没有@ Bean,@ Autowired,@ Component等)配置aspectj建议. 2.我们如何为静态方法配置aspectj建议(我正在使用@EnableLoadTimeWeaving,但也许我缺少了一些东西) 3.如有可能,AspectJ配置应基于Java

    Questions, 1. How can we configure the aspectj advice for classes which are not spring managed i.e. without @Bean, @Autowired, @Component etc. 2. How can we configure aspectj advice for static methods (I am using @EnableLoadTimeWeaving but maybe i am missing something) 3. AspectJ configuration should be java based if possible

    如果需要更多详细信息,请告诉我

    Kindly let me know if more details are required

    推荐答案

    使用-javaagent:/path/to/aspectjweaver-<version>.jar作为JVM的启动参数来启用加载时编织.从您的spring配置中删除@EnableAspectJAutoProxy,这样spring不会尝试使用它自己的Spring AOP框架而不是纯AspectJ. (可选)创建META-INF/aop.xml.如果要将弹性配置应用于不受spring管理的bean(@Configurable POJO),请添加@EnableSpringConfigured.

    Use -javaagent:/path/to/aspectjweaver-<version>.jar as a startup argument to your JVM to enable load-time weaving. Remove @EnableAspectJAutoProxy from your spring configuration so that spring doesn't try to use it's own Spring AOP framework instead of pure AspectJ. Optionally, create META-INF/aop.xml. Add @EnableSpringConfigured if you want to apply spring configuration to beans not managed by spring (@Configurable POJOs).

    这篇关于春季应用中非春季豆的加载时间编织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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