jboss启动时如何实例化一个类 [英] how to instantiate a class while jboss startup

查看:95
本文介绍了jboss启动时如何实例化一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在启动JBOSS 5时实例化自己的Java类(仅一次),并且我将使用该对象,直到我关闭jboss.

I would like to instantiate my own java class (One time only) when the time of startup of JBOSS 5 and i will use that object until i shut down the jboss.

如何实例化

推荐答案

您可以使用

You can implement your class with the ServletContextListener interface , which make your class able to receive notifications from the application server (i.e JBoss) when it starts and shut-downs .

例如:

public class MyServletContextListener implements ServletContextListener {

            /**This method will run when the web application starts***/
           public void contextInitialized(ServletContextEvent sce) {
            /**Put your codes inside , it will run when JBoss starts ***/
           }

}

然后在web.xml中注册MyServletContextListener:

<?xml version="1.0"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <listener>
             <listener-class>com.abc.xyz.MyServletContextListener </listener-class>
    </listener>

</web-app> 

WAR格式打包应用程序,然后将其部署到JBoss中.当JBoss启动时,MyServletContextListener中的contextInitialized()也将运行.

Package the application in the format of WAR and deploy it into the JBoss. When the JBoss starts , the contextInitialized() in the MyServletContextListener will run too.

这篇关于jboss启动时如何实例化一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆