小编典典

Tomcat不读取Spring-Boot应用程序属性

spring

我对spring / java还是相当陌生,并且一直在为我正在工作的项目检查spring-boot。我一直在遵循指南,最后有一个(半)运行中的Web应用程序MVC + JPA用于数据访问。当我通过Jar方法部署应用程序时,一切正常:

java -jar build/libs/client.jar

但是,我们的应用程序最终将部署到Tomcat(v7.0.40),因此我需要从项目中创建一个war文件。我已经在spring.io网站上遵循了将jars转换为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中,然后删除日志并查看以下内容:

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不加载它?我已经尝试了全部搜索,但似乎没有其他人遇到此问题,因此我不确定是否只是我配置错误的内容或什么。

提前致谢。


阅读 327

收藏
2020-04-20

共1个答案

小编典典

尝试:

@PropertySources(value = {@PropertySource("classpath:application.properties")})
2020-04-20