当前位置: 首页 > 知识库问答 >
问题:

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

昝光临
2023-03-14
java -jar build/libs/client.jar
@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";
    }
} 
@ComponentScan
@EnableAutoConfiguration
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
public class HelloWebXml extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

src/main/resources/application.properti

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
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}"
...
...
...

共有1个答案

董谦
2023-03-14

遵循guys的建议:http://blog.codeleak.pl/2013/11/how-to-propertysource-annotations-in.html

尝试:

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

那就来点酱吧。

 类似资料:
  • 问题内容: 我对spring / java相当陌生,并且一直在为我正在工作的项目检查spring- boot。我一直在遵循指南,最后有一个(半)运行中的Web应用程序MVC + JPA用于数据访问。当我通过Jar方法部署应用程序时,一切正常: 但是,我们的应用程序最终将部署到Tomcat(v7.0.40),因此我需要从项目中创建一个war文件。我已经在spring.io网站上遵循了将jars转换为

  • 我正在开发一个Spring Boot应用程序,我使用application.properties配置db连接、服务器端口等。 例如,如何在application.properties中实现以下XML配置

  • 我在将spring boot应用程序部署到tomcat时遇到了问题,尽管在IDE中工作得很好。我按照https://docs.spring.io/spring-boot/docs/current/reference/html/howto-tegratic-deployment.html中的说明进行了操作 更详细的结构: mvn clear包给出错误:

  • service类有一个方法可以进行查询,从Application.properties文件中填充了属性的成员变量中获取查询字符串: 第二个SpringBoot应用程序是一个具有REST控制器的Web应用程序。这些控制器公开调用CaseInvocationAPI库(特别是CaseInvocationService类)的endpoint。 我遇到的问题是,当SpringBoot WEBAPPLICAT

  • 当我们使用任何数据库(如cassandra/mongo)时,Spring Boot应用程序属性需要遵循https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html的约定。如果我们想为数据库设置声明自己的属性而不是spring-boot约定,我们需要做哪些步骤来设置数

  • 我正在使用SpringDoc1.4和Spring-Boot2.3,在OperationCustomizer类中,我需要从应用程序属性文件中读取值。但每次都将字段初始化为。规格如下 应用程序.属性 OperationCustomizer类