Spring Boot 优点
- 轻量化
- 提供 Spring 框架各种默认配置来简化项目配置
- 内嵌 Web 容器
- 没有冗余代码生成和XML配置要求
Maven 导包
-
spring-boot-starter
:核心模块,包括了自动配置支持、日志和YAML -
spring-boot-starter-test
:测试模块,包括JUnit、Hamcrest、Mockito -
spring-boot-starter-web
:Web模块
Spring Boot 项目基本由主加载类、逻辑实现类、单元测试类、以及资源配置文件组成。
一、资源配置文件
1)Maven项目依赖中引入spring-boot-starter-web,在pom.xml中导入相应的依赖包
<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.baron</groupId>
<artifactId>springboot_person</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>springboot</name>
<description>springboot的maven工程</description>
<!-- Inherit defaults from Spring Boot 继承默认配置-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.0.BUILD-SNAPSHOT</version>
</parent>
<!-- Add typical dependencies for a web application 为Web应用程序添加典型的依赖项-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<!-- Package as an executable jar 打包成可执行的jar包-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!-- Add Spring repositories 添加spring的依赖库-->
<!-- (you don't need this if you are using a .RELEASE version) 如果你使用的是修订版本,就不需要添加这个-->
<repositories>
<repository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-snapshots</id>
<url>http://repo.spring.io/snapshot</url>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<url>http://repo.spring.io/milestone</url>
</pluginRepository>
</pluginRepositories>
</project>
2)框架资源的配置
在src/main/resources下可以创建application.properties文件,将服务器端口写入
server.port=8081
二、创建主加载类 Application.java
主加载类是springboot最重要的一个类,是启动程序的入口。
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);
}
}
三、创建逻辑实现类 controller
逻辑实现类就是controller层,是服务接口。
-
@RequestMapping
– 定义处:提供初步的请求映射信息。相对于 WEB 应用的根目录
– 方法处:提供进一步的细分映射信息。相对于类定义处的 URL。若
类定义处未标注 @RequestMapping,则方法处标记的 URL 相对于
WEB 应用的根目录.- DispatcherServlet 截获请求后,就通过控制器上
- @RequestMapping 提供的映射信息确定请求所对应的处理方法。
- @RequestMapping 除了可以使用请求 URL 映射请求外,还可以使用请求方法、请求参数及请求头映射请求
-
@PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中:URL 中的 {xxx} 占位符可以通过@PathVariable("xxx") 绑定到操作方法的入参中。
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 Springweb {
@RequestMapping("/")
public String home() {
return "hello world!";
}
@RequestMapping("hello/{name}")
public String index(@PathVariable String name) {
return "Hello, " + name;
}
}
四、程序run运行
.__ .__ .__ ___.
| |__ ____ | | | | ____ \_ |__ _____ _______ ____ ____
| | \_/ __ \| | | | / _ \ | __ \\__ \\_ __ \/ _ \ / \
| Y \ ___/| |_| |_( <_> ) | \_\ \/ __ \| | \( <_> ) | \
|___| /\___ >____/____/\____/ /\ |___ (____ /__| \____/|___| /
\/ \/ )/ \/ \/ \/
2017-12-21 13:54:39.527 INFO 11188 --- [ main] com.baron.Application : Starting Application on PC-201710141743 with PID 11188 (E:\workspace\springboot_person\target\classes started by Administrator in E:\workspace\springboot_person)
2017-12-21 13:54:39.531 INFO 11188 --- [ main] com.baron.Application : No active profile set, falling back to default profiles: default
2017-12-21 13:54:39.605 INFO 11188 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@125dc0f: startup date [Thu Dec 21 13:54:39 CST 2017]; root of context hierarchy
2017-12-21 13:54:41.384 INFO 11188 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-12-21 13:54:41.400 INFO 11188 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-12-21 13:54:41.401 INFO 11188 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.4
2017-12-21 13:54:41.504 INFO 11188 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-12-21 13:54:41.504 INFO 11188 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1902 ms
2017-12-21 13:54:41.666 INFO 11188 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-12-21 13:54:41.673 INFO 11188 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-12-21 13:54:41.673 INFO 11188 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-12-21 13:54:41.674 INFO 11188 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-12-21 13:54:41.674 INFO 11188 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-12-21 13:54:42.004 INFO 11188 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@125dc0f: startup date [Thu Dec 21 13:54:39 CST 2017]; root of context hierarchy
2017-12-21 13:54:42.071 INFO 11188 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello/{name}]}" onto public java.lang.String com.baron.web.Springweb.index(java.lang.String)
2017-12-21 13:54:42.072 INFO 11188 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto public java.lang.String com.baron.web.Springweb.home()
2017-12-21 13:54:42.075 INFO 11188 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-12-21 13:54:42.076 INFO 11188 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-12-21 13:54:42.112 INFO 11188 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-21 13:54:42.112 INFO 11188 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-21 13:54:42.152 INFO 11188 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-21 13:54:42.286 INFO 11188 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-12-21 13:54:42.346 INFO 11188 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-12-21 13:54:42.350 INFO 11188 --- [ main] com.baron.Application : Started Application in 3.35 seconds (JVM running for 3.769)
五、控制面板显示
在src/main/resources下创建banner.txt文件,里面填写显示的内容。
image.png
image.png
网友评论