springboot是java的开源框架之一,简化spring的大量重复工作(spring.xml的配置,spring和第三框机集成需要的配置,重复jar的添加)通过特定配置进行项目的配置,不需要开发人员定义样板化的配置,实现项目的快速开发。
1.可以创建独立的spring应用程序,基于他的maven,可以创建可执行的jar、war。
2.内嵌tomcat或jetty等servlet容器
3.自动配置spring容器,比如自动注入,自动扫描等
4.无代码生成,不需要xml配置
5.提供自动配置的“starter”项目对象模型(POMS)以简化maven配置
-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0modelVersion>
-<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.2.RELEASEversion>
<relativePath/>
parent>
<groupId>com.yd.appgroupId>
<artifactId>01-springbootartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<name>01-springbootname>
<description>Demo project for Spring Bootdescription>
-<properties>
<java.version>1.8java.version>
properties>
-<dependencies>
-<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
-<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-tomcatartifactId>
<scope>providedscope>
dependency>
-<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
-<exclusions>
-<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
-<build>
-<plugins>
-<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
/***
* 创建springbmvc的controller
* RestController=Controller+ResponseBody
* @RestController 能将controller纳入spring容器
* 还能controller的异步响应
*/
//@Controller
@RestController
public class SpringbootController {
/***
* 处理两个数相加的请求
*/
@RequestMapping("/add")
public Integer add(Integer x,Integer y){
return x+y;
}
}
在浏览器中输入 localhost:8080/add:x=1&y=1访问
#tomcat的访问端口
server.port=8081
#项目的访问上下文
server.servlet.context-path=/boot
#get请求方式的中文乱码处理
server.tomcat.uri-encoding=UTF-8
server:
port: 8082 #配置tomncat端口号
servlet:
context-path: /bt #配置上下文
tomcat:
uri-encoding: UTF-8 #get请求中文乱码处理
#以上的配置都是给系统中类的属性在赋值
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 自定义Users实体类
* 在application.yml中对Uesrs类的属性赋值
*/
@ConfigurationProperties(
prefix = "users", //指定配置文件中配置项的前缀
ignoreUnknownFields = true
)
@Component //将Users纳入spring容器中
public class Users {
private String name;
private String pwd;
private String address;
//生成get/set/toString方法
}
#给自动以类的属性赋值
users:
name: zhagnsan
pwd: 9527
address: beijing
age: 23
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UsersController {
//注入Users
@Autowired
private Users users;
/***
* 处理显示用户信息的请求
*/
@RequestMapping("/show")
public String showUsers(){
return users.toString();
}
}
访问用户信息:localhost:8082/bt/show
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
@SpringBootTest
class ApplicationTests {
/***
* junit在启动的时候加载了spring容器
* junit在启动的时候加载了appliaction.yml
*/
@Autowired
private Users users;
@Test
void contextLoads() {
System.out.println(users);
}
}
<!-- JSTL -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!-- jsp解析器 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
for(int x=1;x<=10;x++){
out.print("
");
}
%>
</body>
</html>
访问localhost:8080/users/index.jsp
@RestController
public class UserController {
/***
* 处理用户信息查询的请求
*/
@RequestMapping("/show")
public String show(){
return "这是个人信息....";
}
}
server:
port: 8081
servlet:
context-path: /user
/***
*通过实现HandlerInterceptor接口,自定义拦截器
*/
public class PremisssionInterceptor implements HandlerInterceptor{
/***
* 实现接口中定义的方法
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//获得客户端提交的凭证
String token = request.getParameter("token");
if(!StringUtils.isEmpty(token)){
if(token.equals("123456")){
System.out.println("token="+token);
//权限验证通过,进行放行到Controller
return true;
}
}
response.setContentType("text/html;charset=UTF-8");
//给客户端响应
response.getWriter().write("权限不足.....");
return false;
}
}
//将该类纳入spring容器
@Configuration
public class ConfigBean implements WebMvcConfigurer {
/**
* 将自定义的拦截器添加到springboot框架中
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new PremisssionInterceptor()).addPathPatterns("/**");
}
}
localhost:8081/user/shw?token=123456 ——>这是个人信息…
localhost:8081/user/shw?token=123 ——>权限不足
静态资源存放在resources/static当中,拦截器中静态资源需要放行
//将该类纳入spring容器
@Configuration
public class ConfigBean implements WebMvcConfigurer {
/**
* 将自定义的拦截器添加到springboot框架中
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new PremisssionInterceptor())
.addPathPatterns("/**") //配置需要拦截取得路径
.excludePathPatterns("/images/**"); //指定不需要拦截的路径
}
}