@PropertySource没有在使用传统tomcat war部署的spring启动应用程序中获取

时间:2015-06-15 02:38:07

标签: java spring-boot tomcat

我有一个传统的war启动应用程序部署到tomcat 8.尽管我已经定义了@PropertySources注释,但似乎找不到在@PropertySources中指定的目录中给出的属性文件中定义的属性。我修改了一个spring boot web示例项目,可以复制问题。在下面的示例中,调用Web控制器的结果始终是“Hello Default,Spring Boot的问候!”。它永远不会从外部application.properties中获取“name”.property(如果我从类属性文件中完全删除该属性,则部署失败,因为该属性永远不会解析)。我错过了什么,以至于外部属性似乎没有被提升?

我的申请类:

@SpringBootApplication
@Profile("dev")
@PropertySource(value = "file:/usr/local/tomcat/config/")
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(
            SpringApplicationBuilder builder) {
        return builder.profiles("dev").sources(Application.class);
    }

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

网络控制器:

@RestController
public class HelloController {

    @Value("${name}")
    private String name;

    @RequestMapping("/")
    public String index() {
        return "Hello " +name+ ", Greetings from Spring Boot!";
    }
}

的src /主/资源/ application.properties:

debug=true
logging.level.org.springframework.boot.context=DEBUG
logging.level.org.springframework.boot.env=DEBUG
logging.level.org.springframework.web=DEBUG
name=Default

/ usr / local / tomcat / config

中的外部application.properties
name=Default

catalina.out显示属性来源的部分:

2015-06-14 22:02:11.685 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:11.687 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:11.687 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:11.687 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:11.687 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:11.688 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:11.717 DEBUG 5010 --- [ost-startStop-1] o.s.boot.SpringApplication               : Running with Spring Boot v1.2.2.RELEASE, Spring v4.1.5.RELEASE
2015-06-14 22:02:11.718 DEBUG 5010 --- [ost-startStop-1] o.s.boot.SpringApplication               : Loading source class hello.Application,class org.springframework.boot.context.web.ErrorPageFilter
2015-06-14 22:02:11.737 DEBUG 5010 --- [ost-startStop-1] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'file:./config/application.xml' resource not found
[typical scanning for properties files]
2015-06-14 22:02:11.738 DEBUG 5010 --- [ost-startStop-1] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file 'classpath:/application.properties' 
[continue and finish default scanning for properties files]

2015-06-14 22:02:11.738 DEBUG 5010 --- [ost-startStop-1] o.s.b.c.c.ConfigFileApplicationListener  : Skipped config file 'classpath:/application.yaml' for profile devresource not found
2015-06-14 22:02:11.742  INFO 5010 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@31c0c0c3: startup date [Sun Jun 14 22:02:11 EDT 2015]; root of context hierarchy
2015-06-14 22:02:11.745 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Replacing [servletContextInitParams] PropertySource with [servletContextInitParams]
2015-06-14 22:02:11.745 DEBUG 5010 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Bean factory for org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@31c0c0c3: org.springframework.beans.factory.support.DefaultListableBeanFactory@1319bea3: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,application,errorPageFilter]; root of factory hierarchy
2015-06-14 22:02:11.786 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [URL [file:/usr/local/apache-tomcat-8.0.23/config/]] PropertySource with lowest search precedence
2015-06-14 22:02:12.269  INFO 5010 --- [ost-startStop-1] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2015-06-14 22:02:12.500 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [applicationConfig: [classpath:/application.properties]] PropertySource with search precedence immediately lower than [applicationConfigurationProperties]
2015-06-14 22:02:12.500 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Removing [applicationConfigurationProperties] PropertySource
2015-06-14 22:02:12.500 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Removing [defaultProperties] PropertySource
2015-06-14 22:02:12.641 DEBUG 5010 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@3fd3a17b]
2015-06-14 22:02:12.642 DEBUG 5010 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Using ApplicationEventMulticaster [org.springframework.context.event.SimpleApplicationEventMulticaster@2c2545cb]
2015-06-14 22:02:12.644 DEBUG 5010 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2015-06-14 22:02:12.644  INFO 5010 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 902 ms
2015-06-14 22:02:12.943 DEBUG 5010 --- [ost-startStop-1] o.s.b.c.e.ServletContextInitializerBeans : Added existing Servlet initializer bean 'dispatcherServletRegistration'; order=2147483647, resource=class path resource [org/springframework/boot/autoconfigure/web/DispatcherServletAutoConfiguration$DispatcherServletConfiguration.class]
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:12.953 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:12.984  INFO 5010 --- [ost-startStop-1] b.a.w.TomcatWebSocketContainerCustomizer : NonEmbeddedServletContainerFactory detected. Websockets support should be native so this normally is not a problem.
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:12.995 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:13.010 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:13.046 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:13.074 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.074 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.074 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.074 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.074 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:13.075 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]
2015-06-14 22:02:13.100 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletConfigInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.101 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [servletContextInitParams] PropertySource with lowest search precedence
2015-06-14 22:02:13.101 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [jndiProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.101 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemProperties] PropertySource with lowest search precedence
2015-06-14 22:02:13.101 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Adding [systemEnvironment] PropertySource with lowest search precedence
2015-06-14 22:02:13.101 DEBUG 5010 --- [ost-startStop-1] o.s.w.c.s.StandardServletEnvironment     : Initialized StandardServletEnvironment with PropertySources [servletConfigInitParams,servletContextInitParams,jndiProperties,systemProperties,systemEnvironment]

1 个答案:

答案 0 :(得分:4)

使用传统的tomcat war部署时,我建议使用相对于类路径的文件夹路径,而不是绝对路径。

配置Spring Boot应用程序时,您需要在开头定义变量ffmpeg -f gdigrab -framerate 25 -i title=Calculator [output]

spring.config.location

我遇到了同样的问题并在此处找到了解决方案:How to externalize Spring Boot application.properties to tomcat/lib folder