1.什么是SpringBoot以及特点。
SpringBoot基于Spring4.0设计,不仅继承了Spring框架原有的优秀特性[IOC AOP DI],而且还通过简化配置来进一步简化了Spring应用的整个搭建和开发过程。另外SpringBoot通过集成大量的框架使得依赖包的版本冲突,以及引用的不稳定性等问题得到了很好的解决.
SpringBoot所具备的特征有:
(1)可以创建独立的Spring应用程序,并且基于其Maven或Gradle插件,可以创建可执行的JARs和WARs;
(2)内嵌Tomcat或Jetty等Servlet容器;
(3)提供自动配置的“starter”项目对象模型(POMS)以简化Maven配置;
(4)尽可能自动配置Spring容器;
(5)提供准备好的特性,如指标、健康检查和外部化配置;
(6)绝对没有代码生成,不需要XML配置。
springboot:----为了简化spring工程的初始化搭建和开发过程。
2.快速搭建springboot工程
1.打开IDEA -->File-->New-->project...(新建一个项目)
2,创建SpringBoot配置
3,创建成功后如下图所示(这个过程会下载很多架包等待几分钟需联网进行)
4,简单分析一下项目框架构成
在主类所在的包下创建controller包并在该包下创建一个HelloController类
主类:Spring01Application
@RestController //标记改类为controller并且该类中所有方法的返回值为json格式。
public class HelloController {
@GetMapping("hello") //思考: 没有引入jackson依赖
public Map<String,Object> hello(){
Map<String,Object> map=new HashMap<>();
map.put("name","李兵兵");
map.put("age",26);
return map;
}
}
启动主函数。
浏览访问HelloContnroller下的路径资源

3.springboot中的一些pom.xml以及配置文件的解析.
pom.xml
<!--继承springboot父工程:该工程中的主要作用:对jar的版本管理-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.12</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
application.properties
#Tomcat端口号[] key=value
server.port=7777
# 设置工程的上下文路径
server.servlet.context-path=/aaa
4.springboot的配置文件的类型。
application.properties
properties:属性文件类型: 格式 key=value格式
#Tomcat端口号[] key=value
server.port=7777
# 设置工程的上下文路径
server.servlet.context-path=/aaa
yml:文件类型: 格式:
server:
port: 8081
servlet:
context-path: /bbb
#如果yml和properties都存在相同的配置,则以properties为主, 如果两个配置文件的内部不同。则合并。#不管是那种格式,他们的名称必须叫:application
5.java读取springboot配置文件的内容。
比如:OSS文件上传。 需要: bucketName 密钥 id. 当时你们写死到类中。改为客户的。修改java源码。---交付的war文件。通过java代码读取配置文件里面的内容。
读取配置文件内容的方式:
第一种: @Value读取
@Value("${student.name}") //读取springboot配置文件中student.name对应的内容
private String name;
@Value("${student.age}")
private Integer age;
@Value("${student.address}")
private String address;
@GetMapping("index")
public String index(){
return "姓名:"+name+";年龄:"+age+";地址:"+address;
}
第二种: @ConfigurationProperties(prefix="前缀") ---类
建一个Vo类接收 不需要再一个一个的写
注意:第一种方式只能读取(基本类型和字符串类型) 第二种方式可以读取任意类型数组,集合等。
server:
servlet:
context-path: /bbb
student:
name: lisi
age: 28
address: zhengzhou
hobby: # yml结构中数组类使用-表示一个元素
- swing
- eating
- sleeping
map:
k1: v1
k2: v2
k3: v3
在线yaml转properties-在线properties转yaml-ToYaml.com
6.springboot多环境开发配置.
环境: (1)开发环境 (2)测试环境 (3)生产环境【线上环境】
由于环境的不同,可能它们的配置也会不同。
(1)application-dev.properties 开发环境
#针对关于开发的配置内容
server.port=8001
(2) application-test.properties 测试环境
#针对关于测试环境的配置内容
server.port=8002
(3)application-online.properties 生产环境【线上环境】
#针对关于线上环境的配置内容---
server.port=8003
application.properties( 最后都要在此中执行)
# 不同环境公共配置可以写在application
student.name=zhangsan
student.age=18
student.address=beijing
student.hobby[0]=swing
student.hobby[1]=eating
student.hobby[2]=sleeping
student.map.k1=v1
student.map.k2=v2
student.map.k3=v3
#激活相应的环境配置---每个环境的文件配置名必须:application-xxx.properties
spring.profiles.active=dev
7.springboot注册web三大组件.
web中的三大组件. servlet filter过滤器 Listener监听器
由于springboot没有web.xml文件了,有时我们需要把第三方的servlet注册到springboot中。讲解shiro安全框架
7.1 注册servlet到springboot内置tomcat中
(1)创建一个servlet类
public class MyServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("************************************************");
}
}
(2)创建一个配置类
package com.ykq.qy165springboot01.config;
import com.ykq.qy165springboot01.servlet.MyServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.http.HttpServlet;
@Configuration //等价于xml配置文件
public class MyConfig {
@Bean //等价于xml配置文件<bean/> ServletRegistrationBean该类用于注册servlet到tomcat容器中
public ServletRegistrationBean<HttpServlet> registrationBean(){
ServletRegistrationBean<HttpServlet> registrationBean=new ServletRegistrationBean<>();
registrationBean.setName("my");
registrationBean.setServlet(new MyServlet());
registrationBean.addUrlMappings("/my");
return registrationBean;
}
}
(3)测试
7.2 注册web的filter组件
回顾: web开发--如何定义过滤器以及如何把过滤注册到web.xml容器中。
public class 类名 implements java.servlet.Filter{ 去 11
doFilter(){}
}
<filter>
<filter-name></filter-name>
<filter-class></filter-class>
</filter>
<filter-mapping>
<filter-name></filter-name>
<url-paterrn></
</filter-mapping>
springboot中不存在web.xml文件。---@Configuration
(1)创建一个过滤器类
public class MyFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//校验是否登录 编码
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~经过了该过滤器----------------------------");
filterChain.doFilter(servletRequest,servletResponse);//放行到下一个过滤器或者到达你的请求地址的controller
}
}
(2)配置类
@Bean //加载方法上。把该方法的返回结果交于springIOC容器来管理
public FilterRegistrationBean<Filter> filterRegistrationBean(){
FilterRegistrationBean<Filter> filterRegistrationBean=new FilterRegistrationBean<>();
filterRegistrationBean.setName("myFilter");
filterRegistrationBean.setFilter(new MyFilter());
filterRegistrationBean.addUrlPatterns("/*");
return filterRegistrationBean;
}