Java Spring Boot application.properties值未填充且未读取
当我从eclipse配置Java Spring Boot application.properties值未填充且未读取,java,eclipse,spring,command-line,spring-boot,Java,Eclipse,Spring,Command Line,Spring Boot,当我从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 我的帖子和问题看起来很相似,我引用了
-Dext.prop.dir
时,它运行良好,一切正常。它在8085端口和指定的上下文“DBService”
上运行
但当我从命令行运行使用maven生成的jar时,它不会读取application.properties。默认情况下,tomcat从8080开始,我无法识别上下文。其他一切都很好
在eclipse中,我提供了:VM参数作为:-Dext.prop.dir=E:/res/-Dlog.name=D:/res/logback.xml
我的帖子和问题看起来很相似,我引用了这个,只有我引用了application.properte为spring boot应用程序配置自定义上下文和端口。我也提到过这一点,但当我从命令行运行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;
}
}
我的应用程序属性:
#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/目录中,它会抛出如下的expeption,这意味着它正在读取该属性文件:
例外情况:
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)
2016-08-07 14:40:09.435信息18168-[main].b.l.ClasspathLoggingApplicationListener:应用程序未能以类路径启动:[文件:/D:com.drd.db.services-1.0.0.M1 jar with dependencidentials.jar]
2016-08-07 14:40:09.445错误18168---[main]o.s.boot.SpringApplication:应用程序启动失败
org.springframework.beans.factory.BeanDefinitionStoreException:未能加载bean类:com.drd.application.configuration.ApplicationRunner;嵌套异常
is java.io.FileNotFoundException:D:\htl properties\application.properties(系统找不到指定的文件)
位于org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:159)
...
位于org.springframework.boot.SpringApplication.run(SpringApplication.java:944)
位于org.springframework.boot.SpringApplication.run(SpringApplication.java:933)
位于com.drd.application.configuration.ApplicationRunner.main(ApplicationRunner.java:45)
原因:java.io.FileNotFoundException:D:\res\application.properties(系统找不到指定的文件)
在java.io.FileInputStream.open(本机方法)
位于java.io.FileInputStream。(FileInputStream.java:146)
位于java.io.FileInputStream。(FileInputStream.java:101)
位于sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
位于sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
据我所知,您希望使用application.properties中的变量
我遵循的方式如下
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
</dependencies>
org.springframework.boot
spring引导配置处理器
然后在pom.xml中插入以下标记
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
src/main/resources
真的
**/*.物业
然后用“mvn安装”或“mvn干净安装”构建您的-jar
我希望它能工作。看起来像类路径问题。我没有编写任何特定的类路径,因为我运行的是从maven生成的:com.drd.db.services-1.0.0.M1-jar-with-dependencies.jar。它包含其中的所有jar和类。因此,我不需要提供任何cp。此外,在创建jar之前,我将pom中的application.properties排除在外。如下所示:*/application.properties**/logback.xml看起来非常清楚:
java.io.FileNotFoundException:D:\htl properties\application.properties(系统找不到指定的文件)
希望它是一个打字错误。。。但是在eclipsevm参数中,目录是E://
,而在java命令中,目录是D://
。那只是打字错误吗?