简体   繁体   English

Java EE Web App插件架构

[英]Java EE Web App Plugin Architecture

I have a simple Web App that receives data via a web service and persists it in a database. 我有一个简单的Web应用程序,它通过Web服务接收数据并将其保存在数据库中。 I am using CXF, Hibernate and Glassfish 3. I am very new to all of this, however I am trying to work out how I can develop a simple plugin model so that when the App fisrt starts up it searches for plugin's in a folder and calls an instantiation(?) class for each plugin. 我正在使用CXF,Hibernate和Glassfish 3.我对这一切都很陌生,但是我想弄清楚如何开发一个简单的插件模型,这样当App fisrt启动时它会搜索文件夹中的插件并且为每个插件调用一个实例化(?)类。 The issue is that I am not really sure how to initiate this process (that is when the App firsts starts) - how do I know when the app first starts. 问题是我不确定如何启动这个过程(即App开始时的第一次) - 我怎么知道应用程序何时首次启动。 Any advice/comments appreciated! 任何建议/意见赞赏!

ok here is a sample class with the modification of web.xml: 好的,这是一个带有web.xml修改的示例类:

package foo.bar.startup;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class AppStartup implements ServletContextListener {

    public void contextInitialized(ServletContextEvent event) {
        // App started - do start up stuff
    }
    public void contextDestroyed(ServletContextEvent event) {
        // App stopped - do something
    }
}




  <listener>
    <listener-class>foo.bar.AppStartup</listener-class>
  </listener>

If you want to know when the app starts / stops, you can implement a ServletContextListener and register it in the web.xml file. 如果您想知道应用程序何时启动/停止,您可以实现ServletContextListener并将其注册到web.xml文件中。

You should look at OSGI - if you need an elaborate plugin model, with support for deploying new plugins at runtime, hot redeployment of plugins etc. 您应该查看OSGI - 如果您需要精心设计的插件模型,支持在运行时部署新插件,热插拔插件等。

When a web application starts, it fires a ServletContextEvent which goes to every registered ServletContextListener . 当Web应用程序启动时,它会触发ServletContextEvent ,该ServletContextEvent将转到每个已注册的ServletContextListener These listeners must be configured in the web.xml of the application: 必须在应用程序的web.xml中配置这些侦听器:

<listener>
    <listener-class>com.foo.bar.MyServletContextListener</listener-class>
</listener>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM