Spring AOP CGLIB 代理的字段为空描述媒体列表类自定义媒体列表类弹簧配置类AOP 配置类测试代码最小示例

2022-09-03 12:49:20

描述

使用 vlcj 组件,自定义组件显示为 AOP 代理对象 null 的结果。

媒体列表类

public class MediaList {
    private libvlc_media_list_t mediaListInstance;
    public MediaList(LibVlc libvlc, libvlc_instance_t instance, libvlc_media_list_t mediaListInstance) {
        this.libvlc = libvlc;
        this.instance = instance;
        createInstance(mediaListInstance);
    }
    private void createInstance(libvlc_media_list_t mediaListInstance) {
        logger.debug("createInstance()");
        if(mediaListInstance == null) {
            mediaListInstance = libvlc.libvlc_media_list_new(instance);
        }
        else {
            libvlc.libvlc_media_list_retain(mediaListInstance);
        }

        this.mediaListInstance = mediaListInstance; // <- assignment
        logger.debug("mediaListInstance={}", mediaListInstance);

        mediaListEventManager = libvlc.libvlc_media_list_event_manager(mediaListInstance);
        logger.debug("mediaListEventManager={}", mediaListEventManager);

        registerEventListener();
    }
    public final libvlc_media_list_t mediaListInstance() {
        return mediaListInstance; // <- proxy object return null, if use aop
    }
}

自定义媒体列表类

public class TestMediaList extends MediaList {

    public TestMediaList(LibVlc libvlc, libvlc_instance_t instance) {
        super(libvlc, instance);
    }

    public void xTest(String test){
        System.out.println(test);
    }
}

弹簧配置类

@Configuration
public class PlayerBeanConfig {

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    @Resource
    public TestMediaList testMediaList(LibVlc libvlc, libvlc_instance_t instance) {
        return new TestMediaList(libvlc, instance);
    }
}

AOP 配置类

@Aspect
public class MediaListAspect {
    @Pointcut("execution(* TestMediaList.xTest(..))")
    private void anyMethod() {
    }

    @Around("anyMethod()")
    public Object lockAndUnlock(ProceedingJoinPoint joinPoint) throws Throwable {
        Object object = joinPoint.proceed();
        return object;
    }
}

测试代码

public static void main(String[] args) {
    boolean b = new NativeDiscovery().discover();

    if (b) {
        springContext = new AnnotationConfigApplicationContext(PlayerBeanConfig.class);

        String[] kkk = new String[]{};
        TestMediaList list = springContext.
                getBean(TestMediaList.class, LibVlc.INSTANCE, LibVlc.INSTANCE.libvlc_new(kkk.length, kkk));

        System.out.println(list.mediaListInstance()); // <- proxy object return null
    } else {
        logger.error("Cannot find vlc lib, exit application");
    }
}

我尝试单步跟踪,当TestMediaList构建完成时。方法的 MediaListInstance () 以返回到正常值,但是当 spring 返回到代理对象时,将返回 null。同时,如果您不使用AOP,我也尝试正确返回值。因此,我在AOP动态代理中确定了基本问题,但不知道为什么,以前没有遇到过这样的情况。


最小示例

所有类在包装:vod.demo

目标类

public class TargetClass {
    private String returnValue;

    public TargetClass() {
        this.returnValue = "Hello World";
    }

    public final String test() {
        System.out.println("TargetClass.test();");
        return returnValue;
    }
}

方面类

@Aspect
public class AspectClass {
    @Pointcut("execution(* vod.demo.TargetClass.*(..))")
    private void targetMethod() {
    }

    @Around("targetMethod()")
    public Object aroundTarget(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("AspectClass.aroundTarget();");
        return joinPoint.proceed();
    }
}

弹簧配置类

@Configuration
@EnableAspectJAutoProxy
@Import(AspectClass.class)
public class SpringConfig {
    @Bean
    public TargetClass target() {
        return new TargetClass();
    }
}

客户端类

public class Client {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
        TargetClass target = context.getBean(TargetClass.class);
        System.out.println("Client invoke:" + target.test()); // <- output null
    }
}

答案 1

这是潜在意外行为的组合。首先,Spring使用CGLYB来代理您的豆子以获取AOP。CGLIB 代理是类的动态子类型的实例,它将所有方法调用委托给类的实际实例。但是,即使代理是子类型,其字段也不会初始化(即,不会调用您的超级构造函数)。更详细的解释可以在这里找到。TargetClass

此外,您的方法

public final libvlc_media_list_t mediaListInstance() {
    return mediaListInstance; // <- proxy object return null, if use aop
}

public final String test() {
    System.out.println("TargetClass.test();");
    return returnValue;
}

是。因此,CGLIB 无法覆盖它们以委托给真实实例。这将在春季日志中有所暗示。例如,您会看到final

22:35:31.773 [main] INFO  o.s.aop.framework.CglibAopProxy - Unable to proxy method [public final java.lang.String com.example.root.TargetClass.test()] because it is final: All calls to this method via a proxy will NOT be routed to the target instance.

将上述所有内容放在一起,您将获得一个代理实例,其中字段位于该代理中,并且代理无法委托给真实实例的方法。所以你的代码实际上会调用null

public final String test() {
    System.out.println("TargetClass.test();");
    return returnValue;
}

对于字段为 的实例。returnValuenull


如果可以,请更改方法,删除修饰符。如果你不能,你将不得不重新考虑你的设计。final


答案 2

推荐