文章目录
SpringBoot概述
SpringBoot提供了一种快速使用Spring的方式,基于约定优于配置的思想,可以让开发人员不必在配置与逻辑业务之间进行思维的切换,全身心的投入到逻辑业务的代码编写中,从而大大提高了开发的效率,一定程度上缩短了项目周期。2014年4月,Spring Boot 1.0.0发布。Spring的顶级项目之一(https://spring.io)。
Spring缺点
SpringBoot功能
小结
SpringBoot快速入门
<!--springboot工程需要继承的父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencies>
<!--web开发的起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>aliyun-maven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun-maven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
</pluginRepository>
</pluginRepositories>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
/**
* 引导类。 SpringBoot项目的入口
*/
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class,args);
}
}
HelloController
package com.itheima.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return " hello Spring Boot !";
}
}
war部署
<packaging>war</packaging>
//继承 SpringBootServletInitializer
@SpringBootApplication
public class HelloApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class,args);
}
//重写 HelloApplication
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(HelloApplication.class);
}
}
<build>
<!--指定打包的名称-->
<finalName>springboot</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
使用Spring Initializr 快速创建项目
SpringBoot起步依赖原理分析
SpringBoot配置
配置文件分类
yaml
基本语法
数据格式
参数引用
小结
读取配置文件内容(3种)
name: abc
person:
name: ${name}
age: 20
address:
- beijing
- shanghai
person2: {name: zhangshan,age: 20}
address1:
- beijing
- shanghai
address2: [beijing,shanghai]
msg1: 'hello \n world'
msg2: "hello \n world"
@Value
@Value("${name}")
private String name;
@Value("${person.name}")
private String name2;
@Value("${person.age}")
private int age;
@Value("${address1[0]}")
private String address1;
@Value("${msg2}")
private String msg2;
Environment
@Autowired
private Environment env;
env.getProperty("person.name")
env.getProperty("address[0]")
@ConfigurationProperties
@Component
@ConfigurationProperties(prefix="person")
@Data
public class Person {
private String name;
private int age;
private String[] address;
}
@Autowired
private Person person
String[] address = person.getAddress();
红条提示,添加后再SpringBoot配置文件中编写时就会有提示了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
过滤器中获取配置文件内容
@Override
public void init(FilterConfig filterConfig) throws ServletException {
String cas= PlatApplicationContext.getProperty("auth.cas");
isAuthfilter="true".equals(cas)?true:false;
ServletContext servletContext = filterConfig.getServletContext();
WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
userId= ctx.getEnvironment().getProperty("userId");
}
jsp读取配置文件内容
1.JSP页面引入jstl中的fmt标签
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
2.使用fmt:setBundle加载properties文件
<!-- Demo为不带properties扩展名的文件名,如Demo.properties;var为存储该配置文件的变量名 -->
<fmt:setBundle basename="Demo" var="demo" />
3.使用fmt:message读取配置值
<!-- key为配置文件中的属性名;var为存储该配置值的变量名;demo为上一步中存储配置文件的变量名 -->
<fmt:message key="hello" var="test" bundle="${demo}" />
4.使用EL表达式读取配置值
5.给js变量赋值,一定要放在单引号中
var hello='${test}'
profile (环境切换)
配置方式
- 多文件方式
- 多文档方式
配置文件加载顺序
-
项目内加载顺序
-
项目外加载顺序
命令行参数 如–server.port=8082 以空格分割
命令行加载指定配置文件--spring.config.location=e://application.properties
自动加载与jar同级目录下config下得application.properties
自动加载与jar同级目录下application.properties