尚硅谷SpringBoot核心技术篇
01、SpringBoot_入门-课程简介
02、SpringBoot_入门-Spring Boot简介
(starter模块&)
03、SpringBoot_入门-微服务简介
(微服务与Spring Cloud &)
04、SpringBoot_入门-环境准备
(下载和安装Maven(settings.xml运行时指定profiles配置,告诉maven用JDK1.8来编译运行项目。启动IDEA时在settings配置文件中的Build构建工具绑定安装的Maven,并勾上Override)&)
05、SpringBoot_入门-springboot-helloworld
(@SpringBootApplication背后的秘密(启动自动导入Jar,导入springBoot的依赖 spring-boot-starter-parent,spring-boot-starter-web,通过@SpringBootApplication标注主程序类告诉是一个springBoot应用,传入主程序的class和args参数,启动spring应用,直接运行main方法启动程序。导入spring-boot-maven-plugin可以将应用打包成可执行的jar包,运行package进行打包处理,java -jar *.jar)&)
pom.xml
4.0.0
com.atguigu
spring-boot-01-helloworld
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
HelloController.java
package com;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}
}
HelloWorldMainApplication.java
package com.atguigu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @SpringBootApplication 来标注一个主程序类,说明这是一个Spring Boot应用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
// Spring应用启动起来
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
package com.atguigu.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class HelloController {
// @ResponseBody
// @RequestMapping("/hello")
// public String hello(){
// return "Hello World!";
// }
}
06、SpringBoot_入门-HelloWorld细节-场景启动器(starter)
(了解纷杂的spring—boot—starter(启动器,spring-boot-starter 导入了web模块正常运行所依赖的组件,版本springBoot会自动控制)&)
07、SpringBoot_入门-HelloWorld细节-自动配置
(理解@EnableAutoConfiguration(@EnableAutoConfiguration开启自动配置功能,)&使用import元素(给容器导入组件,)&)
08、SpringBoot_入门-使用向导快速创建Spring Boot应用
(使用 Spring Initializr(spring Initializr快速创建项目,Resource下的static文件夹保存所有的静态资源,templates保存所有的模块页面,application.properties保存spring Boot应用的配置文件,server.port=8081。Eclipse中使用Spring starter project创建spring BOOT
项目 )&@RestController注解的使用(返回的数据直接写给浏览器)&)
pom.xml
4.0.0
com.atguigu
spring-boot-01-helloworld-quick
0.0.1-SNAPSHOT
jar
spring-boot-01-helloworld-quick
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
application.properties
server.port=8081
package com.atguigu.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBoot01HelloworldQuickApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot01HelloworldQuickApplication.class, args);
}
}
package com.atguigu.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
//这个类的所有方法返回的数据直接写给浏览器,(如果是对象转为json数据)
/*@ResponseBody
@Controller*/
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello world quick!";
}
// RESTAPI的方式
}
09、SpringBoot_配置-yaml简介
(YAML配置(以数据为中心,更适合做配置文件,)&)
10、SpringBoot_配置-yaml语法
(开始一个新项目:三个示例中的Compose YAML(空格控制层级关系,空格一样在一个层级,大小写敏感,字符串默认不需要加引号,双引号不会转义字符串里面的特殊字符,单引号会进行转义只作为一个普通的字符串展示,对象还是键值对的方式,值和冒号中间一定要有空格。行内写法 friends:{lastName:zhangsan,age:19},数组或集合用-表示数组中的一个元素,行内写法pets:[cat,dog])&)
11、SpringBoot_配置-yaml配置文件值获取
(Spring Boot条件化自动装配(@ConfigurationProperties告诉springBoot将本类中的所有属性和配置文件中的相关配置进行绑定,)&注解配置(@Component)(配置文件处理器,spring-boot-configuration-processor,绑定配置文件会有提示,-与驼峰大写都可以)&)
12、SpringBoot_配置-properties配置文件编码问题
(ASCII编码查看器(Setting file encoding 设置文件编码,UTF-8->ASCII)&)
pom.xml
4.0.0
com.atguigu
spring-boot-02-config
0.0.1-SNAPSHOT
jar
spring-boot-02-config
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-configuration-processor
true
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
application.properties
server.port=8088
person.properties
person.last-name=\u674E\u56DB
person.age=12
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15
beans.xml
application-prod.properties
#server.port=80
application-dev.properties
#server.port=8082
application.yml
server:
port: 8081
spring:
profiles:
active: prod
---
server:
port: 8083
spring:
profiles: dev
---
server:
port: 8084
spring:
profiles: prod
application.properties
server.port=8081
#spring.profiles.active=dev
# idea\uFF0Cproperties\u914D\u7F6E\u6587\u4EF6utf-8
# \uFFFD\uFFFD\uFFFD\uFFFDperson\uFFFD\uFFFD\u05B5
person.last-name=\u5F20\u4E09${random.uuid}
person.age=${random.int}
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=14
person.lists=a,b,c
person.dog.name=${person.hello:hello}_dog
person.dog.age=15
package com.atguigu.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;
//@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class SpringBoot02ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot02ConfigApplication.class, args);
}
}
package com.atguigu.springboot.service;
public class HelloService {
}
package com.atguigu.springboot.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@Value("${person.last-name}")
private String name;
@RequestMapping("/sayHello")
public String sayHello(){
return "Hello "+name;
}
}
package com.atguigu.springboot.config;
import com.atguigu.springboot.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @Configuration:指明当前类是一个配置类;就是来替代之前的Spring配置文件
*
* 在配置文件中用标签添加组件
*
*/
@Configuration
public class MyAppConfig {
//将方法的返回值添加到容器中;容器中这个组件默认的id就是方法名
@Bean
public HelloService helloService02(){
System.out.println("配置类@Bean给容器中添加组件了...");
return new HelloService();
}
}
package com.atguigu.springboot.bean;
import org.hibernate.validator.constraints.Email;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Null;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* 将配置文件中配置的每一个属性的值,映射到这个组件中
* @ConfigurationProperties:告诉SpringBoot将本类中的所有属性和配置文件中相关的配置进行绑定;
* prefix = "person":配置文件中哪个下面的所有属性进行一一映射
*
* 只有这个组件是容器中的组件,才能容器提供的@ConfigurationProperties功能;
* @ConfigurationProperties(prefix = "person")默认从全局配置文件中获取值;
*
*/
//@PropertySource(value = {"classpath:person.properties"})
@Component
@ConfigurationProperties(prefix = "person")
//@Validated
public class Person {
/**
*
*
*
*/
//lastName必须是邮箱格式
// @Email
//@Value("${person.last-name}")
private String lastName;
//@Value("#{11*2}")
private Integer age;
//@Value("true")
private Boolean boss;
private Date birth;
//@Value("${person.maps}")
private Map maps;
private List lists;
private Dog dog;
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getBoss() {
return boss;
}
public void setBoss(Boolean boss) {
this.boss = boss;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map getMaps() {
return maps;
}
public void setMaps(Map maps) {
this.maps = maps;
}
public List getLists() {
return lists;
}
public void setLists(List lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
}
package com.atguigu.springboot.bean;
public class Dog {
private String name;
private Integer age;
@Override
public String toString() {
return "Dog{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
package com.atguigu.springboot;
import com.atguigu.springboot.bean.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
/**
* SpringBoot单元测试;
*
* 可以在测试期间很方便的类似编码一样进行自动注入等容器的功能
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigApplicationTests {
@Autowired
Person person;
@Autowired
ApplicationContext ioc;
@Test
public void testHelloService(){
boolean b = ioc.containsBean("helloService02");
System.out.println(b);
}
@Test
public void contextLoads() {
System.out.println(person);
}
}
13、SpringBoot_配置-@ConfigurationProperties与@Value区别
(使用@Resource和@Value配置依赖(@Value(“${person.last-name}”)获取配置文件中的信息,#{11*2}spel可以计算值)&松散索引扫描(@Value()只绑定简单的值,不支持复杂类型的绑定)&JSR303验证)
14、SpringBoot_配置-@PropertySource、@ImportResource、@Bean
(静态函数Object.getOwnPropertySymbols(..)(加载@PropertySource指定的properties配置文件,) &使用import语句导入模块(springBoot想让自己的spring配置文件生效,@ImportResource导入spring的配置文件,springBoot通过配置类给容器添加组件方式,推荐使用全注解的方式@Configuration,容器组件名字和方法名一致)&)
pom.xml
4.0.0
com.atguigu
spring-boot-02-config-02
0.0.1-SNAPSHOT
jar
spring-boot-02-config-02
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
application.properties
server.port=8083
package com.atguigu.springboot02config02;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02Config02ApplicationTests {
@Test
public void contextLoads() {
}
}
application.properties
server.port=8081
# 配置项目的访问路径
server.context-path=/boot02
#spring.config.location=G:/application.properties
package com.atguigu.springboot02config02;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBoot02Config02Application {
public static void main(String[] args) {
SpringApplication.run(SpringBoot02Config02Application.class, args);
}
}
package com.atguigu.springboot02config02.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";
}
}
15、SpringBoot_配置-配置文件占位符
(带占位符的国际化信息(${random.value} 获取随机数,${random.int[1024,65536]})&)
16、SpringBoot_配置-Profile多环境支持
(环境与profile&激活profile(application-{profile}.yml动态配置环境,spring.profiles.active=dev,---三个横杠分成不同的文档块,program arguments --spring-profiles.active=dev,命令行方式 --spring.profiles.active=dev,虚拟机参数 VM options -Dspring.profiles.active=dev)&)
17、SpringBoot_配置-配置文件的加载位置
(配置文件管理(配置文件的位置,先加载file:./config/ 文件路径下(当前项目的根路径和src或者pom.xml同级),或者file:./文件路径下,classpath:/config/类路径config下或者直接放在类路径下(resource),高优先级会覆盖低优先级。互补配置server.context-path=/boot02配置项目的访问路径, spring.config.location来改变默认配置文件的位置)&)
18、SpringBoot_配置-外部配置加载顺序
(context-path导致监控失败(外部配置文件的加载顺序,命令行参数 jar --server.port=8087,jar包外的带application-${profile}.properties(jar包和properties配置文件在同一目录下),jar包内的,jar包外的application.properties,jar包内的,@Configuration注解上的@PropertySource,通过SpringApplication.setDefaultProperties指定的默认属性)&)
19、SpringBoot_配置-自动配置原理
(条件注解@Conditional&spring-boot-autoconfigure模块分析(springBoot启动时加载主配置类,开启自动配置功能@EnableAutoConfiguration,)&)
pom.xml
4.0.0
com.atguigu
spring-boot-02-config-autoconfig
0.0.1-SNAPSHOT
jar
spring-boot-02-config-autoconfig
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.10.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
package com.atguigu.springboot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot02ConfigAutoconfigApplicationTests {
@Test
public void contextLoads() {
}
}
application.properties
# 开启SpringBoot的debug
debug=true
server.port=8080
# 我们能配置的属性都是来源于这个功能的properties类
spring.http.encoding.enabled=true
spring.http.encoding.charset=utf-8
spring.http.encoding.force=true
package com.atguigu.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBoot02ConfigAutoconfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBoot02ConfigAutoconfigApplication.class, args);
}
}
20、SpringBoot_配置-@Conditional&自动配置报告
(条件配置(@Conditional)(@Conditional后面有一个条件判断类,条件成立配置类里内容才会生效,或者才会给容器添加组件)&Conditional使用&Express实战:后端服务&Debugger(debug=true控制台打印自动配置报告,确定哪些类生效)&)
21、SpringBoot_日志-日志框架分类和选择
(Apache Commons Logging和SLF4J(LoggerFactory.getLogger())&)
22、SpringBoot_日志-slf4j使用原理
(Apache Commons Logging和SLF4J (通过适配器包装层,把commons-log转为其他对应的ja包,做为不同日志框架转为SLF4J的转环包。)&)
23、SpringBoot_日志-其他日志框架统一转换为slf4j
(Slf4j&)
24、SpringBoot_日志-SpringBoot日志关系
(选择日志框架(引入其他框架,把其他框架依赖的日志框架排除掉,不用再做任何的配置,springBoot就可以适配起来)&)
25、SpringBoot_日志-SpringBoot默认配置
(日志格式和类型(logger.trace(“”)跟踪日志,debug()调试信息,logger.info自定义信息,warn,error日志级别由低到高,logging.level.com=trace,logging.file=springBoot.log在当前项目下生产日志,logging.path在当前磁盘的根路径下生产文件)&)
26、SpringBoot_日志-指定日志文件和日志Profile功能
(输出格式化的日期和时间(如果使用Logback在类路径下使用logback-spring.xml或logback.xml,如果使用log4j,类路径下log4j2.xml,JDK的logging.properties)&)
27、SpringBoot_日志-切换日志框架
(用户自定义切换语言示例(排除log4j-over-slf4j不要把logging4j替换为slf4j,想要使用logg4j,面向slf4j编程,要用logging4j实现使用slf4j-log4j12,也可以直接引入spring-boot-starter-log4j2,二选一即可)&)
pom.xml
4.0.0
com.atguigu
spring-boot-03-logging
0.0.1-SNAPSHOT
jar
spring-boot-03-logging
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.10.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
spring-boot-starter-logging
org.springframework.boot
org.springframework.boot
spring-boot-starter-log4j2
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
package com.atguigu.springboot;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot03LoggingApplicationTests {
//记录器
Logger logger = LoggerFactory.getLogger(getClass());
@Test
public void contextLoads() {
//System.out.println();
//日志的级别;
//由低到高 trace
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?