文章目录
spirngboot依赖与注解(一)
pom文件web依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
pom文件webflux依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
pom文件热部署依赖
dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-devtools</artifactId>
<scope>runtime</scope>
</dependency>
@Value注解
@Value注解取出properties配置文件中的自定义属性
properties自定义属性
book.name = Springboot
@Value注解使用
@Value("${book.name}")
private String bookName;
properties配置文件使用随机数
#随机字符串
book.value = ${random.value}
还有random.int
, random.uuid
,random.int(1000)
等
@ConfigurationProperties(prefix = “”)与@EnableConfigurationProperties注解
@ConfigurationProperties(prefix = “”)前缀取出properties配置文件中的值
properties
book.value = ${random.value}
book.intValue = ${random.int}
book.longValue = ${random.long}
book.uuid = ${random.uuid}
使用javaBean模式给属性赋值
@ConfigurationProperties(prefix = "book")
public class BookConfigBean{
private String value;
private String intValue;
private String longValue;
private String uuid;
};
在启动类加入@EnableConfigurationProperties(BookConfigBean.class),表明启动这个配置类
在Controller使用@Autowired注入配置类
public class TestController{
@Autowried
private BookConfigBean bookConfigBean;
@GetMapping("test2")
public BookConfigBean test2(){
return bookConfigBean;
}
}
pom文件thymeleaf模板依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
pom文件nekohtml依赖
<!--去除html严格校验-->
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
<version>1.9.22</version>
</dependency>
properties文件thymeleaf配置
#thymeleaf缓存是否开启,开发时建议关闭,否则更改页面后不会实时显示效果
spring.thymeleaf.cache = false
#thymeleaf编码格式
spring.thymeleaf.encoding = UTF-8
#thymeleaf对html的校验很严格,用这个取出thymeleaf严格校验
spring.thymeleaf.mode = LEGACYHTML5
#thymeleaf模板文件前缀
spring.thymeleaf.prefix=classpath:/templates/
#thymeleaf模板文件后缀
spring.thymeleaf.suffix=.html
pom文件freemarker依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
properties文件freemarker配置
## freemarker缓存是否开启
spring.freemarker.cache = false
## freemarker编码格式
spring.freemarker.charset=UTF-8
## freemarker模板文件前缀
spring.freemarker.template-loader-path=classpath:/templates/
## freemarker模板文件后缀
spring.freemarker.suffix=.ftl
模板文件前缀
spring.freemarker.template-loader-path=classpath:/templates/
## freemarker模板文件后缀
spring.freemarker.suffix=.ftl