SpringBoot进阶-2

一、统一集成框架 

spring boot是使用的是(slf4j+ logback)但是底层的spring是使用的JCL   hibernate是使用的 jboss

如何统一

‘抛弃’原来的框架通过框中的jar包与slf4j进行整合

 1.spring boot是使用的是(slf4j+ logback)

 2.springboot在底层把其他jar包转换成了SLF4J

 3.中间替换包

 4.如果引入其他框架,首先要移除这些框架所依赖的日志jar

    spring boot能适配所有的日志,而且底层使用的是slf4j+ logback的方式记录,如果引入其他框架,首先要把引入的框架的日志框架移除换成中间包。

  springboot已经默认配置好了日志框架

5.日志级别

trace > debug > info > warn > error
/*
	* 记录器
	* */
	Logger logger =  LoggerFactory.getLogger(getClass());
	@Test
	public void contextLoads() {
		logger.trace("这是trace日志");
		logger.debug("这是debug日志");
		logger.info("这是info日志");
		logger.warn("这是warn日志");
		logger.error("这是error日志");
	}

springboot 默认的是info级别  我们可以修改默认的日志级别后他就会输出级别以后的所有日志级别的信息

logging.level.com.example.demo = trace

 日志文件

 

logging.level.com.example.demo = trace
#在当前项目文件下生成日志文件,也可以指定盘符  比如桌面
logging.file=spring.log
#在当前磁盘的根路径下创建spring文件夹下创建log文件夹,并使用spring.log来记录日志
#logging.path=/spring/log
#指定在控制台输出的格式
logging.pattern.console=%d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n
#指定在文件中输出的格式
logging.pattern.file=%d{yyyy-MMM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{15} - %msg%n

 格式发生了改变

其他日志配置参考连接

二、springboot web开发

         一、webjars和静态资源映射规则

               原码中对静态资源的处理

public void addResourceHandlers(ResourceHandlerRegistry registry) {
			if (!this.resourceProperties.isAddMappings()) {
				logger.debug("Default resource handling disabled");
				return;
			}
			Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
			CacheControl cacheControl = this.resourceProperties.getCache()
					.getCachecontrol().toHttpCacheControl();
			if (!registry.hasMappingForPattern("/webjars/**")) {
				customizeResourceHandlerRegistration(registry
						.addResourceHandler("/webjars/**")
						.addResourceLocations("classpath:/META-INF/resources/webjars/")
						.setCachePeriod(getSeconds(cachePeriod))
						.setCacheControl(cacheControl));
			}
			String staticPathPattern = this.mvcProperties.getStaticPathPattern();
			if (!registry.hasMappingForPattern(staticPathPattern)) {
				customizeResourceHandlerRegistration(
						registry.addResourceHandler(staticPathPattern)
								.addResourceLocations(getResourceLocations(
										this.resourceProperties.getStaticLocations()))
								.setCachePeriod(getSeconds(cachePeriod))
								.setCacheControl(cacheControl));
			}
		}

 1.所有/webjars/**的请求都到   /META-INF/resources/webjars/   中去找

webjars网站连接

2./**可以访问当前目录下的所有资源

     classpath:/META-INF/resources/

     classpath:/resources/

     classpath:/static/

     classpath:/public/

          二、模板引擎

                         1.引入thymeleaf

                               可在maven库中搜索thymeleaf 得到以下代码加入到pom.xml中 等IDE加载完毕即可使用  

                               spring.thymeleaf.mode = LEGACYHTML5

spring.thymeleaf.mode的默认值是HTML5,其实是一个很严格的检查,改为LEGACYHTML5可以得到一个可能更友好亲切的格式要求

需添加一个额外的库相搭配

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            <version>1.9.22</version>
        </dependency>

                          2.thymeleaf语法

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>hello</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
    
</body>
</html>

                             Ⅰ、须将html页面放在classpath:/templates/ 路径下(src/main/resources/templates)

                             Ⅱ、入门语法

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值