Spring container , web container? - spring

I have moderate understanding of Spring Framework, and in various books I read that Spring acts as a container.
Q1) What does it mean to say that Spring is a container?
Q2) Does spring as a container provide services like transactional, connection pooling etc.
Q3) what difference are in containers spring container vs web container -> It might be totally irrelevant comparsion, but if anyone can help me get this understand, really appreciate.

In spring: Spring container contains beans (Java objects that are subject to dependency-injection). It provides the space for residing these beans and maintains the life-cycle of the Java beans.
this is referred as Spring IOC container because of it provides Spring Inversion of Control.
You can learn more about Spring IOC container at http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html
Web Container, Specially Servlet containers contain servlets, filters, listeners, etc. and manages their state and lifecycle.That is the place where you can deploy your java based web applications.(any java web app e.g:- JSP/Servlet based web app, spring based web app etc...)
So keep it remember that these are two different things.

Related

tomcat webserver/servlet container/dispatcher servlet interaction in springboot

what is the flow diagram of interactions between tomcat web server/servlet container and dispatcher servlet . tomcat servlet container is separate from spring IOC container or applicationcontext ?
Tomcat is a web server. It "knows" how to deal with web applications according to the web applications specification.
This specification defines means of servlets declaration, their order, etc. It also defines additional notions like filters, listeners, etc. This specification has nothing to do with spring. Basically, you can use tomcat to host the application with servlets, filters, listeners without spring dependencies, so you won't even have an application context.
With this in mind, Spring MVC (to which the Dispatcher Servlet actually belongs) is just a third-party among many others from the Tomcat standpoint.
DispatcherServlet is an ordinary servlet that the spring team has developed. It is an entry point to all spring infrastructure in web applications managed by a web server, it's a "glue" that acts as a "driver" - it loads the application context (all the beans), handles all the requests coming to the controllers, etc.

is spring boot only for building rest api?

and if not what are more things that we can do with spring boot?
i know that we can build a whole web app(frontend and backend) in one spring boot application in the folder resource/template and resource/static but in the real world does somebody uses this method to create web application with the resource/template and resource/static?
and one more question what is used in the real world hibernate(with the SessionFactory or EntityManager) or JpaRepository in the spring data jpa?
No Spring Boot isn't just for REST APIs.
Spring Boot is "just" a mechanism for autoconfiguring a Spring Framework based application.
Therefore you can use and it does get used for all kinds of stuff.
REST APIs for webservices
Full web application using Spring MVC
SOAP services (or are they called SOAP dispensers?)
Reactive web applications
Command line tools
Batch jobs
Swing / JavaFX applications
...
Of course there are many more people writing web applications than Swing applications with or without Spring.
The kind of web application you describe and which I put under "Full web application using Spring MVC" is a very well established model and when done right way better aligned with the principles of REST than the average so called REST service. My very personal guess is: They will still be around when nobody remembers what Angular is.
For your additional question:
Your question sounds a little like the relation between JPA and Spring Data JPA might not be completely clear.
(see Spring Data JDBC / Spring Data JPA vs Hibernate)
Both are certainly used in real world projects. By definition more projects use JPA than Spring Data JPA since the first is a superset of the later.
This involves complete Spring history,
Actual motive of Spring was to enable loose coupling , so that unit tests can be easily performed . Spring MVC was for developing web applications with Model View Controller having their proper boundaries.
Then Spring Boot which enabled developer to focus on business logic then configurations. That's why spring boot is a good choice for microservices.
For JPA or hibernate query , many people prefer using JPARepositoy as again you just have to define entity for the repository and Spring boot automatically provides you queries like findById and so on.
In short Spring boot have made it really easy to run the applications with different configurations and environment smoothly.

spring boot without web server but with management server

we have many services developed with spring boot, some are web services with a servlet container and some are services without a servlet container.
we don't use #EnableAutoConfiguration but we add only the auto configurations we need because our classpath is a mess and full auto configuration may create many beans we don't really need.
on services that don't need a servlet container we still want to have actuator and management server, so we register EndpointWebMvcAutoConfiguration but it has a condition on web application and does not create the management server.
of course i could just add EmbeddedServletContainerAutoConfiguration and just have a servlet container that does nothing but i don't like to do that.
Any idea how to cause spring boot to create the management server even on non web environment?
Thank you

Spring Cloud Netflix - how to access Eureka/Ribbon from traditional web app?

Everything I found on the internet about Spring Cloud Netflix is about running microservices from Boot applications using #EnableEurekaClients and so on.
Now I'm trying to connect my logging microservice within a traditional war application (springmvc, jaxws etc) - piece of legacy which can not be converted to Boot or modified in any way (by technical task).
I've created a new maven module "log-server-client" that knows nothing about upper web layer and intended to be used as a simple dependency in any maven project.
How should I configure access to Spring Cloud Netflix for this simple dependency? At least, how to configure Eureka and Ribbon?
I just extracted some lines of code from RestTemplate and created my custom JmsTemplate (microservice works with jms remoting with apache camel and activemq), exactly how it is done in RestTemplate, but this code stil lacks connection to infrastructure
afaik, we can create a global singleton bean, run a separate thread from this bean, and run Boot app from this thread, but don't you think that it is very ugly and can lead to problems? How it really should be used?
Great question!
One approach is to use a "sidecar". This seems to be a companion Spring Boot application that registers with the Eureka Server on behalf of your traditional web app.
See e.g.:
http://www.java-allandsundry.com/2015/09/spring-cloud-sidecar.html
http://cloud.spring.io/spring-cloud-netflix/spring-cloud-netflix.html#_polyglot_support_with_sidecar
Another approach is to use the following library:
"A small lib to allow registration of legacy applications in Eureka service discovery."
https://github.com/sawano/eureka-legacy-registrar
This library can be used outside of Spring Boot.

Spring and EJB integration

In my project we are using JSF and Spring WS (web tier), EJB 3.0 (service layer) and JPA (integration layer). We have exposed EJB in Spring container.
All technologies are used with Spring. So, Spring is a used to bind all layers together. Hence, Spring is common for all layers of architecture.
I read that, many features provided by EJB is also available in Spring. Can't we replace EJB with Spring? Why, EJB and Spring both are used together.
I want to understand, what are benefits of using such architecture.
Spring is an alternative for EJB. Usually EJB and Spring are not used together unless it is a legacy application which is already developed based on EJB and Spring is wired later to support dependency injection and other framework benefits.
EJB is a heavy weight container which requires App containers like JBoss, WebSphere, or Weblogic.
Spring is a very light weight container which can be used in in web containers like Tomcat and even in Standalone applications. Also it provides support for many modules from front-end to back-end.
If there is a chance, you should consider replacing EJB with Spring bean in service layer.
Does it means that Spring provides all features which are available from EJB 3.0. Clustering and all other features of EJB can also be acheived from Spring.

Resources