I know this question is a duplicate of Execute method on startup in spring. however I have tried the advice posted in the accepted answer for that question, and nothing has worked for me. As such, I suspect that although it is the same question being asked here, I strongly feel that the root cause is different and thus requires a different answer/solution.
I am trying to get Spring to create a bean at start up and immediately execute one of its methods.
My spring config (heartbeat-config.xml
):
<beans (all the xmlns stuff here ommitted for brevity)>
<bean id="heartbeat" class="org.me.heartbeat.Heartbeat"/>
</beans>
And Heartbeat.java
:
public class Heartbeat
{
@PostConstruct
public void start()
{
System.out.println("I should see this message in the logs somewhere!!");
}
}
And finally, my web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<!-- The display name of this web application -->
<display-name>Heartbeat</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/heartbeat-config.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
When I run this in Tomcat I don't get any startup errors. Tomcat looks like its running healthy (as I can tell from the logs). I do not, however, see any output in the logs that should be generated by the System.out
invocation in my start()
method (Tomcat redirects all standard output to its log files).
Am I overlooking something here? Is there an obvious diagnosis I could be performing?