brief introduction
spring boot is designed to simplify development and open up various automatic assembly. You don't want to write various configuration files, but you can quickly build a web project by introducing related dependencies. It takes a production-ready application perspective and takes precedence over configuration conventions.
Maybe you have many reasons not to give up SSM,SSH, but once you use springboot, you will feel that everything becomes simpler, configuration becomes simpler, coding becomes simpler, deployment becomes simpler, you feel like you are flying fast, and development speed is greatly improved. For example, when you use IDEA, you will feel that you will never return to the Eclipse era. In addition, IDEA is used as the development tool in this series of tutorials.
Construction Engineering
You need:
- 15 minutes
- jdk 1.8 or more
- maven 3.0+
- Idea
Open Idea - > New Project - > Spring Initializr - > Fill in the group, artifact - > hook up the web - > Click Next.
Engineering catalogue
After the project is created, the catalogue structure of the project is as follows:
- src -main -java -package -SpringbootApplication -resouces - statics - templates - application.yml -test - pom
- The pom file is the basic dependency management file
- resouces resource file
- statics static resources
- templates template resources
- application.yml configuration file
- The entry to the Springboot Application program.
Dependency of 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.forezp</groupId> <artifactId>springboot-first-application</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot-first-application</name> <description>Demo project for Spring Boot</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.8</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>
spring-boot-starter-web not only includes spring-boot-starter, but also automatically opens the web function.
Function demonstration
So much, you may not realize, for example, if you introduce Thymeleaf dependencies, spring boot will automatically help you introduce Spring Template Engine, when you introduce your own Spring Template Engine, spring boot will not help you introduce. It allows you to concentrate on your own business development, rather than various configurations.
Take another chestnut and build a controller:
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RequestMapping; @RestController public class HelloController { @RequestMapping("/") public String index() { return "Greetings from Spring Boot!"; } }
Start the main method of SpringbootFirstApplication, open the browser localhost:8080, browser display:
Greetings from Spring Boot!
Magic:
- You haven't done any web.xml configuration.
- You didn't do any sping mvc configuration; spring boot did it for you.
- You did not configure tomcat; springboot embedded tomcat.
Start springboot mode
cd to the project home directory:
mvn clean mvn package Compile project jar
- mvn spring-boot: run boot
- cd to target directory, java-jar project. jar
Let's see what bean s spring boot injected into us at startup
Add:
@SpringBootApplication public class SpringbootFirstApplication { public static void main(String[] args) { SpringApplication.run(SpringbootFirstApplication.class, args); } @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) { return args -> { System.out.println("Let's inspect the beans provided by Spring Boot:"); String[] beanNames = ctx.getBeanDefinitionNames(); Arrays.sort(beanNames); for (String beanName : beanNames) { System.out.println(beanName); } }; } }
Program output:
Let's inspect the beans provided by Spring Boot: basicErrorController beanNameHandlerMapping beanNameViewResolver characterEncodingFilter commandLineRunner conventionErrorViewResolver defaultServletHandlerMapping defaultViewResolver dispatcherServlet dispatcherServletRegistration duplicateServerPropertiesDetector embeddedServletContainerCustomizerBeanPostProcessor error errorAttributes errorPageCustomizer errorPageRegistrarBeanPostProcessor
.... ....
springboot automatically injects 40-50 bean s at program startup.
unit testing
Open annotations with @RunWith()@SpringBootTest:
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) public class HelloControllerIT { @LocalServerPort private int port; private URL base; @Autowired private TestRestTemplate template; @Before public void setUp() throws Exception { this.base = new URL("http://localhost:" + port + "/"); } @Test public void getHello() throws Exception { ResponseEntity<String> response = template.getForEntity(base.toString(), String.class); assertThat(response.getBody(), equalTo("Greetings from Spring Boot!")); } }
Running it will start the sprigboot project first, then test it, and the test passes ^.^
Source download: https://github.com/forezp/SpringBootLearning
epilogue
There are many springboot books and many springboot blogs on the market. Why should I write such a series? So far, I haven't read a spring boot book, because I haven't had time to read it yet. All of them are official guides. Of course, they also refer to many blogs. They all write very well. When I read the official guides and blogs, I found that they have many differences, so I intend to write a series from the official, through my own understanding and integration, so it is called springboot Unofficial Course. I believe that what I write may be different from what others write. In addition, the most important reason is to improve themselves, with a willing heart to share their understanding to more people in need.
Reference material
Building an Application with Spring Boot
Excellent articles recommend:
- More springboot tutorials: springBoot Unofficial Tutorial Final Chapter: Summary of Articles
- More spring coud tutorials: Summary of the simplest Spring Cloud tutorial in history