为什么我在这个 java 动态 web 项目中没有看到任何 main 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20900098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:07:59  来源:igfitidea点击:

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

javaspringjakarta-ee

提问by Kraken

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

我试图了解 Web 服务是如何工作的,但我遇到了本教程

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 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.

现在,我看到 spring 在企业应用程序中被使用,并且总是想知道主要方法在哪里以及一切如何工作?每当我去 spring 教程时,他们都会从 beanFactory 和 Contexts 开始,而不是什么,所有这些都在一个主要的 java 方法中,然后根据需要继续获取 bean。这与我在应用程序中看到的完全不同。

Can someone tell me 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.

通常,如果我要从命令行运行一个简单的 java 项目,我会做java mainClass. 现在在这种情况下会如何发生。

Thanks

谢谢

采纳答案by Gimby

Web applications don't have a main; the 'program' that is running is actually the web container (Apache Tomcat, Glassfish, JBoss, Weblogic, whatever) and that program will service the web application(s) you deploy into it. You might want to read the JEE tutorial to learn and understand what a Java web environment is.

Web 应用程序没有主程序;正在运行的“程序”实际上是 Web 容器(Apache Tomcat、Glassfish、JBoss、Weblogic 等),该程序将为您部署到其中的 Web 应用程序提供服务。您可能需要阅读 JEE 教程来学习和理解什么是 Java Web 环境。

https://docs.oracle.com/javaee/7/tutorial/

https://docs.oracle.com/javaee/7/tutorial/

回答by Andrei Nicusan

You don't see any explicit mainmethod just because it is a Web project. This project is built into a web application archive (WAR) file which is deployed into a web server / servlet container, e.g. Tomcat in this tutorial.

您不会main因为它是一个 Web 项目而看到任何显式方法。该项目内置于 Web 应用程序归档 (WAR) 文件中,该文件部署到 Web 服务器/servlet 容器中,例如本教程中的 Tomcat。

Web applications does not have to contain mainmethods. This is because you don't need to explicitly start any Java process from within your webapp. Somewhere in its depths, Tomcat calls a mainmethod of the code it has been built from. This happens at server startup time.

Web 应用程序不必包含main方法。这是因为您不需要从您的 web 应用程序中显式启动任何 Java 进程。在它的深处,Tomcat 调用main它构建的代码的方法。这发生在服务器启动时。

Then, it will bind your code to incoming HTTP calls, but it will not start new processes for that. It will rather start new threads.

然后,它会将您的代码绑定到传入的 HTTP 调用,但不会为此启动新进程。它宁愿启动新线程。

回答by Sachin Verma

Web applications are not standalone application, they run on some applications what we call servletContainer in java context so there is no main method or java process(os)for any web application. They are just deployed on those containers that have main method and java process in OS runtime.

Web 应用程序不是独立的应用程序,它们运行在我们在 java 上下文中称为 servletContainer 的某些应用程序上,因此main method or java process(os)任何 Web 应用程序都没有。它们只是部署在那些在 OS 运行时具有 main 方法和 java 进程的容器上。

回答by Angular University

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

还有一个main方法,只是不是由应用程序的开发人员编写的,而是由容器的开发人员编写的。

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

您仍然可以看到使用调试器调用的 main 方法,如下所示:

  • Put a breakpoint in some initialization method, such as the init method of some servlet Servlet.init()
  • When the breapoint hits, scroll down the call trace and the main method should be at the bottom.
  • 在一些初始化方法中设置断点,比如一些servlet Servlet.init()的init方法
  • 当断点命中时,向下滚动调用跟踪,主要方法应该在底部。

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() 而不是 main()。

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

Thread.run() 相当于主线程以外的线程的 main 方法。

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

所以main方法还是存在的,只是在容器层面处理而已。

回答by Hitanshi Mehta

If you've created a basic program in Java then you must know that every Java program has a main() method, which is the starting point of the program. So, how come servlets don't have a main()? That is because servlets are served using via Web containers. Web container will perform all underlying work on behalf of servlet so programmer can focus on business logic. When a client requests a servlet, server hands requests to a Web container where the servlet is deployed.

如果您已经用 Java 创建了一个基本程序,那么您必须知道每个 Java 程序都有一个 main() 方法,它是程序的起点。那么,servlet 怎么没有 main() 呢?这是因为 servlet 是通过 Web 容器提供的。Web 容器将代表 servlet 执行所有底层工作,因此程序员可以专注于业务逻辑。当客户端请求 servlet 时,服务器将请求交给部署了 servlet 的 Web 容器。

enter image description here

在此处输入图片说明