I will decipher this page.
environment sts 3.8.4 Launch sts and search for the target project from "File"-> "New"-> "Import Spring Getting started content". I have imported it and confirmed its operation.
Background I was learning Spring boot, but I decided to read the original document without relying on commercial books. It's all in English, so I'm struggling to read it.
Java skills About Oracle Java8 Silver
I am writing it as a memorandum for learning. If you have any advice, such as points to be corrected or supplements, please do not hesitate to contact us.
ScheduledTasks.java
package hello;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Component
public class ScheduledTasks {
private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
@Scheduled(fixedRate = 5000)
public void reportCurrentTime() {
log.info("The time is now {}", dateFormat.format(new Date()));
}
}
First of all, from this code.
By setting @Component
, it can be used in the Main class of Application.java to be created next. The Main class must be annotated with @ SpringBootApplication
(including @ComponentScan
). This will allow you to automatically find the components in the same package. You will also be able to use the components you create here.
This class creates a mechanism to display the log on the console every 5 seconds.
private static final Logger log = LoggerFactory.getLogger(ScheduledTasks.class);
Get the logger at.
LoggerFactory is a logging library created by the developers of Log4j.
Set a Class object of a class that uses a logger, such as ScheduledTasks.class
, as an argument.
This allows log.info () to be displayed on the console by default.
If you annotate @Scheduled (fixedRate = 5000)
, the method will be executed periodically. By the way, if you annotate a method with arguments, an exception will occur. By specifying @EnableScheduling
in the file to be created next, the method with @Scheduled
will be executed.
SimpleDateFormat and Date are omitted.
Next, let's look at the code of application.java that has the main method.
Application.java
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
}
This code is annotated with @ SpringBootApplication
. This reads @Configuration`` @EnableAutoConfiguration
@EnableWebMvc`` @ComponentScan
as a set.
@ComponentScan
will find the ScheduledTasks class you just created. With @EnableScheduling
, the reportCurrentTime () method annotated with@Scheduled (fixedRate = 5000)
will be executed periodically. Annotations seem to allow for more complex scheduling by using @Scheduled (cron =" ")
.
In sts, if you click the project name and press the play button, it will automatically compile and execute.
Logs will be added to the console every 5 seconds.