My understanding of a basic Java application is that every application should have a main() method but when I wrote a Spring MVC application (not Spring Boot), I don't think there was ever a main() method that I wrote. Isn't it mandatory? What am I missing here? Or is this implemented in Spring somewhere internally? If yes, where is the main defined?
Spring MVC is just a Servlet based framework which can only be run inside Servlet container like Tomcat or Webligic. The main method is deep in Servlet container that when you start will go and load war file into Java VM and delegate HTTP calls made to it to the appropriate Servlet that in turn will delegate to your Spring controller. For example when you run Tomcat startup.bat or startup.sh scripts they eventually will run Java main method.
SpringMVC applications are typically run within an application server, for example Tomcat, so there is no main method like a traditional java program. SpringMVC has a servlet that is loaded by the application server and starts the webapp.
Related
Currently I have simple spring rest application which situated in separate gradle module. Is it possible to add gradle module with play application which will run spring context?
No - Spring requires a servlet container, Play is not a servlet container. It's like asking if you could run a Spring REST application in WordPress, they're completely different technologies.
If there are multiple web applications in different tomcats, how to configure same application context for all the tomcats
I am not sure if i understand the question correctly. I guess you misunderstand some concepts. First, what is your package; is it war or jar. Do you use Spring MVC or Spring Boot (If you are new i suggest you to use Spring Boot). If you are using Spring MVC you are required to deploy your war to a servlet container like Tomcat. And if you want to deploy your applications to multiple tomcats, it is possible of course. However each application run on their own application context. These application contexts will have same beans and will be on same state when they are firstly initialized. As far as i know it is not possible to share one application context over multiple spring mvc applications and i think it doesn't make sense.
If you are using Spring Boot, your application will run on JVM with an embedded servlet container. In this case, your application is packaged in a JAR file with servlet container and you are not required to deploy the application. When you run the application both servlet container and your application context is initialized. In this situation, you can run multiple applications and as i said before these applications will have different application contexts.
I'm currently trying to shift my existing dynamic web project to Spring boot project and it uses web.xml for servlet mapping. I understand that spring would ignore the web.xml file, what should be the correct approach for spring to use the existing web.xml? And yes, I still need to stick to using web.xml for this project.
I'm kinda new to this, please guide me through! Thanks!
I suppose that you need to stick with a web.xml because your container uses an older version of Servlet than 3.0.
Spring Boot is built on Servlet 3.0. You have to update your main class to extend SpringBootServletInitializer and override configure method, which tells spring to use its Servlet 3.0 support. Embedded containers like Tomcat need Servlet 3.0, so if you want to start your project during the development process (including JUnit tests) in embedded containers, I think, from what I know, the only way is to rewrite your web.xml to Servlet 3.0 java config. But if you really want to deploy you app in an older container, you still can by using spring-boot-legacy module. It allows you to use web.xml for older containers; only thing you have to do is to add
org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener in your web.xml.
For more information about deploying war in an old container, take a look at Spring Boot's official documentation.
Spring Boot uses Servlet 3.0 APIs to initialize the ServletContext
(register Servlets etc.) so you can’t use the same application out of
the box in a Servlet 2.5 container. It is however possible to run a
Spring Boot application on an older container with some special tools.
If you include org.springframework.boot:spring-boot-legacy as a
dependency (maintained separately to the core of Spring Boot and
currently available at 1.0.2.RELEASE), all you should need to do is
create a web.xml and declare a context listener to create the
application context and your filters and servlets.
This is a very old question, but I'm currently on the same situation and I will share my experience.
I extended the SpringBootServletInitializer class to create my #SpringBootApplication, and it will INCLUDE your web.xml configuration by default. I'm still able to declare or edit existing servlet mapping, and it will be taken in account.
Normally, when you developing a simple mvc application, with maven, based on Spring Boot, you starts from SpringApplication.run(.class, args), then endpoint that returns you e.g. "Hello World". After that you write mvn spring-boot:run (or start it by your ide) and Spring makes everything for you.
Spring also setting up Tomcat servlet container, and then deploy your package to that, so you don't need to make it manually.
And my question:
How exactly Spring starts that Tomcat service? If i would like to start in same way some other services e.g. Apache Felix, Apache Jackrabbit or some other, what should I do? Is that any configured bean that is set in starter package, or some other mechanism?
How can i tell a application server to call a method in my stateless ejb so that I can print some message during deployment. If I annotate the method with #PostConstruct then it is not working. I also added static block still not working. I dont want to use MBean. Also my ear does not have any web project. So I can not use any servlet and its init method to print such messages.
Server we are using is Jboss.
thanks
There is no such mechanism in EJB 3.0.
Eager / auto loading of EJB / load EJB on startup (on JBoss)