For some reason, the method is performed twice in all ways!

Prompt other ways to automatically execute the method when the program starts. Or if anyone knows, tell me what could be the problem of double launch of the method?

Option 1:

@Service public class RunJob implements InitializingBean { public void afterPropertiesSet() { System.out.println("JOB RUN"); } } 

Option 2:

 public class RunJob { @PostConstruct public void initRun() { System.out.println("JOB RUN"); } } 

Option 3:

mvc-servlet.xml :

 <bean id="runJobBean" class="com.java.myproject.util.RunJob" init-method="init"></bean> public class RunJob { @Override public void init() { System.out.println("JOB RUN"); } } 
  • The problem with the double launch method has not solved. But since my method started the job, I just made a check whether the job was started and solved my problem this way. - Giovanni

1 answer 1

use the init method in the XML description. and there you specify what method to call. this method should be static if I'm not mistaken

This method will be called by the name of init when the bean is initialized.

  • <beans> <bean id = "myBean" class = "..." init-method = "init" /> </ beans> - Senior Pomidor
  • Sorry, can't insert XML in response - Senior Pomidor
  • Thank you, I tried, unfortunately, this is also how the method runs twice! - Giovanni
  • is it possible to describe XML or notation? then you initialize it 2 times - Senior Pomidor
  • Sahaka "means you initialize it 2 times" - did not understand why? For XML: <bean id="runJobBean" class="com.java.myproject.util.RunJob" init-method="init"></bean> For anotation: @PostConstruct public void init() { System.out.println("RunJob!"); } @PostConstruct public void init() { System.out.println("RunJob!"); } - Giovanni