简体   繁体   English

检测是否在servlet容器中运行或独立运行

[英]Detect if running in servlet container or standalone

I have a simple problem: I want to configure an object differently based on whether the object is instantiated within a servlet container, or whether it is instantiated in a stand alone app. 我有一个简单的问题:我想根据对象是否在servlet容器中实例化,或者是否在独立应用程序中实例化来不同地配置对象。

The object is a database connection, and I care about setting query timeouts. 该对象是数据库连接,我关心设置查询超时。

The first solution that I can come up with is: 我能想出的第一个解决方案是:

if (insideServletContainer(this.getClass().getClassLoader()) { 
  /// do some servlet specific config
}
else {
 /// do some standalone config
}

The question is, of course, can I write a reliable method of telling whether the class was loaded within a servlet container. 当然,问题是我是否可以编写一个可靠的方法来判断该类是否已在servlet容器中加载。 It feels like a hack at best. 它充其量只是一种黑客行为。

The second option is to assume that the default case is a stand alone instantiation, set defaults based on stand-alone configuration, and override them within the servlet context. 第二种选择是假设默认情况是独立实例化,基于独立配置设置默认值,并在servlet上下文中覆盖它们。

So, to sum up my question is : Do you know of a good/reliable mechanism if the class was loaded from within a servlet container? 所以,总结一下我的问题是如果从servlet容器中加载了类,你知道一个好的/可靠的机制吗? If not, I will have to take the second route. 如果没有,我将采取第二条路线。

Nick 缺口

This seems like a really bad idea. 这似乎是一个非常糟糕的主意。 Instead, why don't you allow the class to take parameters, then let the container or app configure it appropriately? 相反,为什么不允许类接受参数,然后让容器或应用程序适当地配置它?

Setting aside whether or not this is a good idea, I'd suggest looking up java:comp/env, which is only going to be available in an EE server: 不管这是不是一个好主意,我建议查找java:comp / env,它只能在EE服务器中使用:

try {
  new InitialContext().lookup("java:comp/env");
  /// do some servlet specific config
} catch (NamingException ex) {
  /// do some standalone config
}

An alternate way to do this sort of thing is to have the configuration injected into this class by some sort of bootstrap loader. 执行此类操作的另一种方法是通过某种引导加载程序将配置注入此类。

In a standalone version, this would be done by the main() method (or something called from it). 在独立版本中,这可以通过main()方法(或从它调用的东西)来完成。

In a webapp version, this would be done by a listener or filter invoked configured within the web.xml . 在webapp版本中,这可以通过在web.xml配置的侦听器或过滤器来完成。

Dependency injection is useful here as it removes the need for your application to check these sorts of things; 依赖注入在这里很有用,因为它不需要你的应用程序检查这些东西; instead the application is given what it needs. 相反,应用程序被给予它所需要的东西。

I would recommend Dependency Injection like @matt b. 我会推荐像@matt b这样的依赖注入。

As a second option, if it is only the simple case you described and you don't want to add or learn a DI framework to support this feature. 作为第二种选择,如果只是您描述的简单案例,并且您不想添加或学习DI框架来支持此功能。 You can accomplish the same thing as your current code by using a properties file to load different value based on the environment. 通过使用属性文件根据环境加载不同的值,您可以完成与当前代码相同的操作。 You can simply use a different file for each environment and supply a VM arg to indicate which environment you are running. 您可以简单地为每个环境使用不同的文件,并提供VM arg以指示您正在运行的环境。

db_prop.dev
db_prop.stalone
dp_prop.int
db_prop.prod

Then you can load by resource 然后你可以按资源加载

"db_prop." + System.getProperty("runtime.env")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在servlet的doGet()仍在运行时检测J2EE容器停止事件 - Detect J2EE container stop event while a servlet's doGet() is still running 独立 Servlet 5 容器中的 JAX-RS (Jersey) API 无法启动 - JAX-RS (Jersey) API in standalone Servlet 5 container does not start Servlet容器中的Java Web应用程序与独立版本 - Java web application in a Servlet container vs. standalone Groovy Grails 独立应用程序,没有 tomcat 或任何其他 servlet 容器 - Groovy Grails standalone application without tomcat or any other servlet container 如何检测是否在osgi容器中运行 - how to detect if running in osgi container 运行spring + hibernate应用程序而没有实际的servlet容器 - Running spring + hibernate application without actual servlet container 在tomcat容器中运行的Java servlet中使用“环境”变量 - using a 'environment' variable in java servlet running in tomcat container Java Servlet中的数据表独立编辑器 - Datatables Standalone Editor in Java Servlet 独立守护程序还是应用程序容器? - Standalone Daemon or App Container? Docker-compose - 为运行 spring boot 独立应用程序的 docker 容器提供基于 XML 的配置 - Docker-compose - Providing XML based configuration to docker container running spring boot standalone application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM