开发者

CGLib and serialization

开发者 https://www.devze.com 2023-01-30 13:03 出处:网络
What I want to do is to serialize a class that implements a command pattern so that I can run it later. In best

What I want to do is to serialize a class that implements a command pattern so that I can run it later. In best of all worlds, I would like to serialize an anonymous class (I'm aware of the problems with this)

What makes it a bit complicated is that I'm in a spring environement and the anonymous class that I wish to serialize is defined in a class that will be proxied by CGLib.

My initial try:

public interface CallLater implements Serializeable {
     public run();
}


@Service
public class TestService implements Serializeable {

     public doSomethingMore() { /* Code */ }

     public void doSomething() {

         CallLater job = new CallLater() {
            private static final long serialVersionUID = 4415017504667122645L;

            @Override
            public void run() {
               doSomethingMore();

            }
         }

         storeJob(job);
    }
}

E.g The storeJob will serialize the object and store it for later and it's this part that开发者_运维问答 fails. I have not included the code for this but is's just a ObjectOutputStream call

I'm aware that the entire TestService class needs to be serialize. The problem is that it's wrapped threw a CGLib proxy and this proxy does not implement serializeable (My guess is that this one can't be serialized).

Any help is highly appreciated.


You should only serialize the "target". So you have a reference to the proxy right? There will be away to get the target of that proxy so that you can serialize that.

The thing to do would be to break point the app just before the serialization and dig around in that context for the property you need. Or there might be some proxy utils or something.

0

精彩评论

暂无评论...
验证码 换一张
取 消