Springboot配置文件映射

添加类和配置文件的映射:

1.定义映射类

@Component

@PropertySource("classpath:config/XX.properties")

public class ConfigClass{

  @Value("$name")

  public String userName;

... ...

}

2. properties文件内容

XX.properties:

name=Lorry

... ...

3使用如下:

public class RestController{

  @Autowired

  ConfigClass config;

  public void method(){

    logger.info(config.userName);

  }

}

4. 说明

即可,@Component作用是被spring发现,@PropertySource是指定映射文件,@Value指定映射字段。使用类只要声明为@Autowired即可。

猜你喜欢

转载自www.cnblogs.com/xiashiwendao/p/9818485.html
今日推荐