Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9144661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:19:26+00:00 2026-06-17T10:19:26+00:00

I need to start a daemon when I deploy a war. The daemon itself

  • 0

I need to start a daemon when I deploy a war. The daemon itself uses objects that should be injected with Spring. I did the following:

In web.xml

...
<context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/springapp-servlet.xml</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
    <listener-class>example.AppListener</listener-class>
</listener>

AppListener.java

public class AppListener implements ServletContextListener {
...
  @Override
  public void contextInitialized(final ServletContextEvent sce) {
      log.info("======================= Begin context init =======================");
      try {
        // final ApplicationContext context = new ClassPathXmlApplicationContext("WEB-INF/springapp-servlet.xml");
        final ApplicationContext context = new ClassPathXmlApplicationContext("src/main/webapp/WEB-INF/springapp-servlet.xml");
      //final ApplicationContext context = new ClassPathXmlApplicationContext("//Users/.../WEB-INF/springapp-servlet.xml");

      final SessionServiceDaemon sessionServiceDaemon = context.getBean(SessionServiceDaemon.class);
      sessionServiceDaemon.start();
    } catch (final Exception e) {
      log.error("Was not able to start daemon",e);
    }
}

SessionServiceDaemon.java

@Service
@Singleton
public class SessionServiceDaemon {

  private final static Logger log = LoggerFactory.getLogger(SessionServiceDaemon.class);

  private final SessionServiceHandler handler;

  @Inject
  public SessionServiceDaemon(final SessionServiceHandler handler) {
    log.info("+++++++++++++++++++++++++++++++ SessionServiceDaemon injected");
    this.handler = handler;
  }

My springapp-servlet.xml simply has the packages required for the injection:

<?xml version="1.0" encoding="UTF-8"?>
<beans ...

  <context:component-scan base-package="example" />
    <mvc:annotation-driven />

</beans>

In the startup logs, I see as expected:

+++++++++++++++++++++++++++++++ SessionServiceDaemon injected

followed by

======================= Begin context init =======================

Problem is: I then get an exception that the file does not exist no matter which path I use to point to springapp-servlet.xml:

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [src/main/webapp/WEB-INF/springapp-servlet.xml]; nested exception is java.io.FileNotFoundException: class path resource [src/main/webapp/WEB-INF/springapp-servlet.xml] cannot be opened because it does not exist

I tried different relative paths and even the absolute path without success. I even edited the code above and added just above my attempt to load the context the following:

  try {
    log.info(org.apache.commons.io.FileUtils.readFileToString(new File("src/main/webapp/WEB-INF/springapp-servlet.xml")));
  } catch (final Exception e) {
    log.error("Unable to find file",e);
  }

and that printed the content of springapp-servlet.xml just fine.

My 2 questions:

  • How can I get a “class path resource [src/main/webapp/WEB-INF/springapp-servlet.xml] cannot be opened because it does not exist” when I am able to display the file content using the exact same path from the same method?
  • Do I have the correct approach anyway for starting a Daemon that has dependencies that are injected?

PS: I use Tomcat.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:19:27+00:00Added an answer on June 17, 2026 at 10:19 am

    You are starting two different spring application contexts. The first, the built-in ContextLoaderListener, is likely picking up your springapp-servlet.xml configuration from default locations. (You didn’t say if you are specifying a contextConfigLocation.)

    In your custom listener, you then construct a new application context using ClassPathXmlApplicationContext with an explicit path. Of the three lines you’ve shown, only the one with “”WEB-INF/springapp-servlet.xml” looks like a possible candidate for classpath resolution, although it really depends on how you’ve configured and startup your Tomcat instance. (i.e. What is the classpath from Tomcat’s point-of-view?)

    Regardless, there are better ways to get the Spring application context in to a servlet/listener. A direct approach is to use the ContextLoaderListener as you have done, but then in your custom servlet/listener, make use of Spring’s WebApplicationContextUtils.getWebApplicationContext.

    Spring has direct support for servlets as well, including configuration via annotations, the HttpServletBean class, or even using FrameworkServlet directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to control (start\stop\restart) a perl daemon from a web application (php). Daemon
My Problem My core issue is I need to start the gnome-keyring-daemon from c
I need to start up a jetty with an external (possibly remote) .war file
I need to start a Unix process by calling a PHP-page through the web.
I need to start NSTimer by 0:0:0 that I am doing and I have
I need to start a project that will be json-API-centric, which means that most
What's the correct way to create a custom daemon in Ubuntu, that will start
For a website, I need to be able to start and stop a daemon
I have a daemon that launchd runs at system boot (OS X). I need
I have a few servers and other daemons I need to start up in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.