目录
(1)配置繁琐
(2)依赖繁琐
(1)自动配置(核心)
(2)起步依赖(核心)
(3)辅助功能
总的来说,SpringBoot 提供了一种快速开发 Spring 项目的方式,而不是对 Spring 功能上的增强。
搭建 SpringBoot 工程,定义 HelloController.hello() 方法,返回 ”Hello SpringBoot!”。
在整个步骤中,不需要写一行配置,没有引入 Tomcat 的插件。
(1)导入 SpringBoot 起步依赖
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.7.3version>
- parent>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
-
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <version>4.12version>
- <scope>testscope>
- dependency>
- dependencies>
(2)定义 Controller
- package com.demo.controller;
-
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- @Controller
- public class HelloController {
- @RequestMapping(value = "/hello")
- @ResponseBody
- public String hello() { // 在 /hello 页面输出 hello, springboot!
- System.out.println("/hello");
- return "hello, springboot!";
- }
- }
(3)编写引导类
- package com.demo.application;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class HelloApplication {
-
- }
(4)编写测试代码
- public class HelloApplicationTest {
- @Test
- public void test1() {
- SpringApplication.run(HelloApplication.class);
- }
- }
- package com.demo.application;
-
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class HelloApplication {
- public static void main(String[] args) {
- SpringApplication.run(HelloApplication.class, args);
- }
- }
当我们在 main 方法中调用 run 方法后,该如何访问服务器呢?
在输出的信息中其实有这么一些信息:

显然 Tomcat 服务器已经在 8080 端口开放了,直接访问即可。

如果我们连工程需要引入什么样的依赖都不想自己动手写,那么可以直接使用 SpringBoot 的生成器。但是需要一个联网环境。
(1)新建,选择 SpringInitial

(2)直接选择我们需要的依赖

(3)项目结构

在入门案例中,我们写的 POM 文件里,spring-boot-starter-web 是没有规定版本号的。
这是因为,在 SpringBoot 的父工程里,已经设定了大量的依赖的版本号,这也是 SpringBoot 能防止依赖冲突的原因之一。
这是非常重要的一个问题,主启动类的位置决定了 SpringBoot 自动扫描的范围。
SpringBoot是基于约定的,所以很多配置都有默认值,但如果想使用自己的配置替换默认配置的话,就可以使用 application.properties 或者application.yml (application.yaml)进行配置。(yml 和 yaml 是一样的)

(1)语法的大致区别
prop.driver=110
- prop:
- port: 110
(2)简单使用

(3)加载顺序(优先级)
当 properties、yml、yaml 都存在 application 配置文件时,加载顺序为:
properties > yml > yaml
此时,在低优先级中的同一个名称的属性就会失效。
YAML全称是YAML Ain't Markup Language。

可以发现,properties 不方便看出层级关系;xml 能看出层级关系,但是写标签繁琐;yaml 只需要相同缩进,就能得出层级关系。


(1)示例

(2)参数引用
读取配置文件的方法有好几种:
具体使用:https://blog.csdn.net/qq_31960623/article/details/116902786
(1)application.yml 文件:
- address:
- - beijing
- - wuhan
- - shanghai
-
- myUser:
- name: wyt
- age: 20
- hobby:
- - c++
- - python
- - java:
- web: 'java \n web'
- spring: "spring \n springboot"
- address: ${address[1]}
(2)Controller 代码:
- @Value(value = "${myUser.hobby[2].java.web}")
- private String web;
-
- @Value(value = "${myUser.hobby[2].java.spring}")
- private String spring;
-
- @Value(value = "${myUser.address}")
- private String address;
-
- @RequestMapping(value = "/testAtValue")
- @ResponseBody
- public String testAtValue(@Value(value = "${myUser.name}") String username) {
- System.out.println("username = " + username);
- System.out.println("web = " + web);
- System.out.println("spring = " + spring);
- System.out.println("address = " + address);
- return "testAtValue";
- }
(3)输出结果:

(4)结果分析:
(1)Controller 代码:
- @Autowired
- private Environment environment;
-
- @RequestMapping(value = "/testEnvironment")
- @ResponseBody
- public String testEnvironment() {
- for (int i = 0; i <= 2; ++ i) {
- System.out.println("address1 = " + environment.getProperty("address[" + i + "]"));
- }
- return "testEnvironment";
- }
使用这个注解,可以将配置文件中的对象,对应地注入到一个 POJO 对象中。
(1)编写 POJO 实体类
- package com.demo.pojo;
-
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
-
- @Component
- @ConfigurationProperties(prefix = "person")
- public class Person {
- private String name;
- private Integer age;
- private String[] address;
- }
(2)application.yml 文件:

(3)测试代码:
- @Autowired
- private Person person;
-
- @RequestMapping(value = "/testConfiguration")
- @ResponseBody
- public String testConfiguration() {
- System.out.println("name = " + person.getName());
- System.out.println("age = " + person.getAge());
- for (String s : person.getAddress()) {
- System.out.println("address = " + s);
- }
- return "testConfiguration";
- }
(4)输出结果

(5)给 yml 添加提示功能
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-configuration-processorartifactId>
- <optional>trueoptional>
- dependency>

(1)username
或许有一个疑问,在上面 @Value 中的 yml 配置文件里,为什么写 myUser,而不是直接写个 user 呢?
像数据库连接池需要使用 username 时,就要考虑这个问题了:https://blog.csdn.net/weixin_48841931/article/details/126671315
(2) @ConfigurationProperties 前缀命名问题
(3)@ConfigurationProperties 添加后,yml 还是没有提示
其中一种原因是编码问题:https://blog.csdn.net/gaogzhen/article/details/107348314
我们在开发 SpringBoot 应用时,通常同一套程序要经过好几个环节,会被安装到不同的环境。
不同环境(开发、联调、预发、正式等)所需的配置不同,如果每改变一个环境就更改配置不但麻烦(修改代码、重新构建)而且容易出错。
profile 的功能就是来进行动态配置切换的。
(1)多 profile 文件方式(properties),也是多 yml 文件方式
(2)yml 多文档方式(不是多 yml 文件方式)
profile 的激活方式有三种:配置文件、虚拟机参数、命令行参数。
下面以 配置文件 为例子,说明 profile 激活。
(1)创建配置文件
配置各个环境下对应的 properties 配置文件,并为他们设置不同的服务器端口( server.port = xxxx)。各后缀意义如下:

(2)激活配置
当我们创建好这几个配置文件后,启动 SpringBoot,会发现服务器端口还是 8080。这是因为还没有激活任一配置。



(1)创建配置文件

(2)激活配置

(3)文件代码
- ---
- server:
- port: 8081
- spring:
- config:
- activate:
- on-profile: development
- ---
- server:
- port: 8082
- spring:
- config:
- activate:
- on-profile: product
- ---
- server:
- port: 8083
- spring:
- config:
- activate:
- on-profile: test
- ---
- spring:
- profiles:
- active: development
-
(4)启动结果

(1)虚拟机参数

(2)命令行参数


(1)Demo-Profile-0.0.1-SNAPSHOT.jar中没有主清单属性
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>

(1)引入依赖
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- <dependency>
- <groupId>junitgroupId>
- <artifactId>junitartifactId>
- <scope>testscope>
- dependency>
(2)创建测试类(一个 Service)

(3)@RunWith 注解作用
@RunWith 就是一个运行器;
(4)@SpringBootTest(classes = 启动类名称.class)
基本等同于启动了整个服务,此时便可以开始功能测试。


(1)引入依赖
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redisartifactId>
- dependency>
(2)本机地址测试
- package com.demo.demoredis;
-
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.data.redis.core.RedisTemplate;
-
- @SpringBootTest
- class DemoRedisApplicationTests {
- @Autowired
- // 本机才不用配置端口等信息
- private RedisTemplate redisTemplate;
-
- @Test
- void contextLoads() {
- }
-
- @Test
- public void testSet() {
- redisTemplate.boundValueOps("name").set("wyt");
- }
-
- @Test
- public void testGet() {
- System.out.println(redisTemplate.boundValueOps("name").get());
- }
-
- }
(3)通过配置文件修改 ip 和 port

(1)引入 MyBatis 起步依赖、PostgreSQL 驱动依赖
- <dependency>
- <groupId>org.mybatis.spring.bootgroupId>
- <artifactId>mybatis-spring-boot-starterartifactId>
- <version>2.3.1version>
- dependency>
-
- <dependency>
- <groupId>org.postgresqlgroupId>
- <artifactId>postgresqlartifactId>
- <scope>runtimescope>
- dependency>
(2)建立相关表信息

(3)配置数据源

(4)注解开发
- @Mapper
- @Repository
- public interface UserMapper {
- @Select("select * from \"MyUser\";")
- List
queryForAll(); - }
(5)测试及其结果
- @Test
- public void test() {
- List
userList = userMapper.queryForAll(); - System.out.println(userList);
- }

(6)注解开发小结

(1)是否必须添加 @RunWith 注解
其实不一定非要添加,主要看导入的 @Test 的包是哪一个:https://www.bmabk.com/index.php/post/121982.html