Help to understand what is happening! I create a simple Spring-boot application.

There is only one class:

@Component public class Example { @Scheduled(fixedRate = 5000) private void myMethod() { System.out.println("Hello!"); } } 

in context.xml specify the bin:

 <bean class="com.example.Example"/> 

PROBLEM At the start of the project, as I understand it, objects of all the bins specified in context.xml are created and, accordingly, the Task @Scheduled(fixedRate = 5000) automatically started (This launch usually does not happen if you remove the bin from context.xml ).

After the project is launched, the @Scheduled(fixedRate = 5000) Task @Scheduled(fixedRate = 5000) works as it is supposed to work. And it turns out at the exit two greetings:

 Hello! Hello! 

The bean must be in context.xml . he participates in auto-linking ( @Autowired ) in another class.

Why it happens?

Should I avoid adding the Task Scheduler to the methods of the class specified in ContextApplication? Maybe you should create a separate class for working with buildings and call from it all that is necessary?

  • one
    > The bean must be in context.xml because he participates in auto-linking (@Autowired) in another class. For this purpose, the annotation @Component (which you already have) and the autoscan enabled is sufficient. - Nofate
  • @Nofate thanks! I understood the mistake - Alexey

0