【发布时间】:2016-12-13 05:13:41
【问题描述】:
当我从 Eclipse 配置 -Dext.prop.dir 时,它运行良好,一切正常。它在 8085 端口和指定上下文 "DBService" 上运行。
但是当我从命令行运行使用 maven 生成的 jar 时,它不会读取 application.properties,默认情况下,tomcat 从 8080 开始,我无法识别上下文。其他一切正常。
在 Eclipse 中我提供了:VM 参数为:-Dext.prop.dir=E:/res/ -Dlog.name=D:/res/logback.xml
我的帖子和问题看起来很相似,我参考了这个post already,只有我参考了 application.properteis 来为 Spring Boot 应用程序配置自定义上下文和端口。我也参考了这个link,但是当我从命令行运行 jar 时,我无法弄清楚问题出在哪里。
我提出的相同 vm 参数如下:
java -Dext.prop.dir=D:/res/ -jar com.drd.db.services-1.0.0.M1-jar-with-dependencies.jar
我将 application.properteis 保留为 D:/res/application.properteis。
下面是我的应用程序启动类和 application.properties 。
package com.drd.application.configuration;
import static com.drd.db.service.util.ServicesConstants.EXT_PROP_DIR;
import static com.drd.db.service.util.ServicesConstants.LOG_BACK_XML_SYSTEM_PROPERTY;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;
import com.drd.db.service.util.ServicesConstants;
@ComponentScan(basePackages = { "com.drd.db" })
@Configuration
@PropertySource({"file:///${ext.prop.dir}application.properties"/*, "classpath:application.properties"*/})
@EnableAutoConfiguration
public class ApplicationRunner {
private static final Logger LOG = LoggerFactory.getLogger(ApplicationRunner.class);
public static void main(String[] args) throws Exception {
String extPropDir=System.getProperty(EXT_PROP_DIR);
if(StringUtils.isEmpty(extPropDir)){
LOG.warn("Could not resolve PropertyvSource placeholder 'ext.prop.dir' in string value file:///${ext.prop.dire}application.properties");
LOG.warn("Could not finnd {} System property Please specify valid path for it ",ServicesConstants.EXT_PROP_DIR);
return;
}
String logbackXMLPath=System.getProperty(LOG_BACK_XML_SYSTEM_PROPERTY);
if(StringUtils.isEmpty(logbackXMLPath)){
LOG.info("Could not finnd {} System property for loading logback.xml Please specify valid path for it proper loaggin ", LOG_BACK_XML_SYSTEM_PROPERTY);
}
LOG.info("ApplicationRunner Spring boot application start from this class");
SpringApplication.run(ApplicationRunner.class, args);
}
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
return factory;
}
}
我的 applicaion.properties:
#All application Label properties configure if any.
server.context-path=/DBService
server.port=8085
hibernate.prop.dir=D:/main/resources/
当我从命令行运行时,任何人都可以知道或分享为什么 spring 没有配置或读取这些属性。
还有一个重要的事情是,如果我没有将 application.properties 放在 D:/res/ 目录中,它会抛出如下异常,这意味着它正在读取该属性文件:
例外:
2016-08-07 14:40:09.435 INFO 18168 --- [ main] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/D:com.drd.db.services-1.0.0.M1-jar-with-dependencies.jar]
2016-08-07 14:40:09.445 ERROR 18168 --- [ main] o.s.boot.SpringApplication : Application startup failed
org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.drd.application.configuration.ApplicationRunner; nested exception
is java.io.FileNotFoundException: D:\htl-properties\application.properties (The system cannot find the file specified)
at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:159)
...
at org.springframework.boot.SpringApplication.run(SpringApplication.java:944)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:933)
at com.drd.application.configuration.ApplicationRunner.main(ApplicationRunner.java:45)
Caused by: java.io.FileNotFoundException: D:\res\application.properties (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:146)
at java.io.FileInputStream.<init>(FileInputStream.java:101)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
【问题讨论】:
-
看起来像类路径问题。
-
我没有写任何特定的类路径,因为我正在运行从 maven 生成:com.drd.db.services-1.0.0.M1-jar-with-dependencies.jar。它包含其中的所有罐子和类。所以我不需要提供任何cp。而且,在创建 jar 之前,我在 pom 中排除了 application.properties。如下:
**/application.properties **/logback.xml -
看起来很清晰:
java.io.FileNotFoundException: D:\htl-properties\application.properties (The system cannot find the file specified) -
希望这是一个错字...但是在您的 Eclipse VM 参数中,您的目录为
E:/,但在您的 java 命令中,您的目录为D:/。这只是一个错字吗?
标签: java eclipse spring command-line spring-boot