Hello World with Spring Boot, so it's a procedure memo. Completed in Eclipse. It takes about 30 minutes.
Java7 Eclipse4.5 MARS Elipse plug-in version STS 3.8.3
Please have STS installed in Eclipse. You don't need Tomcat.
File → New → Other → Spring Starter Project Name "bootsample" Group "com.bootsample" Deliverable: "bootsample" Description: "Spring Boot Sample Project" Package: "com.bootsample"
Other than that, next by default → Check Web and complete
①pom.xml
No change Make sure that it looks like this:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bootsample</groupId>
<artifactId>bootsample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>boot</name>
<description>SpringBoot sample Project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Right-click on the boot project and do the following: ・ Maven → Project update → OK ・ Execute → Maven install
In the "com.bootsample" package in src / main / java Confirm that the Java class file "BootApplication.java" is created.
BootApplication.java
package com.bootsample;
import org.springframework.web.bind.annotation.SpringApplication;
@SpringBootApplication
public class BootApplication {
public static void main(String[] args){
SpringApplication.run(BootApplication.class,args)
}
}
@SpringBootApplication is an important annotation that makes you recognize "I am a SpirngBoot project". Be sure to leave the main method as it will trigger the application to start.
In the "com.bootsample" package in src / main / java Create a Java class file "HelloController.java". Please describe as follows.
HelloController.java
package com.bootsample;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping(value="/")
private String hello(){
return "Hello world!!";
}
}
Make the class recognized as a controller by adding @RestController. @RequestMapping will be the URL at the time of access. This time, I access it with localhost: 8080 /, so I only use /.
Right-click on the project and click Run → Spring Boot App to launch the application. If a character string such as ** Started Boot Application ** appears on the console, the startup is successful. Let's go to the world today. http://localhost:8080/
From the second time onward, you can also start and stop the server with the server start and stop buttons on the toolbar.
Java programs tend to get caught up in xml hell and library reincarnation, but Spring Boot is really easy to get rid of this area.
** Thymeleaf ** that can be converted to JSP as HTML file, ** Spring Loaded ** that reflects the source at high speed, ** SprintBootActuator ** that can get the application status via WebAPI (localhost: 8080 / env) Spring selection convenient tools such as (usable) are collected. If Spring pom is described for each part, Spring Boot allows you to receive the library as pom all-in-one for each function. Of course, you can also pom individual parts.
As a general rule, the Spring Boot library gets the latest library ** at that time. If you want to use an older version of springMVC only, you need to pom it separately, and the latest version cannot be removed from the dependency library. Dependency libraries also tend to be bloated.
** Build will create a jar file **. You have to be careful about this. The strength of Spring Boot is that tomcat 8 is built into Spring Boot and it can be operated as it is, but if you want to run it in the existing Linux environment where Tomcat is installed, it will be a little troublesome. In some cases, you will have to disassemble the jar file once, extract tomcat8, and harden it with war again ... but that will kill the fun and speedy part of Spring Boot. It's a little thought.
Although it has been simplified in various ways, the mechanism is Spring after all. I think that those who have no experience with Spring need some learning costs. I think it's a little tough when it comes to Java beginners.
I think that it is difficult to operate in large-scale development, but I think that it can be used when you want to develop Java applications with a sense of speed. It is a framework that I want to be popular.
Recommended Posts