运行 外部文件夹中的类路径 war 文件命令 Spring java

run classpath in external folder war file command Spring java

我有一个 war 包,想包含一个外部 class 路径:

在我的控制器中:

我有以下代码:

WebApplicationclass:

@SpringBootApplication
@PropertySource( "classpath:config.properties" )
public class WebApplication{

   public static void main(String[] args){
        SpringApplication.run(WebApplication.class, args);
   }
}

控制器Class:

@Controller
@PropertySource( "classpath:config.properties" )
public class ApplicationController{

     //doing some stuff here

}

我的文件夹:

|-project

|-src
   |-main
     |-java
       |-com.mypage.temp
         |-WebApplication.java (main class)
       |-com.mypage.temp.controller
         |-ApplicationController (Controller)
     |-resources
       |- [some stuff & not putting my properties file here to include externally]
     |-deploy
       |-config.properties

但是当我构建

project.war

我放了一个远程服务器(unix):

|project

  |-bin
    |-project.war
  |-config
    |-config.properties

Something/something/something/bin/project.war Something/something/something/config/config.properties

运行 命令:

java -jar -Dclasspath=/something/something/something/config/config.properties project.war

错误:

 Failed to parse configuration class [com.mypage.temp.WebApplication];
 nested exception is java.io.FileNotFoundException: class path resource
 [config.properties] cannot be opened because it does not exist
 2016-12-19 18:13:16.763 ERROR 21662 --- [           main]
 o.s.boot.SpringApplication               : Application startup failed

 org.springframework.beans.factory.BeanDefinitionStoreException: Failed to
 parse configuration class [com.mypage.temp.WebApplication]; nested
 exception is java.io.FileNotFoundException: class path resource
 [config.properties] cannot be opened because it does not exist

official documentation开始,SpringBoot依靠spring.config.locationSpringBoot属性指定环境属性文件的位置。
您可以在命令行中提供它。

You can also refer to an explicit location using the spring.config.location environment property (comma-separated list of directory locations, or file paths).

$ java -jar myproject.jar --spring.config.name=myproject or

$ java -jar myproject.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties

所以,试试看:

java -jar project.war --spring.config.location=classpath:/something/something/something/config/config.properties