YAML语法
键: 值(中间有一个空格,否则不生效)
使用缩进来控制层级关系,只要缩进量相同,那么它们就是一个层级的
属性和值对大小写敏感
server:
port: 8081
servlet:
context-path: /hello
使用""双引号表示字符串时,\n会换行
表示对象关系时,注意缩进,两种方式都可以。
person:
name: "lxy"
age: 20
----------------
person: {name: "lxy",age: 20}
SpringBoot
测试时在头加入这段代码
@RunWith(SpringRunner.class)
@SpringBootTest
1、引入外部配置文件@PropertySource("classpath:文件名"),必须加上classpath,否则不成功。
2、配置文件给对象赋值时使用@ConfigurationProperties(prefix = "前缀")
3、参数间引用示例。
cn.lxy.name="lixingyu"
cn.lxy.age=21
cn.lxy.person=${cn.lxy.name}:${cn.lxy.age}
4、配置文件的随机数。使用random函数。
cn.lxy.num=${random.int}
//random方法示例
secret=${random.value}
number=${random.int}
bignumber=${random.long}
uuid=${random.uuid}
number.less.than.ten=${random.int(10)}
number.in.range=${random.int[1024,65536]}
在yml中配置对象关系时,一定要加入无参构造函数,否则会出现java.lang.IllegalStateException: Failed to load ApplicationContext异常。
使用@Value("${yml文件中的键}")才能在class中映射yml中的值。
配置文件注入时,需要在对象头添加
@Component
@ConfigurationProperties(prefix = "xxx")
@ConfigurationProperties和@Value的区别。
@ConfigurationProperties | @Value | |
---|---|---|
功能 | 可以批量导入 | 需要一个一个指定 |
松散绑定 | 不支持 | 支持 |
数据校验 | 支持 | 部支持 |
复杂类型封装 | 支持 | 不支持 |
使用场景:
@ConfigurationProperties:需要绑定多个参数时使用。
@Value:只需要绑定单个参数时使用。
@PropertySource:加载指定配置文件。
@ImportResource:导入Spring的配置文件,如bean.xml等。
使用注解方式给Spring容器添加组件
Custom.java:
@Configuration
public class Custom {
@Bean
public HelloService helloService02(){
System.out.println("添加自定义组件");
return new HelloService();
}
}
HelloService.java:
public class HelloService {
}
日志(slf4j+logback)
示例代码
public class Log {
Logger logger = LoggerFactory.getLogger(Log.class);
@Test
public void log(){
logger.trace("trace....");
logger.debug("debug....");
logger.info("info");
logger.warn("warn");
logger.error("error");
}
}
yml配置
logging:
#设置日志级别
level.cn.lixingyu: trace
#设置日志路径
file: D:/log.log
pattern:
#设置日志在控制台输出格式
console: %d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
#设置日志在文件中输出格式
file: %d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} ==== %msg%n
properties配置
#设置日志级别
logging.level.cn.lixingyu=warn
#设置日志路径
logging.path=D:/
#设置日志在控制台输出格式
logging.pattern.console=%d{yyyy-MM-dd} [%thread] %-5level %logger{50} - %msg%n
#设置日志在文件中输出格式
logging.pattern.file=%d{yyyy-MM-dd} === [%thread] === %-5level === %logger{50} ==== %msg%n
日志输出格式
%d表示日期时间,
%thread表示线程名,
%-5level:级别从左显示5个字符宽度
%logger{50} 表示logger名字最长50个字符,否则按照句点分割。
%msg:日志消息,
%n是换行符
例如:%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
Web
静态资源映射
使用webjars
引入相应的静态资源文件的maven模块后
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>
配置静态资源目录
SpringBoot中默认配置是"/**",表示,访问的任何资源,都会在对应的静态资源目录去查找资源。
"classpath:/META-INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径
也可以使用配置文件的方式
yml:
spring:
resources:
static-locations: /hello/
properties:
spring.resources.static-locations=/hello/
欢迎页
SpringBoot默认在/**(即所有资源目录中查找)index.html文件。
页面图标favicon.ico
SpringBoot默认在/**(即所有资源目录中查找)favicon.ico文件。
Thymeleaf
使用名称空间
<html lang="en" xmlns:th="http://www.thymeleaf.org">
细节点
1、@Controller
一定要使用@Controller,不能使用@RestController,否则return "xxx"的时候页面只会显示xxx,不会显示模板引擎渲染出的数据。
2、@{}语法
尽量在url中使用@{},能够获取绝对路径,防止url加上了其他字符。
3、禁用缓存
spring.thymeleaf.cache=false
语法规则
1、行内语法:[[$]]
国际化
1、国际化文件位置
resources目录下创建一个i18n的文件夹,然后在里面创建properties文件。
2、国际化文件编写格式
文件名.键=值,如图中,文件名是login,所以应该写成login.键=值。
3、配置文件
spring.messages.basename=i18n.login
Docker
安装Docker:yum install docker
启动Docker:systemctl start docker
设置开机启动:systemctl enable docker
停止docker:systemctl stop docker
搜索镜像:docker search xxx(xxx为镜像名)
拉取镜像:docker pull xxx(xxx为镜像名),指定版本:docker pull xxx:yyy(yyy为版本号)
获取下载的所有镜像:docker images
删除镜像:docker rmi 镜像id
启动镜像:docker run --name xxx -d yyy:zzz(xxx为自定义名字,yyy为镜像名,zzz为版本号,-d为后台运行)
显示已经启动的容器:docker ps
停止已经启动的容器:docker stop xxx(容器)
查看所有容器:docker ps -a
启动已经关闭的容器:docker start xxx(xxx为容器id)
删除容器:docker rm xxx(xxx为容器id)
端口映射:启动镜像时加上-p 端口0:端口1 镜像名(端口0是外部端口,端口1是内部端口)
查看容器日志:docker logs 容器名或容器id
注意事项
1、在使用docker run时,一定要把镜像名放在最后。
Bug解决
问题1、提示Not registered via @EnableConfigurationProperties or marked as Spring component。
解决:加上@Component注解。
问题2、java.lang.IllegalStateException: Failed to load ApplicationContext异常。
解决:配置对象关系时,一定要加入无参构造函数。
问题3、使用IDEA时,配置文件中的中文乱码。
解决办法: