spring boot创建+配置


git地址
https://github.com/a18792721831/studySpringCloud.git

1.spring boot 简介

1.1 特点

    1. 自动配置
      自动配置就是程序需要什么,spring boot就会装配什么。
    1. 起步依赖
      向原来的项目中增加依赖是比较有挑战的,版本冲突等必须考虑。在springboot中,只需要增加一个依赖,这个依赖就会将其相关联的依赖都加入,并自动解决版本冲突。
    1. Actuator监控
      Actuator可以监控程序的运行。

2.创建spring boot

2.1 创建

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

2.2 项目结构

在这里插入图片描述
build.gradle是依赖管理
resources为资源文件夹
statics为静态资源
templates为模板资源
application.properties
application.yml是配置文件
SpringbootApplication为程序启动类

2.3 web项目

web项目会自动增加依赖
在这里插入图片描述
有时候maven的默认仓库下载比较慢,可以自动多个maven仓库镜像

    maven{
        url 'https://maven.aliyun.com/'
    }
    maven{
        url 'http://maven.aliyun.com/nexus/content/groups/public/'
    }
    maven{
        url 'https://repo1.maven.org/maven2/'
    }

2.4 启动类

在这里插入图片描述
@SpringBootApplication注解包含了@SpringBootConfiguration和@EnableAutoConfiguration和@ComponentScan,开启了包扫描、配置和自动配置的功能很。

我们新建一个Controller
在这里插入图片描述
绑定controller的请求地址,然后写一个方法,方法也绑定请求地址,然后启动。
接下来访问方法
在这里插入图片描述
请注意,不写方法的请求地址,也会报异常的
在这里插入图片描述

其中,@RestController注解表明这个类是一个RestController。
@RestController是spring 4.0版本的一个注解,功能相当于@Controller注解和@ResponseBody注解之和。
@RequestMapping注解是绑定请求地址映射的。

2.5 测试

在gradle项目中,我们看到,自动生成了测试类。
在这里插入图片描述
在这里插入图片描述
我们直接运行,可以看到启动了容器
在这里插入图片描述
不过这个contextLoads测试类只是为了测试自动生成或依赖没有问题,整个架构能够正确的运行。
更多的是我们自己写的,测试自己的controller,service,dao.
接下来,我们创建controller包(与main里面的同目录名,类名+Test)
在这里插入图片描述
在这里插入图片描述

    1. 为了防止端口占用导致的服务启动失败,先使用@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)随机使用端口。
    1. JUnit4可以使用@RunWith(SpringRunner.class),但是在JUnit5中是@ExtendWith(SpringExtension.class)
    1. 使用@LocalServerPort将随机端口读取进去
    1. 使用assertThat进行判断是否通过测试(否则即使失败,也是通过)
    1. 可以将初始化的操作放在init中
      在这里插入图片描述

3.配置

在spring boot 中有两种配置文件,properties和yml
在这里插入图片描述

3.1 自定义配置

在yml中配置属性
在这里插入图片描述

3.2 如何访问

首先创建一个controller
在这里插入图片描述
接下来启动
在这里插入图片描述
然后访问
在这里插入图片描述
在这里插入图片描述
接下来创建测试类
在这里插入图片描述

3.3 配置赋值到实体

在配置文件中增加
在这里插入图片描述
接下来创建对应的bean
在这里插入图片描述
在这里插入图片描述
创建访问controller
在这里插入图片描述
启动请求
在这里插入图片描述
写测试类
在这里插入图片描述
通过比较自动注入的和通过controller获取得到的是否一致。
在这里插入图片描述

3.4 自定义配置文件-properties

我们在resources下创建我们自己的配置文件
myTest.yml
在这里插入图片描述
数据结构如下:
有一个抽象类,定义name,age,value
然后扩展了三个子类:
man、women、child
最后组成了people
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
需要增加依赖
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
注意前缀。
创建controller
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
创建测试类
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
运行全部测试
在这里插入图片描述
在这里插入图片描述
全部测试通过。

3.5 自定义配置文件-yml

因为默认情况下,@PropertySource注释不适用于YAML文件。

但是从Spring 4.3开始,有可能使其工作。Spring
4.3引入了PropertySourceFactory接口。这PropertySourceFactory是一家工厂PropertySource。使用的默认实现是DefaultPropertySourceFactory创建ResourcePropertySource实例的。

编写自定义实现需要实现单个方法createPropertySource。定制实现需要做两件事:

将给定资源加载到java.util.Properties对象中 创建一个PropertySource来包装已加载的属性

要加载YAML文件,Spring提供了YamlPropertiesFactoryBean。此类将加载1个或多个文件并将其转换为java.util.Properties对象。Spring提供了一个PropertiesPropertySource包装java.util.Properties对象的[
] 。最后,PropertySource的名称是给定的或派生的。如合同中所述,派生名称是资源描述。

上述内容引用自 https://mdeinum.github.io/2018-07-04-PropertySource-with-yaml-files/

首先,创建yml解析的工厂

public class YmlPropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(@Nullable String name, EncodedResource resource) throws IOException {
        Properties propertiesFromYaml = loadYamlIntoProperties(resource);
        String sourceName = name != null ? name : resource.getResource().getFilename();
        return new PropertiesPropertySource(sourceName, propertiesFromYaml);
    }

    private Properties loadYamlIntoProperties(EncodedResource resource) throws FileNotFoundException {
        try {
            YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
            factory.setResources(resource.getResource());
            factory.afterPropertiesSet();
            return factory.getObject();
        } catch (IllegalStateException e) {
            Throwable cause = e.getCause();
            if (cause instanceof FileNotFoundException) {
                throw (FileNotFoundException) e.getCause();
            }
            throw e;
        }
    }

}

然后创建自定义的yml文件
在这里插入图片描述
注意,前缀不能重复,在所有的配置文件中,不能重复。(感觉有些不科学)
然后创建对应的实体
在这里插入图片描述
注意点
实体上的
@PropertySource(factory = YmlPropertySourceFactory.class,value = “classpath:myTest.yml”)
注解需要指定factory
然后写controller
在这里插入图片描述
最后写Test
在这里插入图片描述
测试结果
在这里插入图片描述
其余类似3.4

完整项目,见git.

发布了182 篇原创文章 · 获赞 88 · 访问量 12万+

猜你喜欢

转载自blog.csdn.net/a18792721831/article/details/104290947