我认为我误解了Spring启动配置文件的功能。 我的spring启动应用程序中有两个独立的启动类(ApplicationLocal和ApplicationProduction), 这些类注释如下
@SpringBootApplication
@EnableSwagger2
@Import(value={Config.class})
@ComponentScan(basePackages={"com.abc.*"})
@Profile("local")
public class ApplicationLocal extends SpringBootServletInitializer {
//
@SpringBootApplication
@EnableSwagger2
@Import(value={Config.class})
@ComponentScan(basePackages={"com.abc.*"})
@Profile("production")
public class ApplicationProduction extends SpringBootServletInitializer {
//
并且我希望在运行时通过Spring启动来获取相应的启动类,具体取决于-Dspring.profiles.active jvm arguement的值。 但除非我在pom.xml中提到start-class,否则它不适用于我 喜欢这个..
<properties>
<start-class>com.abc.web.service.ApplicationLocal</start-class>
</properties>
如果我没有在pom.xml中提及start-class,那么我会收到以下错误: (我按照以下方式运行应用程序
mvn spring-boot:run -Drun.jvmArguments=" -Dspring.profiles.active=local"
我收到以下错误
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.3.RELEASE:run (default-cli) on project payee-list-ws: Execution default-cli of goal org.springframework.boot:spring-boot-maven-plugin:1.3.3.RELEASE:run failed: Unable to find a single main class from the following candidates [com.abc.web.service.ApplicationProduction, com.abc.web.service.ApplicationLocal] -> [Help 1]
答案 0 :(得分:1)
您可以为每个env创建一个主类和两个配置:
@SpringBootApplication
@EnableSwagger2
@Import(value={Config.class})
@ComponentScan(basePackages={"com.abc.*"})
public class Application extends SpringBootServletInitializer {
//
@Configuration
@Profile("production")
public class ConfigProduction {
//...
}
@Configuration
@Profile("Local")
public class ConfigLocal {
//...
}
当主类将扫描bean时,Spring将根据您的实际配置文件选择正确的配置。