Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

spring - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...