java Spring 作为独立还是在 Tomcat 上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11080464/
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-10-31 03:42:09  来源:igfitidea点击:

Spring as standalone or on Tomcat?

javaspringjakarta-eetomcat

提问by rayman

I am seeking for the advantages of having Spring deployed on Tomcat rather then have it out side of any application server container.

我正在寻求将 Spring 部署在 Tomcat 上而不是将其置于任何应用程序服务器容器之外的优势。

My project doesn't require any web support. It does requires technologies like transactions management, DB pool, JMX, low latency and more common java-ee technology.

我的项目不需要任何网络支持。它确实需要事务管理、数据库池、JMX、低延迟和更常见的 java-ee 技术等技术。

So why would I use tomcat anyway? if it's just because of the reason of having DB POOL, I could implement it myself. I am looking for low latency solution.

那么我为什么要使用 tomcat 呢?如果只是因为有DB POOL的原因,我可以自己实现。我正在寻找低延迟解决方案。

Again, my project is a total backend no need of any web support.

同样,我的项目是一个完全后端,不需要任何网络支持。

So what do I miss here?

那么我在这里想念什么?

采纳答案by Dave Newton

What do you actually mean by "more common Java EE technology"?

“更常见的 Java EE 技术”实际上是什么意思?

If it's "just a back end", what is the frontend? How will applications talk to the back end?

如果它“只是一个后端”,那么前端是什么?应用程序将如何与后端通信?

If there's no need for a web interface, there's no advantage to using a web container.

如果不需要 Web 界面,则使用 Web 容器没有任何优势。

If you have complex transaction management needs, need message queues, etc. that may be easier to set up under an applicationserver (as opposed to a web container) because there are existing admin/management interfaces. All those may alsobe set up on their own, but can be more of a pain--using Spring may mitigate that pain somewhat.

如果您有复杂的事务管理需求,需要消息队列等,在应用程序服务器(而不是 Web 容器)下设置可能更容易,因为有现有的管理/管理界面。所有这些可以自己设置,但可能更痛苦——使用 Spring 可能会在一定程度上减轻这种痛苦。

The need for "more common Java EE technology", however, makes me a little nervous about implementing a standalone app, though. App containers have all that "common Java EE technology" built-in, tested, and functional. If you're bolting a variety of packages together to give you "common Java EE technology", without using a common Java EE app container, it's likely easier to just use an app container, which also gives you the benefit of providing normalized access to your services from a variety of sources.

然而,对“更常见的 Java EE 技术”的需求让我对实现一个独立的应用程序有点紧张。应用程序容器具有所有“通用 Java EE 技术”的内置、测试和功能。如果您将各种包捆绑在一起以提供“通用 Java EE 技术”,而不使用通用 Java EE 应用程序容器,则仅使用应用程序容器可能更容易,这也为您提供了提供对来自各种来源的服务。

回答by Samarth Bhargava

If you don't need web support, you don't have to use tomcat or any other app server. Spring will provide you with most of the features you need. For connection pool, there are many options available such as c3p0 & apache dbcp. You can use one of them.

如果您不需要网络支持,则不必使用 tomcat 或任何其他应用程序服务器。Spring 将为您提供您需要的大部分功能。对于连接池,有很多可用的选项,例如 c3p0 和 apache dbcp。您可以使用其中之一。

The only thing you have to worry about is a clean shutdown of your process. You can do that by implementing your own shutdown hook.

您唯一需要担心的是彻底关闭流程。您可以通过实现自己的关闭挂钩来做到这一点。

回答by opyate

If your app is not a web app, you can use any of the non-web specific application contexts listed under All Known Implementing Classeshere. You can then init the context from a main method in a runnable jar.

如果您的应用程序不是 Web 应用程序,您可以使用此处列出的所有已知实现类下的任何非 Web 特定应用程序上下文。然后,您可以从可运行 jar 中的 main 方法初始化上下文。

回答by magulla

You shouldn't use tomcat or anything else. Spring is container already. Init spring in one simple thread, makes sure it has proper clean up flow. and that's all. I used to work on several server side integration application which do allot, communicate over different protocols to other server, and everything was easily done with out Web Containers or J2ee Application Servers. Spring have support for almost everything, sometimes with 3d party libs(caching, transactions, pools, etc ....) Simplified version could be like :

你不应该使用 tomcat 或其他任何东西。Spring 已经是容器了。在一个简单的线程中初始化 spring,确保它有适当的清理流程。就这样。我曾经在几个服务器端集成应用程序上工作,这些应用程序做分配,通过不同的协议与其他服务器通信,一切都可以轻松完成,无需 Web 容器或 J2ee 应用程序服务器。Spring 支持几乎所有内容,有时支持 3d 方库(缓存、事务、池等......)简化版本可能如下:

...
pubcic static  void main (String args[]){
 Server.server = new Server(...);
 server.initSpringContext()
 server.keepAlive();
 server.cleanupResources();
}

..
abstract class Server{

abstract void initSpring();
abstract void cleanUpResources();
abstract void shutdown(){
  this.state = STOP;
};
public void keepAlive()
 while(state!=STOP){
  sleep(1000)
 }

}

回答by ssedano

One of the reasons to deploy the application in tomcatis that it will provide you all of the connection burden, thread management and so on. Nothing that you could not implement yourself. But bear in mind that tomcatis robust, and they already deal with all of the troubles of implement that logic.

部署应用程序的原因之一tomcat是它将为您提供所有连接负担、线程管理等。没有什么是你自己无法实现的。但请记住,这tomcat是健壮的,他们已经处理了实现该逻辑的所有麻烦。

Besides of that there is little point in use an application container (if you think that not having to develop and maintain that amount of code is easy).

除此之外,使用应用程序容器没有什么意义(如果您认为不必开发和维护大量代码很容易)。