INTERFACES or TARGET_CLASS: Which proxyMode should I choose?

后端 未结 3 1375
小鲜肉
小鲜肉 2020-12-07 17:44

I am looking for a way to store my object and it seems that the best approach is to use proxies. I found 2 annotation in the internet, which one should I use :



        
3条回答
  •  萌比男神i
    2020-12-07 18:39

    You'll need to understand what each of those annotations does to choose for yourself. See the javadoc, here. Continue for a more detailed explanation.

    The first

    @Scope(value = "session", proxyMode = ScopedProxyMode.INTERFACES)
    

    creates

    a JDK dynamic proxy implementing all interfaces exposed by the class of the target object

    In other words, the proxy will be a subtype of the interfaces that the target object's class implements, but won't be a subclass of the target object's class itself.

    Essentially Spring does the following

    public class Example {
        public static void main(String[] args) throws Exception {
            Foo target = new Foo();
            InvocationHandler proxyHandler = ... // some proxy specific logic, likely referencing the `target`
    
            // works fine
            Printable proxy = (Printable) Proxy.newProxyInstance(Example.class.getClassLoader(),
                    target.getClass().getInterfaces(), proxyHandler);
    
            // not possible, ClassCastException
            Foo foo = (Foo) proxy; 
        }
    
        public static class Foo implements Printable {
            @Override
            public void print() {
            }
        }
    
        public interface Printable {
            void print();
        }
    }
    

    The proxy returned won't be of type Foo and you therefore can't inject it into any targets of that type. For example, Spring will fail to inject it into a field like

    @Autowired
    private Foo foo;
    

    but will successfully inject the proxy into a field like

    @Autowired
    private Printable printable;
    

    All calls to the proxy will be handled by the InvocationHandler (which usually performs some use case specific logic then delegates to the target object).


    The second annotation

    @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS )
    

    creates

    a class-based proxy (uses CGLIB).

    In addition to interfaces, with CGLIB Spring will be able to create a proxy whose class is a subclass of the target's class. In essence, it does the following

    Foo target = new Foo();
    net.sf.cglib.proxy.Enhancer enhancer = new net.sf.cglib.proxy.Enhancer();
    enhancer.setInterfaces(target.getClass().getInterfaces());
    enhancer.setSuperclass(target.getClass());
    net.sf.cglib.proxy.MethodInterceptor interceptor = ... // some proxy specific logic, likely referencing the `target`
    enhancer.setCallback(interceptor);
    
    // works fine
    Foo proxy = (Foo) enhancer.create();
    

    CGLIB creates a new class that is a subclass of Foo and instantiates it (invoking the constructor of Foo). All calls to the proxy will be intercepted by the provided callback (which usually performs some use case specific logic and then delegates to the target object).

    Since the proxy class extends Foo, Spring can inject the proxy into a field (or constructor/method parameter) like

    @Autowired
    private Foo injectMe;
    

    All this to say, if you're programming to interfaces, then ScopedProxyMode.INTERFACES will be sufficient. If you're not, then use ScopedProxyMode.TARGET_CLASS.


    As for using @SessionAttributes, it is not an alternative to session scoped beans. Session attributes are just objects, they are not beans. They don't possess the full lifecycle, injection capabilities, proxying behavior that a bean may have.

提交回复
热议问题