java * 和 ? 之间的区别 在春天@Scheduled(cron=".....")
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30341067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between * and ? in Spring @Scheduled(cron=".....")
提问by Jordan
I've been looking at the Spring Boot example for scheduling tasks (https://spring.io/guides/gs/scheduling-tasks/) and reading through some documentation (https://javahunter.wordpress.com/2011/05/05/cronscheduler-in-spring/) and I see that * and ? are used almost interchangeably.
我一直在查看用于调度任务的 Spring Boot 示例(https://spring.io/guides/gs/scheduling-tasks/)并阅读一些文档(https://javahunter.wordpress.com/2011/05 /05/cronscheduler-in-spring/) 我看到 * 和 ? 几乎可以互换使用。
For example, the line
例如,线
@Scheduled(cron = "0 15 10 ? * *")
and
和
@Scheduled(cron = "0 15 10 * * ?")
do the exact same thing. So what is the difference between * and ?
做完全相同的事情。那么 * 和 之间有什么区别?
采纳答案by Jens
asterix stands for all possible values. question marks should be used for non specific value
星号代表所有可能的值。问号应用于非特定值
*("all values") - used to select all values within a field. For example, "" in the minute field means *"every minute".
? ("no specific value") - useful when you need to specify something in one of the two fields in which the character is allowed, but not the other. For example, if I want my trigger to fire on a particular day of the month (say, the 10th), but don't care what day of the week that happens to be, I would put "10" in the day-of-month field, and "?" in the day-of-week field. See the examples below for clarification.
*("all values") - 用于选择字段中的所有值。例如,分钟字段中的“”表示*“每分钟”。
? (“无特定值”) - 当您需要在允许字符的两个字段之一中指定某些内容时很有用,而另一个字段则不允许。例如,如果我希望我的触发器在一个月的某一天(比如 10 号)触发,但不关心那一天是一周中的哪一天,我会将“10”放在当天-month 字段和“?” 在星期字段中。请参阅下面的示例以进行说明。
Copied from the tutorial
复制自教程
回答by Luchostein
The tutorial is outdated. The symbol ?
means exactlythe same than *
. As of Spring 3.1.2.RELEASE, the call hierarchy is:
教程已经过时了。符号?
是指恰好比相同*
。从 Spring 3.1.2.RELEASE 开始,调用层次结构是:
The constructor CronTrigger(String)
calls the constructor CronSequenceGenerator(String)
which calls parse(String)
which calls setDays(BitSet bits, String field, int max)
. Its implementation is clear:
构造函数CronTrigger(String)
调用CronSequenceGenerator(String)
调用parse(String)
which的构造函数setDays(BitSet bits, String field, int max)
。它的实现很明确:
private void setDays(BitSet bits, String field, int max) {
if (field.contains("?")) {
field = "*";
}
setNumberHits(bits, field, 0, max);
}
So, if ?
, then *
.
所以,如果?
,那么*
。