3.springboot(@PropertyResource的用法)

@PropertyResource 是读取 properties 属性配置文件
项目结构:

 

在 resources 目录下创建 config.properties

 创建数据类 Tiger

package com.it.springboot.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("tiger")
public class Tiger {

    @Value("${tiger.name}")
    private String name;
    @Value("${tiger.age}")
    private Integer age;

    @Override
    public String toString() {
        return "Tiger{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
修改 SystemConfig 文件

创建测试方法

 @Test
    public void test5(){
        ApplicationContext ctx=new AnnotationConfigApplicationContext(SpringConfig.class);
        Tiger tiger= (Tiger) ctx.getBean("tiger");
        System.out.println("tiger="+tiger);
    }

 测试结果

 


版权声明:本文为weixin_59334478原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。