Wednesday, August 5, 2020

How to build a helloworld springboot application in 5 minutes

We assume you have maven installed and know how to run basic command such as "mvn clean package". We also assume you have eclipse with maven plugin installed.

Now our goal is to build a spring application that can print out "hello springboot" in 5 minutes.

Ready?

step 1, Let's build an empty springboot project with spring io. Spring provided an online tool which will create the folder structures, pom file with artifact/group, maven dependencies, plugins, parent pom ready. It will also generate a dummy application.properties, springboot application main class file for us for free. Anyway, who want to write all those boiling plates for the helloworld program?

click generate to create a springboot application


  • go to https://start.spring.io/ fill in the group and artifact, click generate. 
  • once the website generated your spring boot code then downloaded to your local and unzip it.
  • open a shell command window, go to your unzipped directory, issue the following command

springboot>cd Downloads/mydemo
springboot>mvn clean package
springboot>tree

The maven will download all the dependencies specified in the pom.xml and have the project compiled.

step 2. import the project into eclipse. We will run the springboot application, make sure it is start without exceptions. 
import existing maven project

  • click File -> Import.. 
  • under Maven, select Existing Maven Project
  • navigate to the unzipped folder and click open
Step 3. Run the springboot. The entry point of spring boot is nothing but a main method.

package my.example.com.mydemo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MydemoApplication {

public static void main(String[] args) {
SpringApplication.run(MydemoApplication.class, args);
}

}

We right click the file, then select Run As Java Application.
We just ran the famous springboot application. Nothing special, the output should looks like the following.


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.0.RELEASE)

2020-06-02 14:48:47.315  INFO 5407 --- [           main] my.example.com.mydemo.MydemoApplication  : Starting MydemoApplication on Meifangs-MBP.home with PID 5407 (/Users/homenetwork/Downloads/mydemo/target/classes started by homenetwork in /Users/homenetwork/Downloads/mydemo)
2020-06-02 14:48:47.319  INFO 5407 --- [           main] my.example.com.mydemo.MydemoApplication  : No active profile set, falling back to default profiles: default
2020-06-02 14:48:47.945  INFO 5407 --- [           main] my.example.com.mydemo.MydemoApplication  : Started MydemoApplication in 1.091 seconds (JVM running for 2.206)

Step 4. print hello springboot.

The default main method did many things though appears doing nothing. The spring framework is started, it looking for the @SpringBootApplication annotation then register this class as the entry point.  Next, we will add a springboot scheduler, so that it keep printing "hello springboot" every 10 seconds.

  • modify the MydemoApplication.java, at the class level add the @EnalbeScheduling annotation, then add a method with annotation @scheduled(fixedRate=5000)
package my.example.com.mydemo;

import java.time.LocalDateTime;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@SpringBootApplication
@EnableScheduling
public class MydemoApplication {

public static void main(String[] args) {
SpringApplication.run(MydemoApplication.class, args);
}
@Scheduled(fixedRate = 5000)
void updateStatus() {
    System.out.println("hello springboot at " + LocalDateTime.now());
}
}

Run the program again. This time, the spring boot will print hello springboot every 5 seconds until you stop it. The annotation @EnableScheduling tells the spring framework that this springboot application will invoke springboot schedulers to run periodic tasks. With this information, the spring framework (imaging spring framework as a magician and your annotations are spells) then searches methods with annotation @scheduled, the annotated method will be the task run periodically. The @scheduled annotation may also have information such as period, delay etc. The spring framework will read in those information, and schedule the task when the spring boot application start.

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.0.RELEASE)

2020-06-02 15:00:10.434  INFO 5424 --- [           main] my.example.com.mydemo.MydemoApplication  : Starting MydemoApplication on Meifangs-MBP.home with PID 5424 (/Users/homenetwork/Downloads/mydemo/target/classes started by homenetwork in /Users/homenetwork/Downloads/mydemo)
2020-06-02 15:00:10.437  INFO 5424 --- [           main] my.example.com.mydemo.MydemoApplication  : No active profile set, falling back to default profiles: default
2020-06-02 15:00:11.194  INFO 5424 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
2020-06-02 15:00:11.236  INFO 5424 --- [           main] my.example.com.mydemo.MydemoApplication  : Started MydemoApplication in 1.284 seconds (JVM running for 2.292)
hello springboot at 2020-06-02T15:00:11.239
hello springboot at 2020-06-02T15:00:16.213
hello springboot at 2020-06-02T15:00:21.213

Step 5. add a little bit seasonings to the hello springboot.

We can make the helloworld program a little bit tasty by making it configurable. For example, we can set the time interval as a variable, then put it in a configuration file, so that we don't have to change the java class if we decided to use a shorter or longer interval later.
application.properties

  • set the properties file. Open application add content 
statusLoader.delay=PT10S
  • modify the main class.
package my.example.com.mydemo;

import java.time.LocalDateTime;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

@SpringBootApplication
@EnableScheduling
public class MydemoApplication {

public static void main(String[] args) {
SpringApplication.run(MydemoApplication.class, args);
}
@Scheduled(initialDelay=3000L, fixedDelayString="${statusLoader.delay}")
void updateStatus() {
    System.out.println("hello springboot at " + LocalDateTime.now());
}
}
run the program again
This time, the time interval changes to 10 seconds.
You can change it to longer and shorter time interval and observe the changes. 

The spring framework by default load any properties defined in the application.properties file. When a variable is referenced as a meta data in an annotation, "${statusLoader.delay}, spring framework will check if any properties in its context matches, then replace the variable with the value string. Now the spell became concrete @Scheduled(initialDelay=3000L, fixedDelayString=PT10S), the spring framework can create a scheduler thread under the sleeve, run every 10 seconds with 3 seconds initial delay. The scheduled threadpool has similar effect as the following code:

    Executors.newScheduledThreadPool(1).scheduleAtFixedRate(() 
            -> System.out.println("hello springboot at" + LocalDateTime.now()), 
            3, 10, TimeUnit.SECONDS);

Hope this helps.


No comments:

Post a Comment

Why I stopped publishing blog posts as information provider

Now the AI can generate content. Does that mean the web publishing industry reaches the end? ChatGPT said: ChatGPT Not at all. While AI can ...