一、profile配置
通常企业级应用都会区分开发环境、测试环境以及生产环境等等。spring提供了全局profile配置的方式,使得在不同环境下使用不同的applicaiton.properties,走不同的配置。
模板:application-{profile}.properties
示例:
程序会根据application.properties文件中配置的spring.profiles.active=xxx的值,找到对应的application-xxx.properties中具体的属性值
二、springboot运行原理
springboot关于自动配置的源码在spring-boot-autoconfigure.jar中,查看源码可以到此包。
@SpringBootApplication的核心功能其实是由@EnableAutoConfiguration注解提供,源码如下:
原理这块这篇文章讲的不错:https://www.cnblogs.com/shamo89/p/8184960.html
三、spring boot的web开发
springboot提供了spring-boot-starter-web对web开发予以支持,主要嵌入了tomcat以及springmvc的相关依赖
1、thymeleaf模板引擎
在springboot中,jsp不推荐使用,因为jsp在内嵌的servlet的容器上运行有一些问题,内嵌的tomcat不支持以jar形式运行jsp。最为推荐的则是thymeleaf,提供了完美的springmvc的支持
A、引入thymeleaf
<html xmlns:th="http://www.thymeleaf.org">
B、访问model中的数据
<span th:text="${singlePerson.name}"></span>
C、model数据迭代
<div class="panel-body"> <ul class="list-group"> <li class="list-group-item" th:each="person:${people}"> <span th:text="${person.name}"></span> <span th:text="${person.age}"></span> </li> </ul> </div>
D、数据判断
<div th:if="${not #lists.isEmpty(people)}"> <div class="panel-body"> <ul class="list-group"> <li class="list-group-item" th:each="person:${people}"> <span th:text="${person.name}"></span> <span th:text="${person.age}"></span> </li> </ul> </div> </div>
E、js中获取model数据
<script th:inline="javascript"> var single = [[${singlePerson}]]; console.log(single.name + "/" + single.age); </script>
通过<script th:inline="javascript">才能使js直接获取到model中的数据;[[${}]]获取model中的数据