Tomcat不读取Spring-Boot应用程序属性

我是spring / java的新手,并且已经检查了spring-boot以查找我在工作的项目。 我一直在关注指南,最后有一个(半)工作的网络应用MVC + JPA用于数据访问。 当我通过Jar方法部署应用程序时,一切正常:

java -jar build/libs/client.jar 

但是,我们的应用程序最终将部署到Tomcat(v7.0.40),因此我需要从项目中创建war文件。 我在spring.io网站上跟随转换jar到战争指南并遇到了问题。 它似乎没有加载application.properties文件。 以下是重要的代码片段:

的src / main / JAVA /你好/ GreetingController:

 @Controller @Configuration public class GreetingController { @Value("${app.username}") private String username; @RequestMapping("/greeting") public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) { model.addAttribute("name", name); model.addAttribute("username", username); return "greeting"; } } 

的src / main / JAVA /你好/ Application.java

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

的src / main / JAVA /你好/ HelloWebXml.java

 public class HelloWebXml extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } } 

SRC /主/资源/ application.properties

 app.username=foo 

为了完整性,这里是build.gradle:

 buildscript { repositories { maven { url "http://repo.spring.io/libs-snapshot" } mavenLocal() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:0.5.0.M6") } } apply plugin: 'java' apply plugin: 'eclipse-wtp' apply plugin: 'idea' apply plugin: 'spring-boot' apply plugin: 'war' war { baseName = 'client' version = '0.1.0' } repositories { mavenCentral() maven { url "http://repo.spring.io/libs-snapshot" } } dependencies { compile("org.springframework.boot:spring-boot-starter-web:0.5.0.M6") compile("org.thymeleaf:thymeleaf-spring3:2.0.16") testCompile("junit:junit:4.11") } task wrapper(type: Wrapper) { gradleVersion = '1.8' } 

我构建了应用程序:

 gradle clean build 

在tomcat中放弃战争,然后拖尾日志并看到以下内容:

 SEVERE: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina] .StandardHost[localhost].StandardContext[/client]] ... ... ... Caused by: org.springframework.beans.factory.BeanCreationException: Error creating the bean with name 'greetingController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'app.username' in string value "${app.username}" ... ... ... 

正如我所说,当我通过jar运行它时,它可以工作,但是当我将它部署到Tomcat时它不起作用。 我还查看了$TOMCAT_HOME/webapps/client/WEB-INF/classes ,我看到了application.properties文件。 所以我认为它应该在classpath上。 我的问题是,为什么不加载tomcat呢? 我已经尝试过全面搜索,似乎没有其他人遇到这个问题所以我不确定它是否只是我错误配置的东西,或者是什么。

提前致谢。

按照这个家伙的建议: http : //blog.codeleak.pl/2013/11/how-to-propertysource-annotations-in.html

尝试:

 @PropertySources(value = {@PropertySource("classpath:application.properties")}) 

然后繁荣酱油为胜利。

问题是您尝试在@Configuration类中使用@Value注释。 来自@PropertySource的JavaDoc:

为了使用PropertySource中的属性解析定义或@Value注释中的$ {…}占位符,必须注册PropertySourcesPlaceholderConfigurer。 在XML中使用时会自动发生这种情况,但在使用@Configuration类时必须使用静态@Bean方法显式注册。

例如, @Configuration添加到@Configuration类:

 @Bean public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } 

但是,在您的示例中,更合适的方法是将@Configuration批注从GreetingController类(它不包含任何配置)移动到Application类。 由于Application类不包含任何@Value注释,因此它应该在没有建议添加静态PropertySourcesPlaceholderConfigurer bean的情况下工作。

我来这里寻找同样的问题。 当spring boot应用程序作为tomcat内部的war运行时,application.properties没有加载,但是当使用嵌入式tomcat运行时,它运行正常。 问题原来是在文件名中。 我曾使用Application.properties而不是application.properties。 从tomcat运行时看起来它区分大小写。 把它放在这里,以便如果有人像我一样犯下同样的愚蠢错误

2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor [StandardEngine [Catalina]]]跳过的配置文件’file:./ config / application.xml’资源未找到2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor [StandardEngine [Catalina]]]跳过配置文件’文件:./ config / application.yml’资源未找到2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor [StandardEngine [Catalina]]]跳过的配置文件’file:./ config / application.properties’资源未找到2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor [StandardEngine [Catalina]]]跳过配置文件’file:./ config / application.yaml’资源未找到2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor [StandardEngine [Catalina]]]跳过的配置文件’file:./ application.xml’资源未找到2015-09-10 14:42:13,982 DEBUG osbc cConfigFileApplicationListener [ContainerBackgroundProcessor [StandardEngine [Catalina]]]跳过配置文件’file:./ application.yml’找不到资源2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor [StandardEngine [Catalina]]]跳过配置文件’ file:./ application.properties’infault not found 2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor [StandardEngine [Catalina]]]跳过的配置文件’file:./ application.yaml’资源未找到2015-09 -10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor [StandardEngine [Catalina]]]找不到跳过的配置文件’classpath:/config/application.xml’资源2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor [StandardEngine] [Catalina]]]跳过配置文件’classpath:/config/application.yml’找不到资源2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [Containe rBackgroundProcessor [StandardEngine [Catalina]]]跳过配置文件’classpath:/config/application.properties’找不到资源2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor [StandardEngine [Catalina]]]跳过配置文件’classpath :找不到/config/application.yaml’资源2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor [StandardEngine [Catalina]]]跳过配置文件’classpath:/application.xml’找不到资源2015-09- 10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor [StandardEngine [Catalina]]]跳过的配置文件’classpath:/application.yml’找不到资源2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor [StandardEngine [Catalina] ]] skipped配置文件’classpath:/application.properties’资源未找到2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor [ StandardEngine [Catalina]]]找不到跳过的配置文件’classpath:/application.yaml’资源

我认为对于那些将默认命名为“application。[properties,yaml,etc]”的人命名为“service。[properties,yaml,etc]”,可以将其添加到build.gradle任务中:

 bootRun { systemProperties = [ 'spring.config.name':'service' ] }