我正在试验Sping Boot 通过-Dloader.main
参数启动提供给它的主类的能力,如in its documentation所述。
目前我们使用的是Sping Boot 2.7.13。
下面是一个执行main()
的示例 Boot 应用程序的简单shell:
@Slf4j
@SpringBootApplication
@PropertySource(value = "classpath:feeds-config.yml", factory = YamlPropertySourceFactory.class)
public class DownstreamDataFactoryApplication implements CommandLineRunner {
@Autowired
private DataAcquisitionService dataAcquisitionService;
@Autowired
private DataMarshallerService dataMarshallerService;
public static void main(String[] args) {
new SpringApplicationBuilder(DownstreamDataFactoryApplication.class)
.web(WebApplicationType.NONE)
.run(args);
}
@Override
public void run(String... args) throws Exception {
log.debug("Starting execution");
for (int i = 0; i < args.length; ++i) {
log.info("args[{}]: {}", i, args[i]);
}
//read data
Map<String, Object> context = new HashMap<>();
//Flux<?> data = dataAcquisitionService.obtainData(context);
List<?> data = dataAcquisitionService.obtainData(context);
log.info("Obtained data: "+data);
//process data
log.info("Processed data: ");
//write data
//log.debug("Report saved to {}", dataMarshallerService.getOutputFileLocation());
//log.debug("Ending execution");
log.info("Marshalled data to a file: ");
//distribute data
log.info("Send marshalled data to a remote location: ");
}
}
字符串
我的目标是在代码库中通过包结构隔离几个独立的Sping Boot 应用程序,以便根据通过-Dloader.main
传递的内容运行不同的Boot应用程序。
因此,spring-boot-maven-plugin
节中的mainClass
节被禁用:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<mainClass>
<!--
com.p.g.f.d.DownstreamDataFactoryApplication
-->
</mainClass>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M6</version>
</plugin>
型
到目前为止一切顺利
我注意到,无论是否将org.springframework.boot.loader.PropertiesLauncher
作为第二个参数传递,运行时的行为似乎完全相同。
下面是提供org.springframework.boot.loader.PropertiesLauncher
后的输出:
>java -jar target\downstream-data-factory-0.0.1-SNAPSHOT.jar -Dloader.main=com.xxx.globalpayments.feeds.downstream.DownstreamDataFactoryApplication org.springframework.boot.loader.PropertiesLauncher
2023-07-31 16:14:03.287 [main] INFO c.p.g.f.d.DownstreamDataFactoryApplication - Started DownstreamDataFactoryApplication in 2.981 seconds (JVM running for 4.425)
2023-07-31 16:14:03.291 [main] DEBUG o.s.b.a.ApplicationAvailabilityBean - Application availability state LivenessState changed to CORRECT
2023-07-31 16:14:03.292 [main] DEBUG c.p.g.f.d.DownstreamDataFactoryApplication - Starting execution
2023-07-31 16:14:03.293 [main] INFO c.p.g.f.d.DownstreamDataFactoryApplication - args[0]: -Dloader.main=com.pru.globalpayments.feeds.downstream.DownstreamDataFactoryApplication
2023-07-31 16:14:03.293 [main] INFO c.p.g.f.d.DownstreamDataFactoryApplication - args[1]: org.springframework.boot.loader.PropertiesLauncher
2023-07-31 16:14:03.294 [main] INFO c.p.g.f.d.DownstreamDataFactoryApplication - Obtained data: null
2023-07-31 16:14:03.295 [main] INFO c.p.g.f.d.DownstreamDataFactoryApplication - Processed data:
2023-07-31 16:14:03.296 [main] INFO c.p.g.f.d.DownstreamDataFactoryApplication - Marshalled data to a file:
2023-07-31 16:14:03.297 [main] INFO c.p.g.f.d.DownstreamDataFactoryApplication - Marshalled data to a remote location:
2023-07-31 16:14:03.300 [main] DEBUG o.s.b.a.ApplicationAvailabilityBean - Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
2023-07-31 16:14:03.306 [SpringApplicationShutdownHook] DEBUG o.s.c.a.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6c130c45, started on Mon Jul 31 16:14:01 EDT 2023
2023-07-31 16:14:03.310 [SpringApplicationShutdownHook] DEBUG o.s.c.s.DefaultLifecycleProcessor - Stopping beans in phase -2147483647
2023-07-31 16:14:03.311 [SpringApplicationShutdownHook] DEBUG o.s.c.s.DefaultLifecycleProcessor - Bean 'springBootLoggingLifecycle' completed its stop procedure
型
而在没有提供的情况下:
>java -jar target\downstream-data-factory-0.0.1-SNAPSHOT.jar -Dloader.main=com.xxx.globalpayments.feeds.downstream.DownstreamDataFactoryApplication
2023-07-31 16:18:39.720 [main] INFO c.p.g.f.d.DownstreamDataFactoryApplication - Started DownstreamDataFactoryApplication in 2.931 seconds (JVM running for 4.313)
2023-07-31 16:18:39.725 [main] DEBUG o.s.b.a.ApplicationAvailabilityBean - Application availability state LivenessState changed to CORRECT
2023-07-31 16:18:39.726 [main] DEBUG c.p.g.f.d.DownstreamDataFactoryApplication - Starting execution
2023-07-31 16:18:39.726 [main] INFO c.p.g.f.d.DownstreamDataFactoryApplication - args[0]: -Dloader.main=com.pru.globalpayments.feeds.downstream.DownstreamDataFactoryApplication
2023-07-31 16:18:39.728 [main] INFO c.p.g.f.d.DownstreamDataFactoryApplication - Obtained data: null
2023-07-31 16:18:39.728 [main] INFO c.p.g.f.d.DownstreamDataFactoryApplication - Processed data:
2023-07-31 16:18:39.729 [main] INFO c.p.g.f.d.DownstreamDataFactoryApplication - Marshalled data to a file:
2023-07-31 16:18:39.729 [main] INFO c.p.g.f.d.DownstreamDataFactoryApplication - Marshalled data to a remote location:
2023-07-31 16:18:39.731 [main] DEBUG o.s.b.a.ApplicationAvailabilityBean - Application availability state ReadinessState changed to ACCEPTING_TRAFFIC
2023-07-31 16:18:39.734 [SpringApplicationShutdownHook] DEBUG o.s.c.a.AnnotationConfigApplicationContext - Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6c130c45, started on Mon Jul 31 16:18:37 EDT 2023
2023-07-31 16:18:39.739 [SpringApplicationShutdownHook] DEBUG o.s.c.s.DefaultLifecycleProcessor - Stopping beans in phase -2147483647
2023-07-31 16:18:39.740 [SpringApplicationShutdownHook] DEBUG o.s.c.s.DefaultLifecycleProcessor - Bean 'springBootLoggingLifecycle' completed its stop procedure
型
我的假设是,org.springframework.boot.loader.PropertiesLauncher
本身携带了某些密钥,通过-Dloader.main=AppwithMainMethod
进行的无主类调用将依赖于这些密钥,否则就会失败,但情况似乎并非如此。
此PropertiesLauncher
参数的正确用例以及一些示例和文档是什么?它是可选的吗?是否需要?是否可以安全地跳过提供它?什么时候?
先谢谢你。
1条答案
按热度按时间mwg9r5ms1#
在命令行中:
字符串
字符串
-Dloader.main=com.xxx.globalpayments.feeds.downstream.DownstreamDataFactoryApplication
和org.springframework.boot.loader.PropertiesLauncher
作为命令行参数传递给main
。它们不会被java
或Sping Boot 解释。正确的命令行是:
型
假设应用程序类是
com.example.demo.DemoApplication3
我本以为在
spring-boot-maven-plugin
配置中将mainClass
设置为org.springframework.boot.loader.PropertiesLauncher
会允许它与-jar
一起工作,但这给了我一个堆栈溢出错误。这个jar是用一个简单的配置构建的:
型
您可以将
mainClass
设置为任何您喜欢的值--作为默认值使用的应用程序之一,或者在没有设置loader.main
并且jar使用-jar
运行时提供使用消息的类。