Tomcat/Java与PHP比较概览

huangapple go评论76阅读模式
英文:

Overview of Tomcat/Java vs PHP

问题

在Java运行在Tomcat中,比如Spring Boot应用程序内置了Tomcat的情况下:

  1. 由IOC容器管理的所有组件,如@Controller/@Configuration/@Service等注解,指示该类应由容器管理,这些组件是否在应用程序启动时在容器中实例化,在客户端发送任何HTTP请求之前?许多不同路由的不同控制器,它们是否在实际需要处理任何请求之前就被实例化了?
  2. 如果是这样,所有这些组件的实例是否用于处理所有后续的传入请求?比如,控制器实例在处理请求并返回响应后不会被垃圾回收销毁吗?这个控制器一直保存在内存中(我猜是堆内存),直到应用程序停止运行?
  3. 当客户端向Tomcat发送新的HTTP请求时,是否会创建新的线程来处理此请求?这个线程是否使用由容器管理的控制器和其他组件的相同实例来处理请求?

现在是主要问题:

在PHP中,当服务器启动index.php文件并将请求传递给它进行处理时,会启动整个新的进程。PHP程序也有IOC容器、PDO、仓库等组件,这些组件由容器管理。容器可以由开发人员自己创建,也可以由框架(如Symfony、Laminas、Laravel)提供。

这是否意味着每当PHP程序处理请求时,所有可能的控制器都会在IOC容器中实例化,获取它们的依赖关系注入,但由于请求通常只需要处理一个控制器,所有其他在应用程序启动时和IOC容器中创建的控制器和组件都是白白创建的,从未被使用,然后应用程序就死掉了,而没有对那些未使用的控制器/组件执行任何操作,除了与请求的路由匹配的1个控制器?

英文:

Couldn't find this info in google or by asking people in discord programming servers.
Before I ask the main question I wanna confirm something first:

When java runs in tomcat, lets say like in case of Spring Boot application with Tomcat built in it:

  1. all the components managed by ioc container: @controller/@configuration/@service, and other annotations that indicate that class should be managed by the container, do all these components get instantiated in container on startup of the application, before any http request comes from the client ? All the different controllers for many different, routes, do they get instantiated before they are actually needed to process any request ?
  2. if so, do all these instances of these components get used to process all subsequent incoming requests ? like a controller instance wouldnt be destroyed by GC after a request was processed and response was returned to client ? this controller is always kept in memory(heap I guess) until application stops running ?
  3. when a new http request comes to tomcat from client, does some new thread get created to process this request ? does this thread use the same instance of controller and other components managed by container to process the request ?

Now main question.

In PHP the whole new process is started when server starts index.php file and passes the request to it to process it. And php programs also have ioc containers, PDOs, repositories, etc - the components that are manage by container. Container can be self made by developers or provided by FW like symfony, laminas, laravel.

Does this mean that whenever a request is processed by PHP program, all the possible controllers get instantiated in IOC container, get their dependencies injected, but since requests usually need just 1 controller to be processed, all the other controllers and components that got created on startup of app and IOC container, they got just created for nothing, got never used, and then app just died without doing anything with those unused controllers/components, except the 1 controller that was matched by the route of the request ?

答案1

得分: 1

为了回答你的PHP问题:

如果一个容器设计得当,它应该仅在当前请求期间实例化你使用的类。如果你使用单个控制器 - 它应该创建该控制器的单个实例,并将所有依赖项注入其中。

如果一个容器创建所有可能通过它加载的类的实例 - 这将是非常低效的。

通常容器会跟踪如何实例化可能需要的每个实例。就像一个配置文件。每个可以从容器加载的类的依赖关系图。等等。

这意味着容器知道如何实例化通过它配置的每个类,但它只实例化你在当前请求中使用的类。

此外,它应该只加载当前请求所需的文件。而不是全部文件。然而,如果你使用了OPCache扩展,它们将不会每次都被解析,而是会在PHP-FPM进程的内存中保留一个编译后的变体。

英文:

To answer your PHP question:

If a container is done right, it should instantiate ONLY the classes you use during the current request. If you use a single controller - it should make a single instance of that controller and inject all dependencies into it.

If a container makes instances of all possible classes that can be loaded through it - it will be extremely inefficient.

Usually the Containers keeps track of HOW to instantiate every possible instance that you might need. Like a config file. Graph of dependencies of every class that can be loaded from the container. etc.

That means the container KNOWS how to instantiate every class configurable through it, but it instantiates only the ones you use in the current request.

Also it should load only the files needed by the current request. Not all of them. However, if you use OPCache extension, they will not be parsed every time, but will be kept in a compiled variant in-memory of the PHP-FPM process.

huangapple
  • 本文由 发表于 2020年8月8日 22:46:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63316693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定