Web applications web应用程序启动(spring 3.0)

Web applications web应用程序启动(spring 3.0),web-applications,spring-mvc,startup,Web Applications,Spring Mvc,Startup,我希望在web应用程序启动(加载上下文)后执行一个方法(或某些功能)。我使用Spring3.0作为框架 我尝试使用我在类中实现的ServletContextListener 我的听众课 package myapp.listner; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; public class MyContextListner implements Servl

我希望在web应用程序启动(加载上下文)后执行一个方法(或某些功能)。我使用Spring3.0作为框架

我尝试使用我在类中实现的ServletContextListener

我的听众课

package myapp.listner;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

public class MyContextListner implements ServletContextListener{

    public static long appStart=0L;
    @Override
    public void contextDestroyed(ServletContextEvent arg0) {

        System.out.println("Context Destroyed");
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        appStart=System.currentTimeMillis();
        System.out.println("Context Initialised");

    }

}
web.xml的一部分是

<listner>
     <listner-class>myapp.listner.MyContextListner</listner-class>
    </listner>

    <servlet>
      <servlet-name>dispatcher</servlet-name>

      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>


    <servlet-mapping>
      <servlet-name>dispatcher</servlet-name>
      <url-pattern>/forms/*</url-pattern>
    </servlet-mapping>

myapp.listner.MyContextListner
调度员
org.springframework.web.servlet.DispatcherServlet
1.
调度员
/形式/*
但这并不是在打印信息


请帮助我……

施工后和施工前注释,类似于以下内容:

@Configuration
public class MyConfig{

    @PostConstruct
    public void contextInitialized(){
      System.out.println("Context Initialised");
    }

    @PreDestroy
    public void contextdestroyed(){
      System.out.println("Context Destroyed");
    }
}

您不必在配置bean中使用,它可以是。

后构造和PreDestroy注释,类似于以下内容:

@Configuration
public class MyConfig{

    @PostConstruct
    public void contextInitialized(){
      System.out.println("Context Initialised");
    }

    @PreDestroy
    public void contextdestroyed(){
      System.out.println("Context Destroyed");
    }
}

您不必在配置bean中使用,它可以。

它是否需要在任何xml文件中输入lass MyConfig。。。。。如果不是,那么MyConfig类是如何定位的?不,只要扫描注释,就可以将其放在任何您喜欢的地方—这是应用程序上下文中的设置。此外,它不必在配置bean中,您可以将它放在任何spring管理的类中。它是否需要在任何xml文件中输入lass MyConfig。。。。。如果不是,那么MyConfig类是如何定位的?不,只要扫描注释,就可以将其放在任何您喜欢的地方—这是应用程序上下文中的设置。此外,它不必在配置bean中,您可以将它放在任何spring管理的类中?“listener”的拼写错误是故意的吗,特别是在
web.xml
中?最好是剪切和粘贴示例,以避免弄虚作假。这对我来说很好。虽然我倾向于面向Spring的解决方案,但你确定你找对了地方吗?“listener”的拼写错误是故意的吗,特别是在
web.xml
中?最好是剪切和粘贴示例,以避免弄虚作假。这对我来说很好。