1、springBoot是什么
在我们使用SSM开发的时候,我们需要将SSM以xml配置的形式进行整合,同时会发现spring本身的配置就占了很大部分、会导致我们需要对项目的配置文件的进行分类管理,这个时候springBoot就应运而生,专门帮我们管理配置文件。
但是实际上现在springBoot已经发展成了一种开发体系,集成了我们的spring的核心工厂,springMVC、JPA、还有一些spring的功能,同时集成了一个tomcat服务器。我们只需要利用一个springBoot就可以完成整套spring应用的开发,
2、springBoot入门案例
提示:springBoot建议在maven的模式下开发
springBoot集成了我们spring的系列产品,所以我们导包只需要导入一个springBoot就可以了
<project xmlnsprojectxmlns="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.lhc.springbootjpa</groupId>
<artifactId>SpringBoot_jpa_test1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring boot 引用Thymeleaf模板依赖包(Thymeleaf模板如果不适用,这里也可以不添加这段配置,Thymeleaf模板使用在下面会讲到) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
</project>
编写Application类
1、新建项目的包结构
注意:【我们在com.xingxue下面新建Application类,这样就相当于springBoot默认扫描Application类所在所有包】
在resources下新建application.properties文件,里面是服务器的配置,比如修改端口号
package com.xingxue;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@SpringBootApplication
@Controller
@RequestMapping("/test")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@RequestMapping("test.do")
public String test() {
System.out.println("1111111111111");
return "ok";
}
}
直接右键以java程序运行即可,结果如下:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.2.RELEASE)
2017-12-07 14:40:54.337 INFO 4136 --- [ main] com.xingxue.Application : Starting Application on PC-20170521BQHB with PID 4136 (D:\wp_heyi2\SpringBoot\target\classes started by Administrator in D:\wp_heyi2\SpringBoot)
2017-12-07 14:40:54.339 INFO 4136 --- [ main] com.xingxue.Application : No active profile set, falling back to default profiles: default
2017-12-07 14:40:54.616 INFO 4136 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1b853a5: startup date [Thu Dec 07 14:40:54 CST 2017]; root of context hierarchy
2017-12-07 14:40:55.678 INFO 4136 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8089 (http)
2017-12-07 14:40:55.689 INFO 4136 --- [ main] o.apache.catalina.core.StandardService : Starting service Tomcat
2017-12-07 14:40:55.690 INFO 4136 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.11
2017-12-07 14:40:55.818 INFO 4136 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2017-12-07 14:40:55.818 INFO 4136 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1203 ms
2017-12-07 14:40:55.939 INFO 4136 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2017-12-07 14:40:55.944 INFO 4136 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-12-07 14:40:55.945 INFO 4136 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-12-07 14:40:55.949 INFO 4136 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-12-07 14:40:55.950 INFO 4136 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2017-12-07 14:40:56.264 INFO 4136 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1b853a5: startup date [Thu Dec 07 14:40:54 CST 2017]; root of context hierarchy
2017-12-07 14:40:56.321 INFO 4136 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test/test.do]}" onto public java.lang.String com.xingxue.Application.test()
2017-12-07 14:40:56.324 INFO 4136 --- [ 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-07 14:40:56.324 INFO 4136 --- [ 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-07 14:40:56.347 INFO 4136 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-07 14:40:56.347 INFO 4136 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-12-07 14:40:56.392 INFO 4136 --- [ 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-07 14:40:56.418 WARN 4136 --- [ main] .t.AbstractTemplateResolverConfiguration : Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
2017-12-07 14:40:56.843 INFO 4136 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2017-12-07 14:40:56.889 INFO 4136 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8089 (http)
2017-12-07 14:40:56.895 INFO 4136 --- [ main] com.xingxue.Application : Started Application in 2.839 seconds (JVM running for 3.092)
输入地址 http://localhost:8080/test/test.do
我们发现控制台打印 如下
这时表明springboot已经运行,这个过程是:
首先springboot自带tomcat,当我们启动的时候,springboot就把该项目自动发布到自带的tomcat下面,然后默认使用8080端口启动tomcat,项目就已经发布成功,
我们访问的时候直接根据原来的方式访问即可,这时发现请求到达了controller,而我们注解实际上是使用springMVC注解,
总结一句话,相当于我们使用了springBoot,springBoot就默认帮我们生成了springMVC和spring的配置文件,而且自动帮我们发布项目