spring boot reads yml configuration

yml configuration file:

qingnianren:
  blog:
    address: https://blog.csdn.net/m0_60252632
    name: Responsibilities of young people
    brief: Young people should fully understand their responsibilities
    time: 2022-12-02 

method one: 

@value value binding

configuration

/**
 * @Value 【值绑定】
 * 在每个对应的属性上加@Value注解,注解中的内容填写yml中对应的属性,
 * 使用@Value注解时,yml中的属性名和JavaBean中的属性名可以不对应,但是数据类型需要对应
 */
@Data
@Configuration
public class BlogProperties {

    /**
     * 博客地址
     */
    @Value("${qingnianren.blog.address}")
    private String address;

    /**
     * 博客名称
     */
    @Value("${qingnianren.blog.name}")
    private String name;

    /**
     * 博客简介
     */
    @Value("${qingnianren.blog.brief}")
    private String brief;

    /**
     * 博客时间
     */
    @Value("${qingnianren.blog.time}")
    private String time;

}

test

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class BlogPropertiesTests {

    /**
     * 注入Bean
     */
    @Resource
    private BlogProperties blogProperties;

    @Test
    public void testValue() {
        log.info("博客地址:{}", blogProperties.getAddress());
        log.info("博客名称:{}", blogProperties.getName());
        log.info("博客简介:{}", blogProperties.getBrief());
        log.info("博客时间:{}", blogProperties.getTime());
    }
}

structure

Blog Address: Blog of Youth Responsibility_CSDN Blog-Field Blogger

Blog Name: Youth Responsibility

Blog Introduction: Youths Should Fully Realize Their Responsibilities

Blog time: 2022-12-02

Method 2: 

@ConfigurationProperties loose binding

Configuration:

/**
 * @ConfigurationProperties 【松散绑定】,只需要通过prefix属性指定属性参数名的前缀即可
 * 松散绑定的bean属性名需要跟yml中的属性名对应,但是不需要精确匹配,
 * yml可依据约定使用驼峰(如:qingNianRen)、"-"线(如:qing-nian-ren)、全大写加上"_"(如:QING_NIAN_REN)
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "qingnianren.blog")
public class BlogProperties02 {

    /**
     * 博客地址
     */
    private String address;

    /**
     * 博客名称
     */
    private String name;

    /**
     * 博客简介
     */
    private String brief;

    /**
     * 博客时间
     */
    private String time;
}

test:

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class BlogPropertiesTests {

    /**
     * 注入Bean
     */
    @Resource
    private BlogProperties02 blogProperties02;

    @Test
    public void testConfigurationProperties() {
        log.info("博客地址:{}", blogProperties02.getAddress());
        log.info("博客名称:{}", blogProperties02.getName());
        log.info("博客简介:{}", blogProperties02.getBrief());
        log.info("博客时间:{}", blogProperties02.getTime());
    }
}

result:

Blog Address: Blog of Youth Responsibility_CSDN Blog-Field Blogger

Blog Name: Youth Responsibility

Blog Introduction: Youths Should Fully Realize Their Responsibilities

Blog time: 2022-12-02

Method 3: 

@Environment

test:

@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class BlogPropertiesTests {

    /**
     * 注入Bean
     */
    @Resource
    private Environment environment;

    @Test
    public void testEnvironment() {
        log.info("博客地址:{}", environment.getProperty("qingnianren.blog.address"));
        log.info("博客名称:{}", environment.getProperty("qingnianren.blog.name"));
        log.info("博客简介:{}", environment.getProperty("qingnianren.blog.brief"));
        log.info("博客时间:{}", environment.getProperty("qingnianren.blog.time"));
    }
}

structure:

Blog Address: Blog of Youth Responsibility_CSDN Blog-Field Blogger

Blog Name: Youth Responsibility

Blog Introduction: Youths Should Fully Realize Their Responsibilities

Blog time: 2022-12-0

Guess you like

Origin blog.csdn.net/m0_60252632/article/details/128197455