SpringBoot中配置文件和实体类映射

  • 方式一

    • 1.Controller上面配置@PropertySource({"classpath:resource.properties"})

    • 2.增加属性@Value

      @Value("${test.name}")
      private String name;
      
  • 方式二:实体类配置文件

    • 1.添加@Component注解
    • 2.使用@PropertySource注解指定配置文件位置
    • 3.使用@ConfigurationProperties注解,设置相关属性
    • 4.通过@Autowired自动注入实体类,获取映射的配置文件值
    @Configurable
    @Component
    @PropertySource(value = "classpath:resource.properties")
    @ConfigurationProperties(prefix = "test")
    public class TestConfig {
      // 属性
      // getter、setter方法
    }
    
posted @ 2021-03-02 16:13  Gen2021  阅读(658)  评论(0)    收藏  举报