【问题标题】:Use Spring in a WAR在 WAR 中使用 Spring
【发布时间】:2012-09-07 11:33:39
【问题描述】:

我用 Spring 3 TaskScheduler 做了 jar 应用程序。我用 main 方法运行这个应用程序:

public static void main(String[] args) {
  GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
  ctx.load("classpath:scheduler-app-context.xml");
  ctx.refresh();
  while (true) {
    // ...
  }
  // ...
}

是否可以在 web 应用程序(war 文件)中运行这个 jar,main 方法?如何在 web.xml 中运行它。

非常感谢

【问题讨论】:

    标签: spring scheduled-tasks


    【解决方案1】:

    在您的 web.xml 中执行类似操作:

    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:scheduler-app-context.xml</param-value>
    </context-param>
    
    <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    

    这将从 XML 文件中实例化您的 Spring 上下文。因此,不必像在 main 方法中那样手动执行此操作。

    【讨论】:

      【解决方案2】:

      如果你在战争中需要简单的调度器(使用spring框架),你也可以这样做:

      (在 Spring 中,“@PostConstruct”将初始化调度程序 - 所以不需要 main 方法)

          @Component
          public class Scheduler {
      
              private static final Logger LOG = LoggerFactory.getLogger(Scheduler.class);
      
      
              @PostConstruct
              private void initializeTenSecSchedule() {
      
                  final List<Runnable> jobs = new ArrayList<Runnable>();
      
                  jobs.add(doSomeTestLogs());
                  jobs.add(doSomeTestLogs2());
      
                  final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(jobs.size());
      
                  for(Runnable job : jobs){
      
                      scheduler.scheduleWithFixedDelay(job, 10, 10, SECONDS);
      
                  }
      
              }
      
              /**
               * ---------------------some schedule tasks--------------------------
               */
      
              private Runnable doSomeTestLogs(){
      
                  final Runnable job = new Runnable() {
                      public void run() { 
      
                          LOG.debug("== foo SCHEDULE a", 1);
                          System.out.println("Method executed at every 10 seconds. Current time is :: "+ new Date());
      
                      }
                  };
      
                  return job;
      
              }
      
              private Runnable doSomeTestLogs2(){
      
                  final Runnable job = new Runnable() {
                      public void run() { 
      
                          LOG.debug("== foo SCHEDULE b", 1);
                          System.out.println("Method executed at every 10 seconds. Current time is :: "+ new Date());
      
                      }
                  };
      
                  return job;
      
              }
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-03
        • 2014-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-02
        相关资源
        最近更新 更多