Introduction to the

  • Microframeworks, born with Spring4, such as @restcontroller.
  • Quick to get started, integrated with sub-projects (open source frameworks or third party open source libraries).
  • You can get projects up and running very quickly with very little configuration.
  • Fully annotated, simplified XML configuration, no need for web.xml files, built-in HTTP server.

Spring characteristics of the Boot

  • Based on Spring, developers can get started quickly and the threshold is low (Spring buckets).
  • Spring Boot can create applications that run independently of the container.
  • You don’t need to package it as a WAR package and can run it directly in Tomcat.
  • Provides Maven minimalist configuration, but introduces many unnecessary packages.
  • Depend on your project to configure Spring with whatever you need.
  • Provides visual monitoring functions, such as performance and application health.
  • Simplified configuration without having to look at too much XML.
  • Paving the way for Spring Cloud micro-services, Spring Boot can integrate various frameworks to build as services, such as Dubbo, Thrift, and so on.

Spring Boot usage scenarios

  • Anywhere you have Spring
  • The J2EE/Web project
  • Micro service

Spring Boot Starter

Spring Boot Starter includes a set of dependencies that can be integrated into your application, providing one-stop integration with Spring and other technologies without having to hunt around for sample code and dependencies. Spring Boot Official initiators are named spring-boot-starter-xxx, which represents a specific application type. Here are some common Spring Boot dependencies.

  • Spring-boot-starter-logging: Uses the spring Boot default logging framework Logback.
  • Spring-boot-starter-log4j: adds log4j support.
  • Spring-boot-starter-web: Supports web application development, including Tomcat and spring-MVC.
  • Spring-boot-starter-tomcat: use tomcat of spring Boot as the application server.
  • Spring-boot-starter-jetty: Use Jetty instead of the default Tomcat as the application server.
  • Spring-boot-starter-test: contains dependencies required by common tests, such as JUnit, Hamcrest, Mockito, and spring-test.
  • Spring-boot-starter-aop: includes spring-AOP and AspectJ to support section-oriented programming (AOP).
  • Spring-boot-starter-security: includes spring-security.
  • Spring-boot-starter-jdbc: JDBC is used to access databases.
  • Spring-boot-starter-redis: redis is supported.
  • Spring-boot-starter-data-mongodb: includes spring-data-mongodb to support mongodb.
  • Spring-boot-starter-data-jpa: includes spring-data-jPA, spring-ORm, and Hibernate to support JPA.
  • Spring-boot-starter-amqp: Supports AMQP through spring-Rabbit.
  • Spring-boot-starter – Actuator: Adds functions applicable to production environments, such as performance indicators and monitoring.

Spring Boot & Spring Cloud

  • Spring Boot is a fast development framework that allows us to quickly integrate common third-party frameworks, fully annotate them, simplify XML configuration, and ultimately execute them as Java applications.

  • Spring Cloud is currently a complete microservices solution framework with powerful capabilities. Such as registries, client invocation tools, service governance (load balancing, circuit breakers, distributed configuration centers).

  • Spring Boot’s Web component is integrated with Spring MVC by default; Spring Cloud relies on Spring Boot to implement microservices and uses Spring MVC to write microservice interfaces.

Pay attention to

  • Spring Boot is just a rapid development framework, not a microservice framework!

  • Spring Boot + Spring Cloud for microservice development! (RPC remote communication Technology)

Initial experience with Spring Boot

Quickly build

Use Spring official website: start.spring. IO/for quick setup.

Click Generate, and a ZIP package will be downloaded locally, unpacked, and imported into the IDE for development.

Manually set

  • Create a POM file.
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.4. RELEASE</version>
		<relativePath/> 
	</parent>
	<groupId>com.example</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1 - the SNAPSHOT</version>
	<name>hello</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</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>
Copy the code

Spring-boot-maven-plugin is introduced here to generate a JAR package file that can be run directly when using the java-jar command when using the MVN package.

  • Create the Spring Boot application main class

Mark the class with the @SpringBootApplication annotation and let SpringBoot configure the program automatically.

@SpringBootApplication
public class HelloApplication {
 
    public static void main(String[] args) throws Exception { SpringApplication.run(ApplicationDemo.class, args); }}Copy the code
  • Building Web Projects

Spring-boot-starter – Web dependency is introduced in POM file to support Web application development.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code
  • Create API
@RestController
@RequestMapping("/api")
public class HelloWorldResource {

    @GetMapping("/hello")
    public ResponseEntity<String> helloWorld(a) {
        String message = "Hello World!";
        returnResponseEntity.ok(message); }}Copy the code

The source address

springboot-helloworld