Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why don't I see any main method in this Java dynamic web project?

I was trying to understand how the Web Services work and I came across this tutorial.

Now, I've seen Spring being used in enterprise applications and always wondered where the main method was and how everything worked? And whenever I would go to a Spring tutorial they'll start with beanFactory and Contexts and what not, all in a main Java method and from there just keep getting beans as required. This is totally different from what I see in the applications.

How exactly does Spring work in this case? What is the sequence of calls? I guess there will be some hidden main method somewhere, but I am not sure of that.

Normally if I were to run a simple Java project from command line, I'd do java mainClass. Now how would it happen in this case?

like image 452
Kraken Avatar asked Jan 03 '14 09:01

Kraken


People also ask

Where is the main () method of a Java web application?

There is no "main" method (as in application entry point) in web applications, since each servlet is an independent entry point.

Why doesn't a servlet include main ()? How does it work?

Whenever client send a request, webcontainer calls servlet with�its own life cycle method. So there is no need of having main() method in servlets.

Where is main method in spring?

A Spring Boot application's main class is a class that contains a public static void main() method that starts up the Spring ApplicationContext. By default, if the main class isn't explicitly specified, Spring will search for one in the classpath at compile time and fail to start if none or multiple of them are found.

Where is the main method in Spring MVC?

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.


1 Answers

There is still a main method. It's just not written by the developer of the application, but by the developer of the container.

You can still see the main method being called by using the debugger like this:

  • Put a breakpoint in some initialization method, such as the init method of some servlet Servlet.init()
  • When the breakpoint hits, scroll down the call trace and the main method should be at the bottom.

This is an example with Jetty:

Enter image description here

To see this we need to put the breakpoint in an initialization method so that we get the main thread of the application.

Putting the breakpoint in the processing of a request instead of an initialization method would show Thread.run() at the bottom of the stack trace and not main().

Thread.run() is the equivalent of the main method for threads other than the main thread.

So the main method still exists. It's just being handled at the level of the container.

like image 152
Angular University Avatar answered Oct 01 '22 09:10

Angular University