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 7430903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:12:47+00:00 2026-05-29T09:12:47+00:00

I know this question is a duplicate of Execute method on startup in spring

  • 0

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?

  • 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-05-29T09:12:48+00:00Added an answer on May 29, 2026 at 9:12 am

    The most simple way to do that is to forget about annotations and change your bean definition to:

    <bean id="heartbeat" class="org.me.heartbeat.Heartbeat" init-method="start"/>
    

    If you want annotations you need to declare the context namespace and put this int your applicationContext.xml:

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
        <!-- Enable @PostConstruct, @PreDestroy and friends in Spring -->
        <context:annotation-config/>
    
        <bean id="heartbeat" class="org.me.heartbeat.Heartbeat"/>
    </beans>
    

    Note: check the namespaces, they are copypasted from the net.

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

Sidebar

Related Questions

I know this is a duplicate kind of question. I have worked with ASP.Net
This may be a duplicate question, but really don't know how else to word
I know this question has been done but I have a slightly different twist
I know this question is not really important.. however I've been wondering: Which of
Ok, I know this seems like a duplicate question, but don't think it is.
I know this question could have passed a few times here but I haven't
I know there is a seemingly exact duplicate of this question here: iPhone SDK:
I would like to apologize if the duplicate of this question exist. i tried
I know this question may be marked as a duplicate of difference between malloc
I know this is a duplicate question. but i want know it with an

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.