Java 如何以不同的模式创建和运行jar文件,使其能够以不同的方式运行?
我是一个天真的Java J2EE开发人员。我创建了一个spring批处理应用程序,它执行以下任务:Java 如何以不同的模式创建和运行jar文件,使其能够以不同的方式运行?,java,jar,spring-batch,executable-jar,Java,Jar,Spring Batch,Executable Jar,我是一个天真的Java J2EE开发人员。我创建了一个spring批处理应用程序,它执行以下任务: 如果行数小于100,则插入10条记录 插入10条记录(不验证现有行数) 我需要以这样的方式运行Jar文件,如果我执行: java-jar HelloWorld.jar-threshold hello.properties需要执行第一步 java-jar HelloWorld.jar-force hello.properties需要执行第二步 如何创建一个jar文件,使其读取“threshold”/
java-jar HelloWorld.jar-threshold hello.properties
需要执行第一步java-jar HelloWorld.jar-force hello.properties
需要执行第二步提前感谢您可以使用配置文件定义步骤,并在运行时使用
spring.profiles.active
属性相应地加载它们。例如:
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@Configuration
@EnableBatchProcessing
public class JobWithProfiles {
@Autowired
private JobBuilderFactory jobs;
@Bean
public Job job(Step step) {
return jobs.get("job")
.start(step)
.build();
}
@Configuration
public static class Step1Config {
@Autowired
private StepBuilderFactory steps;
@Profile("threshold")
@Bean
public Step step() {
return steps.get("step")
.tasklet((contribution, chunkContext) -> {
System.out.println("executed in threshold mode");
return RepeatStatus.FINISHED;
})
.build();
}
}
@Configuration
public static class Step2Config {
@Autowired
private StepBuilderFactory steps;
@Profile("force")
@Bean
public Step step() {
return steps.get("step")
.tasklet((contribution, chunkContext) -> {
System.out.println("executed in force mode");
return RepeatStatus.FINISHED;
})
.build();
}
}
}
注意:您还可以将@Profile
注释放在配置类上
现在,如果您使用java-jar-Dspring.profiles.active=threshold HelloWorld.jar hello.properties
运行此应用程序,它应该打印:在threshold模式下执行。与force
profile相同
有关配置文件的更多详细信息,请参阅本页:
我希望这能有所帮助。查看hoe在Spring中的使用配置文件感谢您的回复@pvpkiran。但是我想在运行时更改基于Jar的行为。我假设spring概要文件名称是从application.properties读取的。如果我错了,请纠正我。