Java Tomcat 不读取 Spring-Boot 应用程序属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20667653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tomcat Not reading Spring-Boot Application Properties
提问by loganasherjones
I'm fairly new to spring/java and have been checking out spring-boot for a project I have at work. I've been following guides and finally have a (semi) working web app MVC + JPA for data access. Everything works when I deploy the app via the Jar method :
我对 spring/java 相当陌生,并且一直在检查我正在工作的项目的 spring-boot。我一直在遵循指南,最后有一个(半)工作的 Web 应用程序 MVC + JPA 用于数据访问。当我通过 Jar 方法部署应用程序时,一切正常:
java -jar build/libs/client.jar
However, our application is eventually going to be deployed to Tomcat (v7.0.40) so I need to create a war file from the project. I've followed the converting jars to war's guide on the spring.io site and have run into a problem. It appears that it is not loading up the application.properties file. Here are the important code snippets:
但是,我们的应用程序最终将部署到 Tomcat (v7.0.40),因此我需要从该项目创建一个 war 文件。我在 spring.io 站点上遵循了将 jars 转换为 war的指南,但遇到了问题。它似乎没有加载 application.properties 文件。以下是重要的代码片段:
src/main/java/hello/GreetingController:
src/main/java/hello/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/hello/Application.java
src/main/java/hello/Application.java
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
src/main/java/hello/HelloWebXml.java
src/main/java/hello/HelloWebXml.java
public class HelloWebXml extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
}
src/main/resources/application.properties
src/main/resources/application.properties
app.username=foo
for completeness, here is the build.gradle:
为了完整起见,这里是 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'
}
I build the application:
我构建应用程序:
gradle clean build
Drop the war in tomcat, and then tail out the logs and see the following:
在 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}"
...
...
...
As I said, it works when I run it via a jar, but does not work when I deploy it to Tomcat. I also looked inside $TOMCAT_HOME/webapps/client/WEB-INF/classes
and I see the application.properties
file. So I think that it should be on the classpath. My question is, why isn't tomcat loading it? I've tried searching all over and no one else seems to be having this problem so I'm not sure if its just something I have incorrectly configured, or what.
正如我所说,当我通过 jar 运行它时它可以工作,但是当我将它部署到 Tomcat 时它不起作用。我也看了看里面$TOMCAT_HOME/webapps/client/WEB-INF/classes
,我看到了application.properties
文件。所以我认为它应该在类路径上。我的问题是,为什么 tomcat 不加载它?我试过到处搜索,似乎没有其他人遇到这个问题,所以我不确定它是否只是我配置不正确的东西,还是什么。
Thanks in advance.
提前致谢。
采纳答案by codecraig
follow this guys advice: http://blog.codeleak.pl/2013/11/how-to-propertysource-annotations-in.html
遵循这些人的建议:http: //blog.codeleak.pl/2013/11/how-to-propertysource-annotations-in.html
try:
尝试:
@PropertySources(value = {@PropertySource("classpath:application.properties")})
then boom sauce for the win.
然后繁荣酱为胜利。
回答by matsev
The problem is that you attempt to use a @Value
annotation inside your @Configuration
class. From the JavaDoc of the @PropertySource:
问题是您试图@Value
在@Configuration
类中使用注释。来自@PropertySource的 JavaDoc :
In order to resolve ${...} placeholders in <bean> definitions or @Value annotations using properties from a PropertySource, one must register a PropertySourcesPlaceholderConfigurer. This happens automatically when using <context:property-placeholder> in XML, but must be explicitly registered using a static @Bean method when using @Configuration classes.
为了使用来自 PropertySource 的属性解析 <bean> 定义或 @Value 注释中的 ${...} 占位符,必须注册一个 PropertySourcesPlaceholderConfigurer。这在 XML 中使用 <context:property-placeholder> 时会自动发生,但在使用 @Configuration 类时必须使用静态 @Bean 方法显式注册。
e.g. add the following lines to the @Configuration
class:
例如,将以下行添加到@Configuration
类中:
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
However, in your example, a more suitable approach is to move the @Configuration
annotation from the GreetingController
class (it does not contain any configuration) to the Application
class. Since the Application
class does not contain any @Value
annotation it should work without the suggested addition of the static PropertySourcesPlaceholderConfigurer
bean.
但是,在您的示例中,更合适的方法是将@Configuration
注释从GreetingController
类(它不包含任何配置)移动到Application
类。由于Application
该类不包含任何@Value
注释,因此它应该可以在不建议添加静态PropertySourcesPlaceholderConfigurer
bean 的情况下工作。
回答by Jiss Janardhanan
I came here searching for the same issue. application.properties not loading when spring boot application run as a war inside tomcat but it was working fine when running with embedded tomcat. the issue turned out to be in the file name . I had used Application.properties instead of application.properties . when running from tomcat it looks like it is case sensitive . Putting it here so that if someone commits the same stupid mistake as i did
我来到这里寻找同样的问题。application.properties 在 spring boot 应用程序作为 tomcat 内部的战争运行时未加载,但在使用嵌入式 tomcat 运行时运行良好。原来问题出在文件名上。我使用 Application.properties 而不是 application.properties 。从 tomcat 运行时,它看起来是区分大小写的。把它放在这里,以便如果有人犯了和我一样的愚蠢错误
2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'file:./config/application.xml' resource not found 2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'file:./config/application.yml' resource not found 2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'file:./config/application.properties' resource not found 2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'file:./config/application.yaml' resource not found 2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'file:./application.xml' resource not found 2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'file:./application.yml' resource not found 2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'file:./application.properties' resource not found 2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'file:./application.yaml' resource not found 2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'classpath:/config/application.xml' resource not found 2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'classpath:/config/application.yml' resource not found 2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'classpath:/config/application.properties' resource not found 2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'classpath:/config/application.yaml' resource not found 2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'classpath:/application.xml' resource not found 2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'classpath:/application.yml' resource not found 2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'classpath:/application.properties' resource not found 2015-09-10 14:42:13,982 DEBUG o.s.b.c.c.ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] Skipped config file 'classpath:/application.yaml' resource not found
2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件“file:./config/application.xml”资源未找到 2015-09-10 14:42:13,982 ccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件 'file:./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 osbccConfigFileApplicationListener [ContainerBackgroundProcessorCa[Standardine][StandardEngine]找不到文件 'file:./application.yml' 资源 2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件 'file:./application.properties' 资源未找到 2015 -09-10 14:42:13,982 调试 osbccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件 'file:./application.yaml' 资源未找到 2015-09-10 14:42:13,982 调试 osbConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件 'classpath:/config/application.xml' 资源未找到 2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina config]]] 'classpath:/config/application.yml' 资源未找到 2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件 'classpath:/config/application.properties' 资源未找到2015-09-10 14:42:13,982 调试 osbccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件“classpath:/config/application.yaml”资源未找到 2015-09-10 14:42:13,988ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件 'classpath:/application.xml' 资源未找到 2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina config]]]'Skipped 类路径:/application.yml' 资源未找到 2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件 'classpath:/application.properties' 资源未找到 2015-09-10 10 :42:13,982 调试 osbccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件“classpath:/application.yaml”资源未找到找不到资源找不到资源xml' 资源未找到 2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件 'classpath:/application.yml' 资源未找到 2015-09-10 14,9821:调试 osbccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件 'classpath:/application.properties' 资源未找到 2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor[Standard]file]'Catalina]'Catalina]未找到类路径:/application.yaml' 资源xml' 资源未找到 2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件 'classpath:/application.yml' 资源未找到 2015-09-10 14,9821:调试 osbccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件 'classpath:/application.properties' 资源未找到 2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor[Standard]file]'Catalina]'Catalina]未找到类路径:/application.yaml' 资源找不到资源 2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件“classpath:/application.properties”资源未找到 2015-09-10 14:482: [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件 'classpath:/application.yaml' 资源未找到找不到资源 2015-09-10 14:42:13,982 DEBUG osbccConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件“classpath:/application.properties”资源未找到 2015-09-10 14:482: [ContainerBackgroundProcessor[StandardEngine[Catalina]]] 跳过配置文件 'classpath:/application.yaml' 资源未找到找不到资源找不到资源
回答by Jake OS
I think for those who change default naming "application.[properties,yaml,etc]" to, for example, "service.[properties,yaml,etc]", can add this into the build.gradle task as:
我认为对于那些将默认命名“application.[properties,yaml,etc]”更改为例如“service.[properties,yaml,etc]”的人,可以将其添加到 build.gradle 任务中:
bootRun {
systemProperties = [
'spring.config.name':'service'
]
}