Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to start a daemon using Spring injection from a webapp
    primarykey
    data
    text
    <p>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:</p> <p>In web.xml</p> <pre><code>... &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt;/WEB-INF/springapp-servlet.xml&lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;listener&gt; &lt;listener-class&gt;example.AppListener&lt;/listener-class&gt; &lt;/listener&gt; </code></pre> <p>AppListener.java</p> <pre><code>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); } } </code></pre> <p>SessionServiceDaemon.java</p> <pre><code>@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; } </code></pre> <p>My springapp-servlet.xml simply has the packages required for the injection:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans ... &lt;context:component-scan base-package="example" /&gt; &lt;mvc:annotation-driven /&gt; &lt;/beans&gt; </code></pre> <p>In the startup logs, I see as expected:</p> <pre><code>+++++++++++++++++++++++++++++++ SessionServiceDaemon injected </code></pre> <p>followed by</p> <pre><code>======================= Begin context init ======================= </code></pre> <p>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:</p> <pre><code>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 </code></pre> <p>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:</p> <pre><code> 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); } </code></pre> <p>and that printed the content of springapp-servlet.xml just fine.</p> <p>My 2 questions:</p> <ul> <li>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?</li> <li>Do I have the correct approach anyway for starting a Daemon that has dependencies that are injected?</li> </ul> <p>PS: I use Tomcat.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload