在Apache Axis 2启动时加载类方法

时间:2011-08-16 06:56:22

标签: java axis2

我需要在Apache Axis 2启动时在Java类中执行静态方法,或者可以在应用程序范围内执行某些操作。

请建议。

1 个答案:

答案 0 :(得分:1)

您可以实现javax.servlet.ServletContextListener并将其添加到部署描述符(web.xml):

<listener>
    <listener-class>your.pack.age.path.YourServletContextListener</listener-class>
</listener>

在加载servlet上下文后立即调用contextInitialized方法,这样就可以将静态方法调用放在里面了。

另一种方法是扩展Axis2的Servlet并在那里进行初始化。

web.xml中,您将Axis2 Servlet替换为您自己的:

<servlet>
  <servlet-name>Axis2Servlet</servlet-name>
  <servlet-class>your.pack.age.path.YourAxis2Servlet</servlet-class>
  <load-on-startup>1</load-on-startup>
</servlet>

您的Servlet:

package your.pack.age.path;
import org.apache.axis2.transport.http.AxisServlet;

public class YourAxis2Servlet extends AxisServlet {

  public void init(ServletConfig config) throws ServletException {
    super.init(config);
    // your initialization code here
    //...
  }  
  //...
}