1.SpringBoot是什么?
Spring Boot是由Pivotal团队提供的全新框架(下一代框架),其设计目的是用来简化新Spring应用的初始搭建以及开发过程
2.SpringBoot的优点
2.1 对比传统的SSM
配置web.xml,加载spring和spring mvc
配置数据库连接、配置spring事务
配置加载配置文件的读取,开启注解
配置日志文件
配置其他很多的东西
.........................
2.2 SpringBoot的设计思想
约定优先于配置
3.SpringBoot与SpringMVC和Spring的区别
Spring 开发是需要很多的配置文件的,Spring Boot把许多的通用配置进行了封装,不需要重新配置,只有在做一些个性化的开发时才会进行额外的配置(约定优先于配置)
Spring Mvc开发的应用程序依赖于Tomcat
Spring Boot内嵌了Tomcat,用户无需安装Tomcat,直接运行.jar文件即可
4.SpringBoot 解决的问题
Spring Boot使编码变简单
Spring Boot使配置变简单
Spring Boot使部署变简单
Spring Boot使监控变简单
微服务
5.SpringBoot开发
5.1 创建SpringBoot项目
创建普通Maven项目即可
5.2 导入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<!--开发web项目的依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
5.3建立目录结构
5.4创建 UserController
package com.ma.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class UserContrpller {
@GetMapping("/findUserById")
public String findUserById(){
System.out.println("findUserById......");
return "index.html";
}
}
5.5创建启动类
package com.ma.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
//启动Spring应用
SpringApplication.run(Application.class,args);
}
}
5.6配置资料
application.properties
server.port=8088
5.7 index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
index.jsp
</body>
</html>
5.8运行程序
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.2.RELEASE)
2018-08-29 20:57:17.788 INFO 12760 --- [ main] com.ma.springboot.Application : Starting Application on 马越的电脑 with PID 12760 (C:\Users\lenovo\IdeaProjects\girl\target\classes started by lenovo in C:\Users\lenovo\IdeaProjects\girl)
2018-08-29 20:57:17.871 INFO 12760 --- [ main] com.ma.springboot.Application : No active profile set, falling back to default profiles: default
2018-08-29 20:57:18.246 INFO 12760 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@214b199c: startup date [Wed Aug 29 20:57:18 CST 2018]; root of context hierarchy
2018-08-29 20:57:22.572 INFO 12760 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8088 (http)
2018-08-29 20:57:22.679 INFO 12760 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-08-29 20:57:22.680 INFO 12760 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-08-29 20:57:22.691 INFO 12760 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_91\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Java\jdk1.8.0_91\bin;C:\ProgramData\Oracle\Java\JAVA_HOME;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;C:\Program Files\MySQL\MySQL Server 5.5\bin;D:\program\apache-maven-3.5.3\bin;D:\program\apache-tomcat-7.0.88\lib;D:\program\apache-tomcat-7.0.88\bin;;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\TortoiseSVN\bin;D:\program\SlikSvn\bin;C:\Users\lenovo\AppData\Local\Microsoft\WindowsApps;;.]
2018-08-29 20:57:23.167 INFO 12760 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-08-29 20:57:23.167 INFO 12760 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 4980 ms
2018-08-29 20:57:23.604 INFO 12760 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-08-29 20:57:23.614 INFO 12760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-08-29 20:57:23.615 INFO 12760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-08-29 20:57:23.616 INFO 12760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-08-29 20:57:23.616 INFO 12760 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-08-29 20:57:23.997 INFO 12760 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-08-29 20:57:24.643 INFO 12760 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@214b199c: startup date [Wed Aug 29 20:57:18 CST 2018]; root of context hierarchy
2018-08-29 20:57:24.769 INFO 12760 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/findUserById],methods=[GET]}" onto public java.lang.String com.ma.springboot.controller.UserContrpller.findUserById()
2018-08-29 20:57:24.780 INFO 12760 --- [ 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.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-08-29 20:57:24.781 INFO 12760 --- [ 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.servlet.error.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-08-29 20:57:24.836 INFO 12760 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-08-29 20:57:24.837 INFO 12760 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-08-29 20:57:25.318 INFO 12760 --- [ main] o.s.j.e.a.AnnotationMBeanExporter : Registering beans for JMX exposure on startup
2018-08-29 20:57:25.424 INFO 12760 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8088 (http) with context path ''
2018-08-29 20:57:25.435 INFO 12760 --- [ main] com.ma.springboot.Application : Started Application in 9.05 seconds (JVM running for 14.495)
2018-08-29 20:57:39.053 INFO 12760 --- [nio-8088-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-08-29 20:57:39.054 INFO 12760 --- [nio-8088-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization started
2018-08-29 20:57:39.105 INFO 12760 --- [nio-8088-exec-1] o.s.web.servlet.DispatcherServlet : FrameworkServlet 'dispatcherServlet': initialization completed in 51 ms
findUserById......
http://localhost:8088/findUserById
6.SpringBoot的热部署
6.1pom依赖
<!--spring boot热部署的插件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
6.2修改properties
spring.thymeleaf.cache=false
6.3修改IDEA自动编译Java
file--->other settings--->default settings---->Build--->compiler--->build project autoxxxx(勾选)
6.4修改idea运行中的自动编译
help--->find action--->Registry--->compiler.automake.allow.when.app.running(勾选)
7.SpringBoot中的单元测试
7.1 pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
7.1 User类
package com.ma.springboot.pojo;
public class User {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
7.2 UserService类
package com.ma.springboot.service;
import com.ma.springboot.pojo.User;
import com.ma.springboot.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
private UserDao userDao;
public User findUserById(Integer id){
return userDao.findUserById(id);
}
}
7.3 UserDao类
package com.ma.springboot.dao;
import com.ma.springboot.pojo.User;
import org.springframework.stereotype.Repository;
@Repository
public class UserDao {
public User findUserById(Integer id){
User user = new User();
user.setId(id);
user.setName("张三");
user.setAge(20);
return user;
}
}
7.4 UserServiceTest类
package com.ma.springboot.service;
import com.ma.springboot.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserServiceTest {
@Autowired
UserService userService;
@Test
public void testFindUserById(){
User user = userService.findUserById(2);
System.out.println(user);
}
}
7.5 运行结果