SpringBoot简介
1.介绍
1.Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化Spring应用的初始搭建以及开发过程。 -使用springboot以后,搭建一个spring应用和开发变得很简单.
2.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
3.Spring Boot并不是一个框架,它就是一些maven库的集合,maven或者gradle项目导入相应依赖即可使用Spring Boot,而且无需自行管理这些库的版本。
2.为什么使用
1.Spring Boot是为简化Spring项目配置而生,使用它使得jar依赖管理以及应用编译和部署更为简单
2.Spring Boot提供自动化配置,使用Spring Boot,你只需编写必要的代码和配置必须的属性。
3.无需手动管理依赖jar包版本
简单而言,即Spring Boot使编码更简单,使配置更简单,使部署更简单,使监控更简单。Springboot就是为了简化spring应用搭建,开发,部署,监控的开发工具。
SpringBoot使用
1.创建Maven项目
2.导入Spring Boot依赖
parent的pom.xml
<!-- spring boot 父节点依赖,引入这个之后相关的引入就不需要添加version配置,
spring boot会自动选择最合适的版本进行添加 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<!--springboot版本管理-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.0.5.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
child
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.新建启动类完成启动 (@SpringBootApplication、SpringApplication.run)
@SpringBootApplication//标识该应用为springboot的应用
public class HelloApplication {
public static void main(String[] args) {
//启动springboot应用
SpringApplication.run(HelloApplication.class);
}
}
4.写controller,启动后完成测试
@Controller
@RequestMapping("/hello")
public class HelloController {
@RequestMapping("/hi1")
@ResponseBody //直接作为字符串返回而不是页面地址
public String sayHello(){
return " say hello";
}
}
Spring boot web
前面我们使用Spring Boot能往浏览器中输出一个字符串!实际上我们需要的是跳转到一个页面或者获取一个Json数据
跳转jsp
1.创建Maven Web Project
2.导入Maven依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- 依赖配置-->
<!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- servlet 依赖. -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- tomcat 的支持. -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
tomcat依赖包主要是用于跳转jsp
3.配置application.properties对jsp支持
添加src/main/resources/application.properties(与传统的web项目在web.xml中配置视图解析器一样)
# 页面默认前缀目录
spring.mvc.view.prefix=/WEB-INF/jsp/
# 响应页面默认后缀
spring.mvc.view.suffix=.jsp
# 自定义属性,可以在Controller中读取
application.hello=Hello Angel From application
4.编写测试Controller
@Controller
public class JspController {
@RequestMapping("/hello")
public String helloJsp(Model model){
model.addAttribute("hello", "你好");
return"index";
}
}
5.编写jsp页面
在 src/main 下面创建 webapp/WEB-INF/jsp 目录用来存放我们的jsp页面,要和上面application.properties的页面默认前缀目录一样
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
${hello}
</body>
</html>
6.编写启动类
@SpringBootApplication//标识该应用为springboot的应用
public class JspAppliaction {
public static void main(String[] args) {
//启动springboot应用
SpringApplication.run(JspAppliaction.class);
}
}
7.注意 在使用模块开发的时候,启动tomcat 访问jsp 需要配置
Freemaker支持
1.创建naven项目
2.导入jar包
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
</dependencies>
3.配置-application.properties
#FreeeMarker 模板引擎配置
#设定ftl文件路径
spring.freemarker.tempalte-loader-path=classpath:/templates
#关闭缓存,及时刷新,上线生产环境需要修改为true
spring.freemarker.cache=false
spring.freemarker.charset=UTF-8
spring.freemarker.check-template-location=true
spring.freemarker.content-type=text/html
spring.freemarker.expose-request-attributes=true
spring.freemarker.expose-session-attributes=true
spring.freemarker.request-context-attribute=request
spring.freemarker.suffix=.ftl
注意 如果使用上面的配置 需要加templates
4.编写controller
@Controller
public class FreemakerController {
@RequestMapping("/index")
public String index(Model model){
model.addAttribute("message", "呵呵!");
return "index";
}
}
5.在/templates中创建模板(*.ftl)
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello , ${message}</h1>
</body>
</html>
6.编写启动类
@SpringBootApplication
public class FreemakerApplication {
public static void main(String[] args) {
SpringApplication.run(FreemakerApplication.class);
}
}
获取Json数据
1.创建Maven项目,与跳转jsp一样的流程
2.创建一个Domain类Person.java
public class Person {
private Long id;
private String name;
private Date birthday;
.......
2.编写controller
@Controller
@RequestMapping("/json1")
public class JsonController {
//字符串
@RequestMapping("/str")
@ResponseBody
public String json1(){
return "yhptest";
}
//对象-日期
@RequestMapping("/obj")
@ResponseBody
public Person json2(){
return new Person(1L,"yhptest",new Date());
}
//数组
@RequestMapping("/array")
@ResponseBody
public List<Person> json3(){
return Arrays.asList(new Person(1L,"yhptest",new Date())
,new Person(2L,"yaohuaipeng",new Date()));
}
}
springboot配置
1.YAML
Springboot除了支持properties的配置,还支持yaml,而且企业中也是用的最多的。
YAML是YAML Ain’t Markup Language递归缩写,是YAML不是标记语言的意思,读音“yamel”(或者“雅梅尔”)。YAML是便于人阅读基于unicode编码的各种语言的序列号标准。它的用途广泛,用于配置文件,日志文件,跨语言数据共享,对象持久化,复杂的数据结构。
原则:
1、大小写敏感
2、使用缩进表示层级关系
4、缩进长度没有限制,只要元素对齐就表示这些元素属于一个层级。
5、使用#表示注释
6、字符串可以不用引号标注
2.Springboot 打包运行方式
在ide中直接通过main函数启动,而在上线时只能依靠jar包
1.pom.xml配置上插件
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>cn.itsource.freemark.FreemarkTest</mainClass> <!--主类 包含main-->
<layout>JAR</layout>
</configuration>
</plugin>
</plugins>
</build>
2.在cmd中切换到项目所在文件夹下(mvn clean package spring-boot:repackage)
3.在target目录 运行jar包(java -jar xxxx.jar)
SpringBoot测试
1.导入jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
2.创建一个启动类
@SpringBootApplication
Public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
3.测试类
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class) //告诉它启动类的类型
Public class SpringbootTest {
@Test
publicvoid test() throws Exception {
System.out.println(1111);
}
}