properties控制定时器Scheduled的总开关,以及获取properties公共方法

在配置文件中设置一个属性名,对应开关的状态
scheduling.enable=false
@Component
@Lazy(false)
@ConditionalProperty(prefix = "scheduling", name = "enable", havingValue = "true")

@ConditionalOnProperty来控制Configuration是否生效,从application.properties中读取某个属性值,如果该值为空,默认值为true

 

import com.hisome.exampt.timedTask.StdHySendTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * mango
 *
 */
public class PropertyUtil {
    private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
    private static Properties props;
    static{
        loadProps();
    }

    synchronized static private void loadProps(){
        logger.info("开始加载properties文件内容.......");
        props = new Properties();
        InputStream in = null;
        try {
//<!--第一种,通过类加载器进行获取properties文件流-->
                    in = PropertyUtil.class.getClassLoader().getResourceAsStream("application.properties");
// <!--第二种,通过类进行获取properties文件流-->
                    //in = PropertyUtil.class.getResourceAsStream("/jdbc.properties");
                    props.load(in);
        } catch (FileNotFoundException e) {
            logger.error("application.properties文件未找到");
        } catch (IOException e) {
            logger.error("出现IOException");
        } finally {
            try {
                if(null != in) {
                    in.close();
                }
            } catch (IOException e) {
                logger.error("application.properties文件流关闭出现异常");
            }
        }
        logger.info("加载properties文件内容完成...........");
        logger.info("properties文件内容:" + props);
        StdHySendTask s= new StdHySendTask();
    }

    public static String getProperty(String key){
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key);
    }

    public static String getProperty(String key, String defaultValue) {
        if(null == props) {
            loadProps();
        }
        return props.getProperty(key, defaultValue);
    }


    public static void update(String key, String value) {
        props.setProperty(key, value);
        FileOutputStream oFile = null;
        try {
            oFile = new FileOutputStream("application.properties");
            //将Properties中的属性列表(键和元素对)写入输出流
            props.store(oFile, "");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                oFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

获取配置内容,properties在启动时加载一次

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
@Scheduled注解可以用于创建定时任务。在方法上加上@Scheduled注解,可以将这个方法定义为一个定时任务,并且可以使用cron表达式来控制任务的执行时间。为了开启定时任务,需要在类上添加@EnableScheduling注解。定时任务的执行过程包括解析注解参数并将其放入定时任务列表中,然后在定时任务列表中统一执行相应的任务。定时任务的执行顺序按照cron表达式的配置来判断执行时间,计算出下次执行时间并放入线程中,到了执行时间就会执行任务。此外,可以通过fixedRate和fixedDelay来配置任务的间隔时间,而cron表达式则可以配置任务在具体时刻执行。根据任务的需求,建议使用fixedRate和fixedDelay来强调任务间隔,而使用cron表达式来强调任务在特定时刻执行。配置文件中可以对定时任务进行相应的配置。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Java定时器 @Scheduled注解的使用](https://blog.csdn.net/m0_64416017/article/details/129582204)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [@Scheduled定时器](https://blog.csdn.net/zqy123456_123/article/details/127845614)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值