Tomcat不读取Spring-Boot应用程序属性 [英] Tomcat Not reading Spring-Boot Application Properties

查看:557
本文介绍了Tomcat不读取Spring-Boot应用程序属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 :

java -jar build/libs/client.jar

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

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:

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

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

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'
}

我构建应用程序:

gradle clean build

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

Drop the war in tomcat, and then tail out the logs and see the following:

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呢?我已经尝试过全面搜索,似乎没有其他人遇到这个问题所以我不确定它是否只是我错误配置的东西,或者是什么。

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.

提前致谢。

推荐答案

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

follow this guys advice: http://blog.codeleak.pl/2013/11/how-to-propertysource-annotations-in.html

尝试:

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

then获胜的热潮酱。

then boom sauce for the win.

这篇关于Tomcat不读取Spring-Boot应用程序属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆