当我从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 /目录中,它会抛出如下所示的excpeption,这意味着它正在读取该属性文件:
例外:
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)
答案 0 :(得分:2)
据我所知,您希望使用application.properties中的变量。
我遵循的方式如下;
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
之后在pom.xml中插入以下标记
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
然后用“mvn install”或“mvn clean install”构建你的-jar。
我希望它有效。