学习项目对象:
github上高star项目:
地址:https://github.com/xkcoding/spring-boot-demo
项目说明
spring boot demo
是一个用来深度学习并实战 spring boot 的项目,目前总共包含 66 个集成demo,已经完成 54 个。
该项目已成功集成 actuator(监控)
、admin(可视化监控)
、logback(日志)
、aopLog
(通过AOP记录web请求日志)、统一异常处理(json级别和页面级别)、freemarker
(模板引擎)、thymeleaf
(模板引擎)、Beetl
(模板引擎)、Enjoy
(模板引擎)、JdbcTemplate
(通用JDBC操作数据库)、JPA
(强大的ORM框架)、mybatis
(强大的ORM框架)、通用Mapper
(快速操作Mybatis)、PageHelper
(通用的Mybatis分页插件)、mybatis-plus
(快速操作Mybatis)、BeetlSQL
(强大的ORM框架)、upload
(本地文件上传和七牛云文件上传)、redis
(缓存)、ehcache
(缓存)、email
(发送各种类型邮件)、task
(基础定时任务)、quartz
(动态管理定时任务)、xxl-job
(分布式定时任务)、swagger
(API接口管理测试)、security
(基于RBAC的动态权限认证)、SpringSession
(Session共享)、Zookeeper
(结合AOP实现分布式锁)、RabbitMQ
(消息队列)、Kafka
(消息队列)、websocket
(服务端推送监控服务器运行信息)、socket.io
(聊天室)、ureport2
(中国式报表)、打包成war文件
、集成 ElasticSearch
(基本操作和高级查询)、Async
(异步任务)、集成Dubbo
(采用官方的starter)、MongoDB
(文档数据库)、neo4j
(图数据库)、docker
(容器化)、JPA多数据源
、Mybatis多数据源
、代码生成器
、GrayLog
(日志收集)、JustAuth(第三方登录)、LDAP
(增删改查)、动态添加/切换数据源
、单机限流
(AOP + Guava RateLimiter)、分布式限流
(AOP + Redis + Lua)、ElasticSearch 7.x
(使用官方 Rest High Level Client)、HTTPS
、Flyway
(数据库初始化)。
本博客目的
在各module的学习中,把自己之前没用到的东西和觉得后面可能有用的地方记录下来(只针对自己而言,如果对各位coder也有帮助,那就是意外之喜了)
**
spring-boot-demo-helloWorld
**
hutool
说明:java工具包,个人感觉非常好用
maven引入:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
</dependency>
//使用方式:StrUtil工具类
return StrUtil.format("Hello, {}!", who);
//Dict改进 对HashMap优化,在@RestController的后台类中很好用
return Dict.create().set("applicationProperty", applicationProperty).set("developerProperty", developerProperty);
**
spring-boot-demo-properties
**
配置文件的使用方式
多配置文件切换
,可以用于生产和开发环境的切换
#在active中填入要选择的yaml文件名称
spring:
profiles:
active: prod
- 配置文件内
属性使用及和bean属性对应
之前一直用@Value
注解
@Data
@Component
public class ApplicationProperty {
@Value("${application.name}")
private String name;
@Value("${application.version}")
private String version;
}
发现另一种方式:@ConfigurationProperties
注解
@Data
@ConfigurationProperties(prefix = "developer")
@Component
public class DeveloperProperty {
private String name;
private String website;
private String qq;
private String phoneNumber;
}
说明:
1.@ConfigurationProperties 和 @value 有着相同的功能,但是 @ConfigurationProperties的写法更为方便
2.@ConfigurationProperties 的 POJO类的命名比较严格,因为它必须和prefix的后缀名要一致, 不然值会绑定不上, 特殊的后缀名是“driver-class-name”这种带横杠的情况,在POJO里面的命名规则是 下划线转驼峰 就可以绑定成功,所以就是 “driverClassName”
参考链接:https://www.cnblogs.com/tian874540961/p/12146467.html