天天看点

Spring boot——约定优于配置

目录

        • spring boot 解决了spring的问题
      • 单元测试和热部署
        • 单元测试
        • 热部署
        • application.properties文件配置
        • 引入自定义配置文件
        • 随机数设置及参数间引用
        • spring boot 项目启动流程图
        • Spring boot视图技术
          • 标签
          • 标准表达式
          • 多语言国际化配置文件

spring boot 官方 guide

高人写的 spring boot 学习材料

spring boot是基于spring开发项目的起点。spring boot的设计是为了让你尽可能快的跑起来你的应用程序而尽可能减少你的配置文件。

spring boot 解决了spring的问题

  1. 起步依赖(对pom.xml的优化):
    • 使用spring boot只需要在pom文件里添加一个stater依赖即可。操作方便
  2. 自动配置
    • spring boot会自动将一些配置类的bean注册进ioc容器,我们只需要在需要使用的地方用

      @Autowired

      或者

      @Resourse

      注解来注入。
    • spring boot项目会把一些默认的内容进行配置,在使用过程中一般情况下不需要进行配置,除非要进行个性化配置。

单元测试和热部署

单元测试

  1. pom.xml
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-starter-test</artifactId>
    	<scope>test</scope>
    </dependency>
               
  2. Test类上添加
    @RunWith(SpringRunner.class) // 测试启动器,并加载Spring Boot测试注解
    @SpringBootTest
               
  3. 测试方法内如果需要使用ioc容器中的bean,需要在Test类中使用

    @Autowired

    进行注入。

热部署

  1. pom.xml
    <!-- 引入热部署依赖 -->
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-devtools</artifactId>
    </dependency>	
               
  2. 打开项目自动编译
    【File】=>【Settings】=>【Compiler】=>打开

    Build project automatically

    选项
  3. 打开程序运行时子自动编译功能

    Ctrl+Shift+Alt+/

    打开Maintenance选项框,并打开

    Registry

    页面,勾选

    compiler.automake.allow.when.app.running

    复选框
  4. 可能有的版本需要在程序代码更新后,手动点击

    build

    按钮

application.properties文件配置

  1. pojo配置application.properties内容提示
  2. pom.xml
    <dependency>
    	<groupId>org.springframework.boot</groupId>
    	<artifactId>spring-boot-configuration-processor</artifactId>
    	<optional>true</optional>
    </dependency>
               
  3. 在pojo类上添加注解

    @Component

    @ConfigurationProperties(prefix="person")

  4. 在application.properties文件中,输入前缀person时,会在后面进行配置提示。

引入自定义配置文件

  1. java类中如果需要读取配置名称不是application开头的配置文件的内容时需要添加如下配置

随机数设置及参数间引用

  1. 随机数设置
my.secret=${random.value} // 配置随机值
my.number=${random.int} // 配置随机整数
my.bignumber=${random.long} // 配置随机long类型数
my.uuid=${random.uuid} // 配置随机uuid类型数
my.number.less.than.ten=${random.int(10)} // 配置小于10的随机整数
my.number.in.range=${random.int[1024,65536]} // 配置范围在[1024,65536]之间的随机整数
           
  1. 参数间引用
app.name=MyApp
app.description=${app.name} is a Spring Boot application
           

spring boot 项目启动流程图

  1. SpringApplication实例的初始化创建
    1. 把项目启动类.class设置为属性存储起来
    2. 判断当前webApplicationType应用的类型
    3. 设置监听器(Listener)
    4. 用于判断并设置项目main()方法启动的主程序启动类
  2. 项目的初始化启动
    1. 获取并运行监听器
    2. 根据SpringApplicationRunListener以及参数来准备环境
    3. 创建Spring容器
    4. Spring容器前置处理
    5. 刷新容器
    6. Spring容器后置处理
    7. 发出结束执行的事件
    8. 执行Runners
      Spring boot——约定优于配置

Spring boot视图技术

Thymeleaf模板文档

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
	<head>
		<meta charset="UTF-8">
		<link rel="stylesheet" type="text/css" media="all"
		href="../../css/gtvg.css" th:href="@{/css/gtvg.css}" />
		<title>Title</title>
	</head>
	<body>
		<p th:text="${hello}">欢迎进入Thymeleaf的学习</p>
	</body>
</html>
           
标签
th:标签 说明
th:insert 布局标签,替换内容到引入的文件
th:replace 页面片段包含(类似JSP中的include标签)
th:each 元素遍历(类似JSP中的c:forEach标签)
th:if 条件判断,如果为真
th:unless 条件判断,如果为假
th:switch 条件判断,进行选择性匹配
th:case 条件判断,进行选择性匹配
th:value 属性值修改,指定标签属性值
th:href 用于设定链接地址
th:src 用于设定链接地址
th:text 用于指定标签显示的文本内容
标准表达式
说明 表达式语法
变量表达式 ${…}
选择变量表达式 *{…}
消息表达式 #{…}
链接URL表达式 @{…}
片段表达式 ~{…}

pom.xml 引入依赖

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

application.properties

spring.thymeleaf.cache = true #启用模板缓存
spring.thymeleaf.encoding = UTF-8 #模板编码
spring.thymeleaf.mode = HTML5 #应用于模板的模板模式
spring.thymeleaf.prefix = classpath:/templates/ #指定模板页面存放路径
spring.thymeleaf.suffix = .html #指定模板页面名称的后缀
           
多语言国际化配置文件

在项目的类路径resources下创建名称为i18n的文件夹,并在该文件夹中根据需要编写对应的多语

言国际化文件login.properties、login_zh_CN.properties和login_en_US.properties文件

login.tip=请登录
login.username=用户名
login.password=密码
login.rememberme=记住我
login.button=登录
           

application.properties

# 配置国际化文件基础名
spring.messages.basename=i18n.login