Spring MVC Controller vs Servlet - spring-mvc

I have a query regarding how Controllers actually serve the requests..when compared to servlets.
Servlets when instantiated ..have only 1 instance and for each request spawns a thread. How
does Spring MVC controllers serve requests.
Regards

Related

Spring Boot : Apache CXF SOAP with #RestController for rest ws

I'm working SOAP and REST together into the same application. Rest web service with #RestController and SOAP with apache cxf.
Rest ws and soap have the same path, for example:
Rest: GET http://localhost:8080/ws/person
SOAP: http://localhost:8080/ws/findPerson
For configuring cxf servlet, i create the following method
#Bean
public ServletRegistrationBean cxfServletRegistration() {
return new ServletRegistrationBean(new CXFServlet(), "/ws/*"); }
SOAP Service are running fine after change but REST (#RestController) stop working, but if I disable the method cxfServletRegistration(), the rest WS working fine.
Could you suggest any solution to make all WS working together ?
You can't, because each servlet must "own" its listening basepath. Despite the lack of an explicit registration, RestControllers listen on a base path (default /*) Do you actually need to use #RestController? CXF has REST support via JAX-RS.
Otherwise, I would suggest to separate your REST and SOAP functionality, such as having REST on /model/... and SOAP on /api/... or some such separation.

how to log the request information using logback for very method annotated with #RequestMapping in a spring mvc controller?

Right now I am using logback for logging in one spring mvc project. And I need to log every request against the controller. There are quite a lot of methods in the controller and they are annotated with the #RequestMapping. I am thinking if there is a way to make the log automatically with Aspectj or something which is already built-in in SpringMVC framework. Someone who knows it?
#RequestMapping(value = "accounts/", method = RequestMethod.GET)
public Collection<DTOWithAccountInfo> getAccounts(final HttpServletRequest request) {
//todo:log the request info for
}
so I want to log the request info like URI for example for very #RequestMapping annotated method in spring mvc, maybe AOP is a good point to start with?

Spring Boot - Why it does not consider project name as context for the dispatcher servlet mapping

Just like Spring MVC when we used deploy the application in the servlet container it is used to pickup the project name as servlet context and then as per our servlet mapping the container will process the request.
Here my question is i created spring-boot rest application having crud operations and deployed in tomcat server. But when i try to access any url like [http://localhost:8080/findAllRecords] then the server return 404 error request url not found but i add extra / in the url[http://localhost:8080//findAllRecords] it successfully execute the request.

Spring MVC #PreDestroy method not working

I am using #controller in my controller class which internally creates bean. For this controller I need to use #postConstruct and #preDestroy methods, #postConstruct is working fine but #preDestroy is not working. It means Bean is not destroying. So how can I destroy bean in spring MVC(annotation based)(I am not using ApplicationContext).
Correct me if my assumption was wrong.
WHen you say you dont use application context, do you mean to say that you are not using xml based configuration and are using java annotation config?
With spring mvc controllers, #PreDestroy annotated method will be called on session expiry (unless it's prototype scoped)
Here is a post #PreDestroy on Session-scoped Spring MVC Controllers
Here is a good explantation on spring bean life cycle http://www.journaldev.com/2637/spring-bean-life-cycle-methods-initializingbean-disposablebean-postconstruct-predestroy-aware-interfaces
I found out that if I set the bean scope to singleton, I can get PreDestroy called but not if I set it to prototype.

How does the web container manage the lifecycle of a spring controller

If i write a simple servlet application, in my servlet class i extend the http servlet. This lets the container know that my class is a servlet and it will manage the 'lifecycle' of the servlet. init, doget(), destroy() etc.
But the Spring MVC framweork controller class does not extend any servlet class. it is jsut a POJO with it s own custom methods. Now I can call those methods individually using Requestmapping.
But will this spring controller class be 'mananged' by the container in a same way a servlet lifecyle is managed?
But will this spring controller class be 'mananged' by the container
in a same way a servlet lifecyle is managed?
Not directly. Then entry point of a Spring MVC application is typically a DispatcherServlet*. This class extends (not directly, but through inheritance) HttpServlet. You declare it as you would any other Servlet, typically in web.xml.
However you don't declare it by itself. You provide a Spring ApplicationContext from which the DispatcherServlet can go and get the #Controller annotated classes it will use to handle requests.
The DispatcherServlet handler stack is pretty big. There are many components involved. The official Spring MVC is an excellent document. You should read it.
*I say typically because Spring provides other handlers, HttpRequestHandler for example.
Additional Reading:
Spring MVC and Servlets 3.0 - Do you still need web.xml?
What happens behind the scenes of deploying a Spring MVC application to tomcat outside of eclipse?
What's the difference between #Component, #Repository & #Service annotations in Spring?
Spring MVC: difference between <context:component-scan> and <annotation-driven /> tags?
Difference between <context:annotation-config> vs <context:component-scan>
ContextLoadListener and DispatcherServlet

Resources