栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot配置注解

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot配置注解

在ymal中配置的资源怎么被读取生效的?

spring:
  #基础配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://mysql:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
    username: root
    password: root
#打印日志,即输出mysql数据库表中的详细信息
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
people:
  id: 1
  name: zhangsan
  tel: 15451351351

1.创建People实体类并添加@ConfigurationProperties注解

@Data
//读取了yaml配置中的people
@ConfigurationProperties(prefix = "people")
public class People {
    private Integer id;
    private String name;
    private String tel;
}

2.为了使上面实体类的@ConfigurationProperties注解生效,还需要创建一个配置类PeopleConfig

@Configuration
//为了使使用 @ConfigurationProperties 注解的类生效。
@EnableConfigurationProperties(People.class)
public class PeopleConfig {
}

3.test

@Test
    public void test1(){
        ConfigurableApplicationContext run = SpringApplication.run(MybatisplusAutoApplication.class);
        System.out.println(run.getBean(People.class));
    }

输出

@EnableConfigurationProperties使使用 @ConfigurationProperties 注解的类生效。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/325476.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号