#Spring Proxying Mechanism (AOP)

1 messages · Page 1 of 1 (latest)

tough hawk
#

So if I understand correctly the jdk dynamic proxies works kinda like this if the target implements any interface (does that include abstract classes?):

interface SomeInterface {
    void proxiedMethod();
}

class Target implements SomeInterface {

    @Override
    public void proxiedMethod() {
        // some implementation
    }

}

and then the proxy looks something like this (completely guessing here lol):

class Proxy implements SomeInterface {
    private final Target target = ...; // actual class to be proxied
    
    @Override
    public void proxiedMethod() {
        // some additional logic before calling
        target.proxiedMethod();
        // some additional logic after calling
    }
}

and then if it doesnt implement an interface:

class Target {
    public void proxiedMethod() {
        // some implementation
    }
}

and then the proxy using cglib is going to be a subclass if it instead of "wrapping":

class Proxy extends Target {
    @Override
    public void proxiedMethod() {
        // some additional logic before calling
        super.proxiedMethod();
        // some additional logic after calling
    }
}
quick haloBOT
#

<@&1004656351647117403> please have a look, thanks.

tough hawk
bitter lance
#

an interface is an abstract class btw, for lower level stuff like this, there's no difference

tough hawk
#

ok yeah makes sense

odd pivot
#

The JDK dynamic proxy option is only for classes that implement a interface. That doesn't include extending an abstract class if I remember correctly, but I don't know the reason behind that.

#

My understanding is if Spring sees your target class doesn't implement any interface if would choose to do the cglib thing.

tough hawk
#

yeah I know that, but how does it actually work?

odd pivot
tough hawk
#

hm idk if thats really helpful, I dont want to go into too much detail, just same basic understanding
like why does jdk dynamic proxy work only with interfaces, etc

tough hawk
odd pivot
native dagger
#

Basically, dynamic proxy is a jdk mechanism of strapping an arbitrary interface on an arbitrary object

native dagger
#

what?

#

it's true

tough hawk
#

Not sure if that answers my question though

native dagger
#

Dynamic proxy is used whenever spring needs to build an implementation of an interface

#

For example, projection return types for repositories

#

If spring needs to extend a concrete class, cglib is used (all your transactional services, aop and stuff)

tough hawk
#

is my question not clear? I want to know if my understanding of proxies is correct by looking at how it wraps (if it does in the first place) the original class, like is my example from above on how it works correct?

subtle granite
#

Most likely the primary reason for the JDK dynamic proxy to require proxying an interface is to ensure that all of the type-identity invariants of the language are valid - there is no general-case solution for proxying in a class-hierarchy. Spring just assumes you'll only try to do it on types that won't trip over this violation. Really, if we were strictly following the rules, the only way to ever do it, would be to dynamically decorate an interface (but class-loading hacks are a thing).

tough hawk
#

yeah I understand that, but not sure if that answers my question

#

lol

#

whatever it aint that important

#

thanks for the help ig