SpringBoot:使用PropertySoucre与外部文件并使用java jar命令时的FileNotFoundException

时间:2018-01-03 17:55:24

标签: java spring-boot spring-profiles spring-properties

服务将从eclipse启动,但是如果我开始使用jar命令,同样的事情并不起作用。它只适用于我在属性源中提供绝对路径。我希望属性源能够使用相对路径。

以下是日志,服务的主要类和服务的文件结构



java -jar review-service.jar  --spring.profiles.active=dev
2018-01-03T23:17:15.883+0530 [background-preinit] INFO  org.hibernate.validator.internal.util.Version - HV000001: Hibernate Validator 6.0.4.Final
2018-01-03T23:17:15.909+0530 [main] INFO  org.springframework.context.annotation.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5c7fa833: startup date [Wed Jan 03 23:17:15 IST 2018]; root of context hierarchy
2018-01-03T23:17:16.349+0530 [main] INFO  org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-01-03T23:17:16.521+0530 [main] INFO  org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.retry.annotation.RetryConfiguration' of type [org.springframework.retry.annotation.RetryConfiguration$$EnhancerBySpringCGLIB$$e6c8a335] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-01-03T23:17:16.533+0530 [main] INFO  org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$d1f0e0d] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::             (v2.0.0.M6)

2018-01-03T23:17:28.078+0530 [main] INFO  org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration - Located property source: CompositePropertySource {name='consul', propertySources=[ConsulPropertySource {name='config/review-service,dev/'}, ConsulPropertySource {name='config/review-service/'}, ConsulPropertySource {name='config/application,dev/'}, ConsulPropertySource {name='config/application/'}]}
2018-01-03T23:17:28.087+0530 [main] INFO  com.ns.services.review.main.ReviewServiceLauncher - The following profiles are active: dev
2018-01-03T23:17:28.111+0530 [main] INFO  org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@48aaecc3: startup date [Wed Jan 03 23:17:28 IST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@5c7fa833
2018-01-03T23:17:28.128+0530 [main] WARN  org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.ns.services.review.main.ReviewServiceLauncher]; nested exception is java.io.FileNotFoundException: config/review-service-dev.properties (No such file or directory)
2018-01-03T23:17:28.129+0530 [main] ERROR org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroy method on bean with name 'org.springframework.boot.autoconfigure.internalCachingMetadataReaderFactory' threw an exception
java.lang.IllegalStateException: ApplicationEventMulticaster not initialized - call 'refresh' before multicasting events via the context: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@48aaecc3: startup date [Wed Jan 03 23:17:28 IST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@5c7fa833
	at org.springframework.context.support.AbstractApplicationContext.getApplicationEventMulticaster(AbstractApplicationContext.java:420) ~[spring-context-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
	at org.springframework.context.support.ApplicationListenerDetector.postProcessBeforeDestruction(ApplicationListenerDetector.java:95) ~[spring-context-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
	at org.springframework.beans.factory.support.DisposableBeanAdapter.destroy(DisposableBeanAdapter.java:240) ~[spring-beans-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroyBean(DefaultSingletonBeanRegistry.java:576) [spring-beans-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingleton(DefaultSingletonBeanRegistry.java:552) [spring-beans-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingleton(DefaultListableBeanFactory.java:953) [spring-beans-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.destroySingletons(DefaultSingletonBeanRegistry.java:521) [spring-beans-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
	at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.destroySingletons(FactoryBeanRegistrySupport.java:227) [spring-beans-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.destroySingletons(DefaultListableBeanFactory.java:960) [spring-beans-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.destroyBeans(AbstractApplicationContext.java:1035) [spring-context-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:562) [spring-context-5.0.1.RELEASE.jar!/:5.0.1.RELEASE]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:122) [spring-boot-2.0.0.M6.jar!/:2.0.0.M6]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:751) [spring-boot-2.0.0.M6.jar!/:2.0.0.M6]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:387) [spring-boot-2.0.0.M6.jar!/:2.0.0.M6]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:327) [spring-boot-2.0.0.M6.jar!/:2.0.0.M6]
	at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:136) [spring-boot-2.0.0.M6.jar!/:2.0.0.M6]
	at com.ns.services.review.main.ReviewServiceLauncher.main(ReviewServiceLauncher.java:35) [classes!/:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_144]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_144]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_144]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_144]
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [review-service.jar:?]
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [review-service.jar:?]
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [review-service.jar:?]
	at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) [review-service.jar:?]




主类



package com.ns.services.review.main;

import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.PropertySource;

import com.ns.commons.application.BaseSpringBootApplication;

import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableDiscoveryClient
//@ComponentScan(basePackages = { "com.ns.services.review", "com.ns.commons", "com.ns.api" })

@ComponentScan(
		basePackages = { "com.ns.services.review", "com.ns.commons",
		"com.ns.api" }
	)
@EnableSwagger2
@PropertySource({"file:config/review-service-${spring.profiles.active}.properties"})
@EnableFeignClients(basePackages = { "com.ns.services.review", "com.ns.api.clients" })
public class ReviewServiceLauncher extends BaseSpringBootApplication {

	public static void main(String... args) {
		new SpringApplicationBuilder(ReviewServiceLauncher.class).web(WebApplicationType.SERVLET).run(args);
	}

	@Bean
	public Docket api() {
		return new Docket(DocumentationType.SWAGGER_2).select()
				.apis(RequestHandlerSelectors.basePackage("com.ns.services.review")).paths(PathSelectors.any()).build();
	}
}




文件结构快照

enter image description here

1 个答案:

答案 0 :(得分:0)

config目录应该是您拥有review-service.jar文件的位置。把它放在那个jar文件旁边就可以了。

它适用于eclipse,因为在eclipse环境中,应用程序的根目录在你的项目根目录中。在独立版本上,应用程序root是jar文件(或者更确切地说是当前工作目录)的位置