Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring在服务器启动时自动连接类_Java_Spring - Fatal编程技术网

Java Spring在服务器启动时自动连接类

Java Spring在服务器启动时自动连接类,java,spring,Java,Spring,我有一个spring应用程序。我是自动布线班的学生,他们工作得很好。 例如 但现在我有一个名为ScheduleServlet的服务器上启动类,它使用init方法来调度某些内容 public class SchedulerServlet extends HttpServlet { @Override public void init(ServletConfig config) throws ServletException { super.init(config);

我有一个spring应用程序。我是自动布线班的学生,他们工作得很好。 例如

但现在我有一个名为ScheduleServlet的服务器上启动类,它使用init方法来调度某些内容

public class SchedulerServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {

    super.init(config);
    this.LOGGER.info("timer servlet is initialized  ");
    try {
        InitialContext ic = new InitialContext();
        TimerManager tm = (TimerManager) ic.lookup("java:comp/env/tm/TimerManager");
        Timer timer = tm.schedule(new GlobalTemplateScheduler(), 0, 3600000);// one hour interval
        System.out.println("Timer..... " + timer);
    }
...
在本文中,我的GlobalTemplateScheduler类具有TimerSpired方法,该方法计划在每一小时间隔后执行

public class GlobalTemplateScheduler implements TimerListener {

    @Autowired
    private TemplateControl templateControl;

    @Override
    public void timerExpired(Timer timer) {
        try {
            templateControl.updateMappings(names);
        } catch (Exception e) {
            this.LOGGER.error(e.getMessage());
            e.printStackTrace();
        }
...
所以我必须自动连接templateControl,我得到的是null。这应该在服务器启动时发生

在updateMappings内部还有一个datasource对象,它也是作为构造函数arg自动连接的(这在浏览器请求上运行良好,但需要在服务器启动时执行)

注意:我不能使用ApplicationListener接口

任何建议都会很有帮助


谢谢。

应用程序启动时,bean初始化不会完成,可以在应用程序上下文刷新后或bean初始化后使用bean,除非检测bean是否准备就绪,否则执行启动时需要bean的逻辑是没有意义的


您可以在bean中使用@PostConstruct执行一些逻辑,这些逻辑将在bean初始化之后执行,这样您就可以在bean初始化之后以某种方式操作您的逻辑,或者您也可以在ContextRefreshedEvent之后通过执行applicationListener来检测和执行逻辑,并将您的逻辑放入OnApplicationEvent方法。

一种解决方案是在servlet中使用Spring的容器。有许多用于此目的,例如AnnotationConfigApplicationContext。Spring的文档描述了如何使用

假设GlobalTemplateScheduler也是一个bean,那么关键点是:

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

GlobalTemplateScheduler sheduler = context.getBean("sheduler", GlobalTemplateScheduler.class);

sheduler.someMethod();
ClassPathXmlApplicationContext使用的XML内容很小。但您需要启用组件扫描:

<context:component-scan base-package="foo.bar.baz" />

我可以建议,另一种方法是使用Spring的DispatcherServlet将所有bean连接在一起。它可以使用相同的XML,只需加载它即可。好处是您不需要自己加载应用程序上下文并启动bean作为入口点

有很多关于如何使用这个servlet的教程


如果您不喜欢编写XML,可以使用

,正如我所说的,我正在自动连接的bean工作正常。我只需要在我的调度器Servlet中使用这些bean

这是有效的解决方案

在我的调度器servlet中,我获得了应用程序上下文xml,并使用了所需的bean

ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
GlobalTemplateControl globalTemplateControlObject = context.getBean("globalTemplateControl", GlobalTemplateControl.class);

感谢@duffymo@Amer Qarabsa@spindizy的建议:)

您可以通过调用new来实例化您的GlobalTemplateScheduler。这意味着它在你的控制之下,而不是Spring的控制之下。正如您已经发现的,自动关联注释在这种情况下没有任何效果。您必须制作GlobalTemplateScheduler和SchedulerServletSpringbeans并对它们进行注释。只是要小心:如果这个应用程序在集群中运行,那么每个实例将有一个“全局”调度器。
ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
GlobalTemplateControl globalTemplateControlObject = context.getBean("globalTemplateControl", GlobalTemplateControl.class);