java,springboot 在注解中读取yml文件的变量的方法

一般情况下,java springboot读取yml文件的方法

1 写yml文件

2 写一个配置相关的类

3 启动类上加标签

代码如下

文件 XXX.yml 只是举例 与实际项目无关

  index:
    indexFileRootPath: C:\Users\Administrator\Desktop\data\
    indexFileName: videoInfo.txt
    # 表示定时任务的字符串
    cronString: 0 45 3-23 * * ?
    indexStartDay: 20181211

 

@Data
@ConfigurationProperties("index")
public class VideoIndexProperties {

    /**
     * 首页文件根路径
     */
    private String indexFileRootPath;

    /**
     * 存储视频首页信息的文件名
     */
    private String indexFileName;

    /**
     * 表示定时任务的字符串
     */
    private String cronString;

    /**
     * 没有任务时开始入库的日期
     */
    private int indexStartDay;
}
/**
 * 启动类
 * @author szb
 */
@SpringBootApplication
@EnableConfigurationProperties(VideoIndexProperties.class)
@EnableScheduling
public class TestApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }

}

 然后此项目中任意其他类中 @Autowored  VideoIndexProperties videoIndexProperties 可以直接调用变量。

 但此方法不可以解决在标签中使用yml文件变量的问题。

 如配置文件中的七段表达式,正确使用方法如下

@Scheduled(cron = "${video.index.cronString}")
public void Task() {
  // 一些需要定时运行的方法,七段表达式在配置文件中
}

 

 

 

 

你可能感兴趣的:(java,java,springboot)