我对Spring/Java相对较新,在工作中正在研究使用Spring Boot进行项目开发。我一直在跟随指南学习,最终成功搭建了一个(半工作)的Web应用程序MVC+JPA用于数据访问。当我通过Jar方法部署应用程序时,一切都正常运行:
java -jar build/libs/client.jar
然而,我们的应用最终将部署到Tomcat(v7.0.40),因此我需要从项目创建war文件。我遵循了spring.io网站上将jar转换为war指南,并遇到了一个问题。似乎它没有加载application.properties文件。以下是关键代码段:
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
@ComponentScan
@EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
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
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中的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}"
...
...
...
就像我说的,当我通过一个jar包运行它时它能够工作,但是当我部署到Tomcat上时就无法工作。我还查看了$TOMCAT_HOME/webapps/client/WEB-INF/classes
目录中的application.properties
文件,因此我认为它应该在类路径上。我的问题是,为什么Tomcat没有加载它?我已经尝试搜索各种资源,但似乎没有其他人遇到这个问题,所以我不确定是我配置有误还是别的什么原因。
提前感谢您的帮助。
application.properties
。尝试在你的@Configuration
类上添加@PropertySource("application.properties")
,看它是否能够找到它。 - Sotirios DelimanolisCannot find annotation method 'value()' in type 'Repeatable': class file for java.lang.annotation.Repeatable not found.
当我将它放入tomcat时,我得到了以下错误:Failed to load bean class: hello.Application; nested exception is org.springframework.core.NestedIOException: Unable to collect imports; nested exception is java.lang.ClassNotFoundException: java.lang.annotation.Repeatable
- loganasherjones@PropertySource("classpath:application.properties")
。 - Sotirios Delimanolis@PropertySource
注解可以与 Spring Boot 应用程序一起使用,但如果它是classpath:application.properties
,则不需要声明它(并且某些框架功能无法使用@PropertySource
进行配置)- 尽管这在此处不相关。 - Dave Syer