【发布时间】:2014-01-07 04:37:18
【问题描述】:
我对 spring/java 还很陌生,并且一直在为我工作的项目检查 spring-boot。我一直在遵循指南,终于有了一个(半)工作的网络应用程序 MVC + JPA 用于数据访问。当我通过 Jar 方法部署应用程序时,一切正常:
java -jar build/libs/client.jar
但是,我们的应用程序最终将部署到 Tomcat (v7.0.40),因此我需要从项目中创建一个 war 文件。我遵循了 spring.io 网站上的converting jars to war 指南并遇到了问题。它似乎没有加载 application.properties 文件。以下是重要的代码sn-ps:
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,然后tail out日志看看如下:
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不加载它?我已经尝试过全面搜索,但似乎没有其他人遇到这个问题,所以我不确定它是否只是我配置错误的东西,还是什么。
提前致谢。
【问题讨论】:
-
Spring boot 默认加载
application.properties。尝试将@PropertySource("application.properties")添加到您的@Configuration类中,看看它是否真的找到它。 -
嘿@SotiriosDelimanolis 感谢您的回复。不幸的是,这也不起作用。当 tomcat 尝试加载战争时,我得到了 FileNotFoundException。如果需要,我可以给你更多细节,请告诉我。
-
对不起,应该是
classpath:application.properties -
再次感谢,我继续添加,但注意到编译器警告:
Cannot 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 -
@SotiriosDelimanolis
@PropertySource注解可以与 Spring Boot 应用程序一起使用,但如果它是classpath:application.properties则不需要声明它(并且有一些框架功能不能配置为@PropertySource) - 不过这里不相关。
标签: java spring tomcat spring-boot