如何在Java代码中检测应用程序类型
假设我们有一个 java 包。这个包可以在任何地方使用。但是,此包中的一些代码与上下文相关。例如,如果使用此包的应用程序是一个 Web 应用程序,我们需要通过调用一个函数来执行一些任务,而如果该应用程序是一个控制台应用程序,则需要通过调用相同的函数来执行其他任务。
这是我的问题:
在java中是否有任何方法可以在代码中检测应用程序是作为网络应用程序还是控制台运行? 我感谢任何帮助:)
作为一个现实世界的例子,我们加载属性文件的方式对于 Web 和控制台应用程序是不同的。
对于 Web 应用程序,我们可能使用 this.getClass().getClassLoader().getResourceAsStream(url)
,对于控制台应用程序,我们使用 new FileInputStream(physical path)
。
Imagine we have a java package. This package can be used anywhere. However, there are some codes in this package which are context dependant. For instance, if the application which uses this package is a web app we need to perform some tasks by calling a function while performing other tasks if the application was a console application by calling the very same function.
Here is my question:
Is there any way in java that within the code we can detect if the application was run as a web app or a console?
I appreciate any help :)
As a real world example, the ways we load properties files are different for web and console applications.
For web applications we probably use this.getClass().getClassLoader().getResourceAsStream(url)
and for console apps we use new FileInputStream(physical path)
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好在某处设置构建属性,而不是尝试检测您的应用程序类型,因为我认为没有可靠的方法可以做到这一点。
此外,您不应该尝试检测应用程序类型,因为您的视图层(Web、桌面或控制台)应该根据现代架构原则轻松互换。
回应你的最后一条评论。
正如 user384706 所说,IMO DI 是正确的选择。
我将举一个 Spring 的例子。
在您的控制台和 Web 应用程序部分中,您可以具有:
对于
PropertyProvider
的不同实现,您的loadProperties()
方法将被覆盖。在 Spring 上下文中,您可以拥有:
以及
WebService
和WebPropertyProvider
的同一对 bean 定义。It might be better to set a build property somewhere rather then trying to detect your application type, because I don't think there is a reliable way to do that.
Moreover you shouldn't try to detect application type because your view layer (either web, desktop or console) should be easily interchangeable according to modern architectural principles.
In response to your last comment.
As user384706 said DI is the correct choice here IMO.
I will give an example with spring.
In both your console and web app parts you can have:
Where your
loadProperties()
method would be overriden for different implementations of yourPropertyProvider
.And in your spring context you can have:
And the same pair of bean definitions for your
WebService
andWebPropertyProvider
.只需使用依赖注入
只需放置所有适当的参数即可通过设置器配置您的库,并让容器或应用程序使用 DI 进行相应配置。
所以你不需要任何检查,恕我直言,这是一个不好的方法
Just use dependency injection
Just place all the appropriate parameters to configure your library via setters and let the container or application configure it accordingly using DI.
So you will not need any checks which IMHO is a bad approach
J2EE 方式。如果您的包在 Web 应用程序中使用,那么我假设您位于 J2EE 容器中,那么您可以添加 部署时命名中的参考。您还可以注册一个 MDB,它可以侦听对此引用的更改并在运行时修改代码的行为。不错吧?
传递调用者上下文的其他标准方式是通过参数化工厂或通过属性。
一种非标准方法 - 有趣的是获取堆栈跟踪 并查找调用者是谁或查找 j2ee 上下文等。
The J2EE way. If your package is used in a web application then, I am assuming that you are in a J2EE container then, you can add a Reference in the naming at deploy time. You can also register an MDB that can listen to the changes to this reference and modify behavior of your code at runtime. Sweet right?
Other standard way in which you can pass the context of the caller is through a parametrized Factory or through properties.
One non-standard way - for fun is to get the stack trace and look for who the caller is or look for j2ee context etc.