-1

I am doing a mock exam where I didn't quite understand one of the answers which lacked an explanation of why it was correct.

(Edited from here downward by kriegaex, adding the question from the below comment and some formatting and rephrasing in order to make the text a bit more coherent and readable.)

  • Question: "Which below statement is true about Spring's proxy feature?"
  • Answer: "There is a type of Spring proxy that can replace the object being returned by the method."

I understand that Spring AOP can use two types of proxies:

  • JDK dynamic proxies
  • CGLIB proxies

In my understanding these are the two type of proxies that are heavily used in Spring. For example when using @Transactional or when creating aspects (@Aspect).

What I understand of the answer given is that they are pointing at the @Around aspect. However, I don't understand why they call it a "type of Spring proxy". Is an aspect a proxy? Thus, is my understanding of an aspect making use of the JDK or the CGLIB proxies incorrect?

kriegaex
  • 63,017
  • 15
  • 111
  • 202
Bart Boersma
  • 244
  • 2
  • 13

1 Answers1

2

The question would be easier to understand if you had provided all possible answers, also the incorrect ones. But given the correct one (which really does sound strange), I can tell you the following:

  • Both JDK and CGLIB proxies serve the same purpose: to wrap and replace the original objects in order to be able to register some interceptors to their method calls, be it via Spring AOP or other methods.
  • Yes, both proxy types are used heavily in Spring, JDK proxies for classes implementing interfaces, CGLIB proxies for classes not implementing any interfaces. Optionally, you can configure Spring to use CGLIB proxies also for interface types.
  • There is no such thing as an "@Around aspect", only an @Around advice type (beside other advice types like e.g. @Before and @After).
  • No, an aspect is not a proxy. But Spring AOP uses proxies in order to implement its way of doing AOP via delegation pattern, as opposed to AspectJ which does not use any proxies but uses direct bytecode instrumentation to achieve its goal.

Find more information in the Spring documentation.

kriegaex
  • 63,017
  • 15
  • 111
  • 202