超类的字段为null

时间:2018-09-18 09:51:46

标签: java cglib

我是有关cglib的新蜜蜂,今天我用cglib编写了一些代码,代码如下所示

public class App {
    public static void main(String[] args) {
        //filed
        UserService userService = new UserService();

        RealSubject realSubject = new RealSubject();
        realSubject.setUserService(userService);
        Enhancer enhancer = new Enhancer();

        enhancer.setSuperclass(realSubject.getClass());

        enhancer.setCallback((MethodInterceptor) (obj, method, args1, proxy) 
                      -> proxy.invokeSuper(obj, args1));
        realSubject = (RealSubject) enhancer.create();
        realSubject.doService();

         // i expect get userService but null 
        System.out.println(realSubject.getUserService());
    }
}

os:uname -a

txt Darwin tes.local 17.7.0 Darwin Kernel Version 17.7.0: Thu Jun 21 22:53:14 PDT 2018; root:xnu-4570.71.2~1/RELEASE_X86_64 x86_64

jdk:java -version

➜ ~ java -version Java(TM) SE Runtime Environment (build 1.8.0_161-b12) Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)

我如何获得该领域? ,请原谅我的英语不好。

0 个答案:

没有答案