Spring boot is a lightweight web framework launched by Spring, which mainly solves the problem of configuration and development speed that Spring has been criticized for small projects.
Spring Boot contains the following features:
Create Spring applications that can run independently.
Embedding Tomcat or Jetty servers directly does not require deploying WAR files.
Provide recommended basic POM files to simplify Apache Maven configuration.
Automatically configure the Spring framework based on project dependencies as much as possible.
Provide functions that can be used directly in production environments, such as performance indicators, application information and application health checks.
No code generation, no XML configuration file.
****** First SpringBook application: ******
1. Building maven project
IDEA:
Create New Project - > Maven - > Select JDK - >.
GroupId : com.example, ArtifactId: springBootDemo -> Project name : springBootDemo
2. Compiling 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.example</groupId> <artifactId>springBootDemo</artifactId> <version>1.0-SNAPSHOT</version> <!-- Inherit defaults from Spring Boot --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.3.RELEASE</version> </parent> <!-- Add typical dependencies for a web application --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <!-- Package as an executable jar --> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3. Programming Application.java is stored under myFirstProject src main java com example
package com.example; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
4. Write Example.java stored under myFirstProject src main java com example Web
package com.example.web; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } @RequestMapping("/hello/{myName}") String index(@PathVariable String myName) { return "Hello " + myName; } }
5. operation
Select Run'Application'on the Application.java file (or file)
The following picture:
6. visit
After running successfully, open the browser and enter http://localhost:8080
The page shows Hello World!
Annotations:
Common annotations in Spring boot are as follows:
@ResponseBody
The function modified with this annotation fills the result directly into the HTTP response body, which is generally used to construct RESTful api.
@Controller
Used to define the controller class, in spring project, the controller is responsible for forwarding the user's URL request to the corresponding service interface (service layer).
@RestController
@Collection of ResponseBody and @Controller
@RequestMapping
Provide routing information, responsible for mapping URL s to specific functions in Controller.
@EnableAutoConfiguration
Spring Boot auto-configuration: Try to automatically configure your Spring application based on the jar dependencies you add. For example, if you have HSQLDB in your classpath and you do not manually configure any database connection beans, then we will automatically configure an in-memory database.You can add @EnableAutoConfiguration or @SpringBootApplication annotations to an @Configuration class to select automatic configuration . If you find that a specific automatic configuration class you don't want is applied, You can disable them by using the exclusion property of the @Enable AutoConfiguration annotation.
@ComponentScan
Represents that by automatically discovering (scanning) this class and registering it as a Bean, all Spring components can be automatically collected. Including the @Configuration class. We often use the @ComponentScan annotation to search beans,Import with @Autowire annotation.
@Configuration
Equivalent to traditional xml configuration files, if some third-party libraries need to use xml files, It is still recommended that the @Configuration class be used as the main configuration class for the project.——xml configuration files can be loaded using the @ImportResource annotation.
@SpringBootApplication
Equivalent to @Enable AutoConfiguration,@A collection of ComponentScan and @Configuration.
@Import
Used to import other configuration classes.
@ImportResource
Used to load the xml configuration file.
@Autowired
Automatically import dependent bean s
@Service
Components commonly used to modify service layers
@Repository
Use the @Repository annotation to ensure that DAO or repositories provide exception translation The annotated DAO or repositories classes will be found and configured by ComponetScan without having to provide XML configuration items for them.