- SpringBoot
- 一、配置介绍
- SpringbootApplication.java
- ServletInitializer.java
- application.yaml
- 二、基础使用
- 2.1 热部署
- 2.2 Properties之DEBUG模式
- 2.3 异常处理
- 2.4 配置加密
- 2.5 条件装配
- @Conditional
- properties
- 2.6 自定义starter
- 使用流程
- 举个例子
- 三、框架集成
- 3.1 Thymeleaf
- 3.2 Mybatis
- 依赖包导入
- application配置文件
- 扫描mapper包
- 四、原理解析
- 4.1 自动装配的实现
- @SpringBootConfiguration
- @EnableAutoConfiguration
- @ComponentScan
- 4.2 启动类流程
- over
其实总结只有四个部分
-
配置介绍
-
基础使用
-
框架集成
-
原理解析
package com.example.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
ServletInitializer.java
Web.xml的替代
现在JavaConfig配置方式在逐步取代xml配置方式。而WebApplicationInitializer可以看做是Web.xml的替代,它是一个接口。通过实现WebApplicationInitializer,在其中可以添加servlet,listener等,在加载Web项目的时候会加载这个接口实现类,从而起到web.xml相同的作用
(1条消息) Spring中WebApplicationInitializer的理解_小小程序猿-CSDN博客
application.yaml-
application.properties
-
- 语法结构 :key=value
-
application.yml
-
- 语法结构 :key:空格 value
- application.yaml
- 语法结构 :同上,还可以如下
# 玩法一:
server:
port: 7897
# 玩法二:键值对形式,注意空格
student: {name: text,age: 18}
# 玩法三:数组
student: [ 1,4,5,6 ]
说明:语法要求严格!
1、空格不能省略
2、以缩进来控制层级关系,只要是左边对齐的一列数据都是同一个层级的
3、属性和值的大小写都是十分敏感的
给对象赋值
yaml文件
user: name: test age: 18
实体类
package com.example.springboot.controller;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Component //注册bean到容器中
@ConfigurationProperties(prefix = "user")
public class User {
private String name;
private Integer age;
// 此处省略了toString和get set方法
}
test.java
package com.example.springboot;
import com.example.springboot.controller.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
public class DemoApplicationTests {
@Autowired
private User user01;
@Test
public void test()
{
System.out.println(user01);
}
}
运行结果挺神奇的,很意外
User{name='13544', age=18}
还可以加载指定的配置文件
**@PropertySource :**加载指定的配置文件;
@configurationProperties:默认从全局配置文件中获取值;
@PropertySource(value = "classpath:person.properties")
还可以引入一个Jar,让赋值的时候更加丝滑
org.springframework.boot
spring-boot-configuration-processor
二、基础使用
2.1 热部署
在每次修改代码后,都需要重启服务器或者重新启动Java程序,现在可以动态加载代码查看效果
导入依赖后,按Ctrl +F9即可查看效果
2.2 Properties之DEBUG模式org.springframework.boot spring-boot-devtools runtime true
在Properties文件中加入debug模式,不过下面我用了yaml作为举例
debug: true # 进入调试模式
开启后,可以在控制台窗口查看未启动的依赖,例如
Negative matches:
ActiveMQAutoConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'javax.jms.ConnectionFactory' (OnClassCondition)
AopAutoConfiguration.AspectJAutoProxyingConfiguration:
Did not match:
- @ConditionalOnClass did not find required class 'org.aspectj.weaver.Advice' (OnClassCondition)
2.3 异常处理
package springboot.com.controller.error;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Configuration
public class ErrorHandler implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
Exception ex) {
ModelAndView mv = new ModelAndView();
if (ex instanceof ArithmeticException) {
mv.setViewName("error/NoFindView");
mv.addObject("Exception", ex.toString());
return mv;
}
if (ex instanceof NullPointerException) {
mv.setViewName("error/NoFindView");
mv.addObject("Exception", ex.toString());
return mv;
}
mv.setViewName("error/NoFindView");
mv.addObject("Exception", ex.toString());
return mv;
}
}
2.4 配置加密
1
2.5 条件装配 @Conditional一般打在配置类,跟注解@Configuration和@Bean配合使用
然后看看SpringBoot上面的
propertiesspring-autoconfigure-metadata.properties
通过配置文件的方式实现自动装配条件配置
2.6 自定义starter 使用流程- 创建工程
- 添加Jar包依赖
- 定义属性类(类似于跟我们在application.properties绑定用的那个类)
- 自动装配类,实现你的业务逻辑
- 创建meta-INF/spring.factories
org.springframework.boot spring-boot-starter-thymeleaf 2.5.3
声明空间
3.2 Mybatis 依赖包导入
application配置文件mysql mysql-connector-java org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.0 org.springframework.boot spring-boot-starter-jdbc
主文件
spring:
profiles:
active: dev
子文件
server:
port: 80
spring:
thymeleaf:
cache: false
# stop the Thymeleaf cache
datasource:
username: root
password: szsti@123
url: jdbc:mysql://47.106.207.254:3306/empdb
driver-class-name: com.mysql.cj.jdbc.Driver
mvc:
hiddenmethod:
filter:
enabled: true
# start html rest model
mybatis:
mapper-locations: classpath:springboot/com/mapper/*Mapper.xml
type-aliases-package: springboot.com.entity
扫描mapper包
package springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("springboot.com.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
完成剩余常规编写
四、原理解析 4.1 自动装配的实现在SpringBoot入口main方法中有一个注解
@SpringBootApplication
它其实由下面三个组件组成,下面来分开进行说明
@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan@SpringBootConfiguration
1、它代表当前SpringBoot运行的这个类是个配置类
2、同价与Spring中的标准@Configuration
3、它继而也应用了@Component,代表它也是一个组件扫描的候选者
- @Configuation等价于
- @Bean等价于
首先从名字出发,可以知道这是一个开启自动配置类
这个注解下有两个数组,有两个用处
抬头一看,上面还挂了俩注解
@AutoConfigurationPackage
打开其中源码,里面有一个导入注解,@import(AutoConfigurationPackages.Registrar.class)
这是一个内部类,利用它批量的把包含注释类的包注册到 AutoConfigurationPackages 中,它通过元注解的信息来获取到包名
::指示 包含注释类 的包应注册到 AutoConfigurationPackages
@import(AutoConfigurationimportSelector.class)
实现配置类的批量装配
因为其中重写了 selectimports 方法
并且通过Spring提供的SpringFactoriesLoader机制,扫描classpath路径下的meta-INF/spring.factories文件,读取需要实现自动装配的配置类
@ComponentScan
1、配置用于Configuration类的组件扫描指令,制定扫描哪个路径下的Spring注解
2、如果不定义路径,就从声明这个注解的类的包开始扫描
4.2 启动类流程1. 创建了一个IOC容器
SpringApplication.run(ProductApplication.class, args);
2. 加载启动类的字节码,扫描到了SpringBootApplication这个注解
@SpringBootApplication
3. 将启动类标识为一个配置类
4. ComponentScan:包扫描,指定扫描的范围--->启动类所在的包下的所有类的注解以及子包中所有类的注解
5. @EnableAutoConfiguration自动装配:加载了可以能使用到的所有的bean对象--->pom文件中的依赖
over


