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方法部署app时,一切都正常:
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文件。我在春天的时候跟随了把罐子换成战争指南。并遇到了io站点的问题。它似乎没有加载应用程序。属性文件。以下是重要的代码片段:
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 /资源/ application.properties
app.username=foo
for completeness, here is the 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中删除war,然后去掉日志并查看以下内容:
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,然后看到了应用程序。属性文件。所以我认为它应该在类路径中。我的问题是,为什么tomcat没有加载它?我已经试遍了所有的遍,没有人有这个问题,所以我不确定它是不是我配置错误的东西,或者是什么。
Thanks in advance.
提前谢谢。
4 个解决方案
#1
21
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- annots-in.html
try:
试一试:
@PropertySources(value = {@PropertySource("classpath:application.properties")})
then boom sauce for the win.
然后用爆酱赢得胜利。
#2
11
The problem is that you attempt to use a @Value
annotation inside your @Configuration
class. From the JavaDoc of the @PropertySource:
问题是您试图在您的@Configuration类中使用@Value注释。@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.
为了解决${…
定义或@Value注释中的}占位符使用来自PropertySource的属性,必须注册一个PropertySourcesPlaceholderConfigurer。这在使用XML中的 时自动发生,但是在使用@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类(它不包含任何配置)转移到应用程序类。由于应用程序类不包含任何@Value注释,因此它应该在不添加静态PropertySourcesPlaceholderConfigurer bean的情况下工作。
#3
0
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
我来这里寻找同样的问题。应用程序。当spring引导应用程序在tomcat中作为war运行时,属性不加载,但是在使用嵌入式tomcat运行时它运行良好。问题出现在文件名中。我使用的应用程序。属性,而不是应用程序。属性。当从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调试o.s.b.c.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./config/应用程序。未找到xml的资源2015-09-10 14:42 13982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./config/应用程序。yml资源未找到2015-09-10 14:42:13,982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./config/应用程序。属性'资源未找到2015-09-10 14:42:13982调试o.s.b.c.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./config/应用程序。未找到yaml的资源2015-09-10 14:42 13982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./应用程序。未找到xml的资源2015-09-10 14:42 13982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./应用程序。yml资源未找到2015-09-10 14:42:13,982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./应用程序。属性的资源未找到2015-09-10 14:42 13982 DEBUG o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./应用程序。未找到yaml的资源2015-09-10 14:42 13982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过了配置文件的类路径:/config/application。未找到的xml资源2015-09-10 14:42:13982调试o.s.b.c.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过了配置文件的类路径:/config/application。yml资源未找到2015-09-10 14:42:13,982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过了配置文件的类路径:/config/application。属性的资源未找到2015-09-10 14:42 13982 DEBUG o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过了配置文件的类路径:/config/application。未找到yaml的资源2015-09-10 14:42 13982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的类路径:/应用程序。未找到xml的资源2015-09-10 14:42 13982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[标准引擎[Catalina]])跳过配置文件的类路径:/应用程序。yml资源未找到2015-09-10 14:42:13,982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的类路径:/应用程序。属性的资源未找到2015-09-10 14:42 13982 DEBUG o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的类路径:/应用程序。yaml的资源没有找到
#4
0
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:
我认为对于那些更改默认命名的应用程序。[属性,yaml等]“到,例如,”服务。[属性、yaml等]"可以将其添加到构建中。gradle任务为:
bootRun {
systemProperties = [
'spring.config.name':'service'
]
}
#1
21
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- annots-in.html
try:
试一试:
@PropertySources(value = {@PropertySource("classpath:application.properties")})
then boom sauce for the win.
然后用爆酱赢得胜利。
#2
11
The problem is that you attempt to use a @Value
annotation inside your @Configuration
class. From the JavaDoc of the @PropertySource:
问题是您试图在您的@Configuration类中使用@Value注释。@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.
为了解决${…
定义或@Value注释中的}占位符使用来自PropertySource的属性,必须注册一个PropertySourcesPlaceholderConfigurer。这在使用XML中的 时自动发生,但是在使用@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类(它不包含任何配置)转移到应用程序类。由于应用程序类不包含任何@Value注释,因此它应该在不添加静态PropertySourcesPlaceholderConfigurer bean的情况下工作。
#3
0
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
我来这里寻找同样的问题。应用程序。当spring引导应用程序在tomcat中作为war运行时,属性不加载,但是在使用嵌入式tomcat运行时它运行良好。问题出现在文件名中。我使用的应用程序。属性,而不是应用程序。属性。当从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调试o.s.b.c.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./config/应用程序。未找到xml的资源2015-09-10 14:42 13982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./config/应用程序。yml资源未找到2015-09-10 14:42:13,982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./config/应用程序。属性'资源未找到2015-09-10 14:42:13982调试o.s.b.c.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./config/应用程序。未找到yaml的资源2015-09-10 14:42 13982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./应用程序。未找到xml的资源2015-09-10 14:42 13982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./应用程序。yml资源未找到2015-09-10 14:42:13,982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./应用程序。属性的资源未找到2015-09-10 14:42 13982 DEBUG o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的文件:./应用程序。未找到yaml的资源2015-09-10 14:42 13982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过了配置文件的类路径:/config/application。未找到的xml资源2015-09-10 14:42:13982调试o.s.b.c.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过了配置文件的类路径:/config/application。yml资源未找到2015-09-10 14:42:13,982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过了配置文件的类路径:/config/application。属性的资源未找到2015-09-10 14:42 13982 DEBUG o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过了配置文件的类路径:/config/application。未找到yaml的资源2015-09-10 14:42 13982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的类路径:/应用程序。未找到xml的资源2015-09-10 14:42 13982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[标准引擎[Catalina]])跳过配置文件的类路径:/应用程序。yml资源未找到2015-09-10 14:42:13,982调试o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的类路径:/应用程序。属性的资源未找到2015-09-10 14:42 13982 DEBUG o.s.b.c。ConfigFileApplicationListener [ContainerBackgroundProcessor[StandardEngine[Catalina]]]跳过配置文件的类路径:/应用程序。yaml的资源没有找到
#4
0
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:
我认为对于那些更改默认命名的应用程序。[属性,yaml等]“到,例如,”服务。[属性、yaml等]"可以将其添加到构建中。gradle任务为:
bootRun {
systemProperties = [
'spring.config.name':'service'
]
}