第一个springboot项目

依赖:

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
        </dependency>
    </dependencies>
更改端口号 可以通过debug: true查看哪些配置生效
server:
  port: 8868

给对象注入值

实体类:

@Component
@ConfigurationProperties(prefix = "person")//对应yaml文件
public class Person {
    private String name;
    private Date btr;
    private Map map;
    private List list;
    private Dog dog;}

yaml配置文件:

person:
  name: 德莉莎
  btr: 2020/02/02
  map: {k1: 1,k2: 2}
  list: [1,2,3]
  dog:
    name: ${random.uuid} #生产uuid随机字符串
    age: ${random.int} #生产随机数字

name-i 可与 nameI 进行松散绑定<驼峰原则>

jsr303数据校验

实体类:

@Component
@ConfigurationProperties(prefix = "userurl")
@Validated //开启数据校验
public class Url {

    private String name;
    @Email()//数据校验内容为email
    private String url;}

yaml配置:

userurl:
  name: hahaha
  url: www.ss@qq.com    此属性进行数据校验在实体类中 JSR303

方式一:多环境指定配置 例:现有环境application.yaml, application-test.yaml, application-dev.yaml使用"—分割不同环境"
spring:
  profiles:
    active: test  #此处只写-后面的标识,指的是application-test.yaml 默认使用application.yaml

# 方式二:多环境指定配置
# 在同一.yaml文件中使用---分割不同环境配置  例:
---
spring:
  profiles: dev  #环境名

server:
  port: 8080

---
spring:
  profiles: test  #环境名

server:
  port: 8080

---
spring:
  profiles: run  #环境名

server:
  port: 8080
# 指定:在第一个环境中指定,如下:
spring:
  profiles:
    active: test  #此处只写-后面的标识,指的是application-test.yaml 默认使用application.yaml

自动装配的原理:

精髓:
1)、SpringBoot启动会加载大量的自动配置类
2)、我们看我们需要的功能有没有在SpringBoot默认写好的自动配置类当中;
3)、我们再来看这个自动配置类中到底配置了哪些组件; (只要我们要用的组件存在在其中, 我们
    就不需要再手动配置了)
4)、给容器中自动配置类添加组件的时候,会从properties类中获取某些属性。
    我们只需要在配置文件中指定这些属性的值即可;
xxxAutoConfigurartion:自动配置类;给容器中添加组件
xxxProperties:封装配置文件中相关属性;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值