Impossibility of adding advice to final methods when using Spring MVC - spring

I'm reading this official page of Spring documentation and then I read this sentence which I didn't understand :
You cannot add advice to final methods when you use Spring MVC. For
example, you cannot add advice to the
AbstractController.setSynchronizeOnSession() method. Refer to Section
10.6.1, “Understanding AOP proxies” for more information on AOP proxies and why you cannot add advice to final methods.
Can anybody explain to me what they mean by this, and specially by advice?

An advice is a method that should be called before or after a method of another class is invoked.
An example could be a logging advice, that is attached to every method of a service to log out the invocation of every service method.
In order to attach an advice to a method, Spring subclasses the class, the method belongs to and overrides the method with an implementation that calls the advice when the method is invoked. Additionaly the proxy method will also call the overwritten method (the super method) to obtain the original functionality.
A final method cannot be overidden, so Spring cannot create a proxy and you cannat attach an advice.
Its a general limitation, that it is impossible to use a subclass proxy for final methods. It is not a special limitation for aspects.

An advice isn't something specific to Spring MVC, but rather a concept from Aspect Oriented Programming (or AOP for short, see this wikipedia page for a general introduction).
The way Spring Beans work, and the way they allow for AOP, is by taken the class you annotated as a bean, and creating a proxy based on that class, which means on-the-fly / at runtime creating a subclass instance that inherits from your class and which provides custom implementations for each method ('overriding' them). As you know, overriding final methods is inherently impossible (that's what makes them final). That's the reason why the documentation states:
you cannot add advice to final methods

Related

why #RestController by default use singleton scope?

I am actually a EJB developer and very new in spring framework.
i find a couples of conflict conceptually. Like ..
#RestController use by default scope which is singleton. By single object per loc have to manage heavy trafic.
is it a good design?
Of course, it is a good design because the same instance of the object will be reused instead of keep creating it each time you need it. That is the whole point of that design pattern.
Here is a great example where singleton comes to the rescue.
https://rules.sonarsource.com/java/RSPEC-2119
By default, spring will take care of the creation and destruction of all singleton beans, while the prototype has to be manually handled. Therefore in a lot of cases prototype scope is for custom beans made by developers.
In SpringMVC Controller layer, #Scope("prototype") vs #Scope("singleton")
is it a good design?
Yes, all beans in Spring are singletons (by default).
We have 100+ controllers in several applications and it works perfectly.
If you really need to instantiate controllers more than once, you can, of course, consider other bean scopes (see brief explanation of scopes here https://www.baeldung.com/spring-bean-scopes)

What is Spring proxy

I know what is Proxy in network community (server intermediary), but what is proxy in Spring ? Why spring beans are wrapped proxy ? I don't understand the idea of proxy in Spring. Thanks for response.
A proxy is a Spring generated class, that wraps your class for a given purpose, ie: adding transactional behaviour
Take a deeper look at the documentation here
It's a class that wraps your class. It is a proxy because all calls to methods of your class pass through it before actually getting to your class. The goal is to enhance your class with additional functionality, for example as #CristianMeneses said, to add transactional behavior to it, or inject some resources.

Using Inheritance in Spring MVC 3.1

Researching the use of inheritance in Spring MVC.
Is it a good idea to have a base controller that other controllers can extend ?
The base controller would be to hold functionality common to all controllers.For E-g getting a handle to the logged-in user etc.
If using the base controller is not a good idea are there any other suggestions to implement something like what I have mentioned above.
It is perfectly acceptable to have a base controller that other controllers can extend. When Spring introduced #Controller annotations, they paved the way for you to use whatever class hierarchy you want.
Just be aware that as an object oriented design principle, it's good to favor composition over inheritance. As a rule of thumb (not a hard and fast rule) I would recommend moving your common controller code into a class whose interface can be injected into your controllers via #Inject or #Autowired.
Another suggestion for getting a handle to the logged-in user is a little more work but very nice once you have it. See the discussion here about using a current user annotation on your controller method arguments. This is what I do on my project and it works great!

Scenario when we may be needing #Configurable in spring?

I have question about the need of using #configurable. I have gone through the blog that explains how to use #configurable. But the question that comes to my mind is, what can be the scenario when we need to use #configurable. I can think of two scenarios where it can be useful
In a legacy project, when we are already making any bean with new operator and we want to make it spring managed.
In a new project, we want to enforce that even if developer makes the bean with new operator, still it is spring managed.
Otherwise for new beans we can always declare them in applicationContext.xml and I do not see any need to declare them #configurable.
Please let me know if above understanding is correct or if I am missing something.
UPDATE:- Basically as per my understanding configurable is generally used to inject dependency when creating the object with new operator. But why would i be creating the object with new operator when i am using spring
#Configurable annotation is meant for injecting dependencies in domain-driven applications. That means, in such applications, the domain objects interact with each other to perform a certain operation.
Take the following example:
In an invoicing application, the Invoice class provides a constructor to create it, then it has methods to validate, and finally persist it. Now, to persist the invoice, you need a DAO implementation available within the invoice. This is a dependency you would like to be injected or located. With Spring's #Configurable, whenever an invoice is created using the new operator, the appropriate DAO implementation will get injected and can be used for all persist operations.
I had a more realtime scenario where I used #Configurable annotation as described here.

How to add a custom annotation to Spring MVC?

Can anyone explain what I need to do to implement my own annotation that would add functionality to my web requests?
For example:
#Controller
public class MyController {
#RequestMapping("/abc")
#RequiresSomeSpecialHandling
public void handleSecureRequest() {
}
}
Here #RequiresSomeSpecialHandling would be my own annotation that causes some special work to be done before or after the given web request /abc.
I know that on a very high level I would need to write a bean post processor, scan classes for my annotations, and inject custom mvc interceptors when needed. But are there any shortcuts to simplify this task? Especially for the two examples above.
Thanks in advance,
This kind of Annotations, (that add additional functionality when invoking a method) looks like annotations that trigger an AOP Advice.
#see Spring Reference Chapter 7. Aspect Oriented Programming with Spring
The idea is to use the Annotation to trigger the AOP Advice.
like:
#Pointcut("#target(com.example.RequiresAuth)")
Depends on what you want to do as a result of #RequiresSomeSpecialHandling. E.g. do you want it to influence request mappings or the invocation of the method (i.e. resolving method arguments, processing the return value)?
The support for annotated classes in Spring 3.1 became much more customizable. You can browse some examples in this repo.
Also keep in mind that a HandlerInterceptor in Spring 3.1 can cast the handler Object to HandlerMethod, which gives you access to the exact method including its annotations. That may be enough for what you need to do.
If caching is one of your goals, take a look at the #Cacheable annotation (and its siblings #CachePut, #CacheEvict and #Caching), available as of Spring 3.1.

Resources