Java 如何定义spring quartz targetMethod的输入参数?
我有一个方法,我想添加一个调度程序任务。问题是我有一些输入参数,我需要用这些参数调用这个方法。有办法吗 这是我的方法Java 如何定义spring quartz targetMethod的输入参数?,java,spring,Java,Spring,我有一个方法,我想添加一个调度程序任务。问题是我有一些输入参数,我需要用这些参数调用这个方法。有办法吗 这是我的方法 public class RunMeTask { public void printMe(List<StudentType> myList) { System.out.println("test"); } } public enum StudentType{ SENIOR, GRADUATE } 公共类RunMeTask{
public class RunMeTask {
public void printMe(List<StudentType> myList) {
System.out.println("test");
}
}
public enum StudentType{
SENIOR,
GRADUATE
}
公共类RunMeTask{
public void printMe(列表myList){
系统输出打印(“测试”);
}
}
公共枚举学生类型{
级别高的
毕业
}
Bean定义
<bean id="runMeJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="runMeTask" />
<property name="targetMethod" value="printMe" />
</bean>
<bean id="runMeCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="runMeJob" />
<property name="cronExpression" value="0 0/10 * * * ?" />
</bean>
编辑:我发现我可以添加一个名为arguments的新属性,但我得到了预期的ClassCastException。我需要发送StudentType列表而不是字符串列表
java.lang.String cannot be cast to dr.domain.StudentType
<property name="arguments">
<list>
<list>
<value>SENIOR</value>
</list>
</list>
</property>
java.lang.String不能强制转换为dr.domain.StudentType
级别高的
arguments属性是方法调用的参数列表。
但是你的第一个论点是一个列表本身。所以你需要一个列表。试一试
<property name="arguments">
<list>
<list>
<value type="com.your.package...StudentType">SENIOR</value>
</list>
</list>
</property>
级别高的
您可以使用这种方法
定义一个列表,该列表将加载属性文件中的值
public class RunMeTask {
@Value("#{'${arguments}'.split(',')}")
private List<String> myList;
@Schedule(...)
public void printMe() {
//here you can convert your String list to Enum. Not sure if you can directly load the enums in the list from the properties file
System.out.println("test");
}
}
更新:
正如您所说,您不能使用属性文件或字符串。然后你可以看看这个链接,很好地解释怎么做
您在哪里收到这些参数?@cralfaro参数是常量。我的意思是runMeJob将始终使用SENIOR调用printMe方法。谢谢您的帮助,但我仍然不知道如何使用enum调用此方法。@hellzone只需通过谷歌搜索即可。e、 g.为您更新了答案。另一个解决方案-不要使用spring;)XML配置-是一个噩梦我不能使用字符串类。这对我是禁止的。我不能使用任何属性文件。@hellzone看一下更新,我在其中添加了一个链接来解释如何操作,但这改变了您的第一个类,现在您必须创建一个bean,在该bean中以编程方式定义cron触发器,并能添加所需的所有参数。但不会再次使用调度程序注释
arguments=SENIOR,JUNIOR