Spring boot
Spring boot学习笔记
Quick Start
创建maven 工程
配置pom.xml文件 新增依赖
```xml
org.springframework.bootspring-boot-starter-parent2.1.6.RELEASE
org.springframework.boot spring-boot-starter-web
3. 创建Example.java
* 目录:src/main/java/Example.java
```java
import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;
@RestController
@EnableAutoConfiguration
public class Example {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(Example.class, args);
}
}填完信息自动生成项目文件 点击下载即可
导入到IDE里面
运行DemoApplication
localhost:8080访问查看运行结果
注解开发
1. @SpringBootApplication
等价于@SpringBootConfiguration+ @EnableAutoConfiguration+ @ComponentScan
2. @RestController
等价于 @Controller+@ResponseBody
注:@RestController与@RequestMapping是Spring MVC的注解,不是Spring Boot特有的
3.@RequestMapping,@PathVariable
4.@GetMapping
简化method设置
5.@RequestParam
6.@RequestBody
请求头的 Content-Type需设置为application/json,参数放在body中
7.@RequestHeader
8.HttpServletRequest
其它Http请求
json处理(jackson)
spring-boot 目录结构
同名静态资源,加载顺序
META/resources->resources->static->public 有则直接返回,没有则404
引入thymeleaf
默认的模板映射路径是:src/main/resources/templates,
访问路径:localhost:8080/page/index
其它静态资源
默认配置
spring.resource.static-locations=
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
更改配置写入自定义静态资源文件夹
resources目录下创建application.properties,加入test目录
文件上传
服务端
html
硬编码方式修改文件上传大小限制
springProApplication文件(带有@SpringBootApplication注解)中添加
打包jar包
引入maven插件
打包命令
运行命令(Running as a Packaged Application)
指定文件上传预览路径
配置application.properties
文件服务器
fastdfs
阿里云oss
nginx搭建文件服务器
pom引入插件
注意:IDEA中需进行以下设置
不被热部署的文件
默认:/META-INF/maven,/META-INF/resources,/resources,/static,/public,/template
指定不进行热部署的文件
指定不监听application.properties文件,
在配置application.properties,中添加
通过触发器,控制什么时候进行热更新
```properties
spring.devtools.restart.trigger-file=.reloadtrigger拦截器
取值
方式2
使用实体类配置文件
@Component 注解
@PropertySource 注解指定配置文件位置
WConfigurationProperties 注解设置相关属性
使用
必须用过注入IOC对象Resource,才能在类中获取到配置文件的值
示例
ServerSettings.java
common.properties
testController.java
```java
@Autowired
private ServerSettings serverSettings;
@GetMapping("/test/properties") public Object getProperties(){ return serverSettings; }
```
单元测试
引入依赖
普通测试
API测试
个性化启动banner设置和debug日志
banner.txt
application.properties
打成jar包使用命令运行即可查看 启动debug日志
配置全局异常处理
自定义异常捕获
MyException.java
```java public class MyExcepion extends RuntimeException {
private Integer code;
private String msg;
public Integer getCode() { return code; }
public void setCode(Integer code) { this.code = code; }
public MyExcepion(Integer code, String msg) { this.code = code; this.msg = msg; }
public String getMsg() { return msg; }
public void setMsg(String msg) { this.msg = msg; } }
部署war包于tomcat中
maven常规打包成war包放入tomcat Webapps目录
配置自定义过滤器
启动类中添加
创建一个Filter
```java
/**
urlPatterns支持正则
*/
@WebFilter(urlPatterns ={"/api/user/*"},filterName = "userFilter")
public class UserFilter implements Filter {
}
Servlet3.0自定义原生Listerner
spring boot2.0拦截器
CustomWebMvcConfigurer.java
UserIntercepter.java
```java public class UserIntercepter implements HandlerInterceptor {
/**
进入controller之前 */ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { System.out.println(request.getRequestURI()); System.out.println("UserIntercepter--->preHandle");
return request.getParameter("username").equals("admin"); }
/**
进入controller之后,是图渲染之前 */ @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { System.out.println("UserIntercepter--->postHandle"); }
/**
执行controller之后
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("UserIntercepter--->afterHandle");
}
}
修改启动类 增加 mapperScan扫描mapper
配置数据源
编写mapper
使用#替代$ 防止sql注入
service调用
controller调用
其它mapper
事务
事务出错回滚
Redis
相关链接
linux下``` 下载 wget http://download.redis.io/releases/redis-5.0.5.tar.gz
解压 tar xzf redis-5.0.5.tar.gz
编译源码 cd redis-5.0.5 make
启动服务 ./src/redis-server
连接服务 ./src/redis-cli
测试 set test hello-world get test
简单使用
Redis桌面应用
封装工具类
redis操作工具类
json与 Object相互转换
jsonData回调模板
定时任务与异步任务处理
定时任务
启动类增加新注解
使用 (testTimerTask.class)
@fixedRate 多久执行一次
@fixedDelay 在上一次执行结束之后再隔 多久执行一次
@fixedDelayString 采用字符串作为参数
异步任务
启动类添加注解
异步任务类
业务调用
等待异步回调完成
Last updated
Was this helpful?