【问题标题】:Spring Boot application - not reading application.properties in Tomcat, but works under Spring Tool SuiteSpring Boot 应用程序 - 不读取 Tomcat 中的 application.properties,但在 Spring Tool Suite 下工作
【发布时间】:2020-06-06 05:51:52
【问题描述】:

我编写了一个 Spring Boot Web 应用程序。该应用程序在 Spring Tool Suite 下运行良好,但是,当我在 Tomcat 中部署时,它无法读取配置值。我认为区别在于应用程序的运行方式。任何帮助或建议将不胜感激。我知道这个问题已经被问过很多次了 - 我确实尝试了所有建议的解决方案,但没有任何运气。

这是我的代码/配置:

TestApplication.java

package com.example.test;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class TestApplication extends SpringBootServletInitializer
{
  @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
    {
        return application.sources(TestApplication.class);
    }

  public static void main(String[] args)
  {
    ApplicationContext context = SpringApplication.run(TestApplication.class, args);

    ServiceConfig bean = ServiceConfig.getServiceConfig();
    AutowireCapableBeanFactory factory = context.getAutowireCapableBeanFactory();
    factory.autowireBean( bean );
    factory.initializeBean( bean, "ServiceConfig" );
  }
}

TestController.java

package com.example.test;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;

@RestController
public class TestController
{
  @GetMapping(value = "/")
  public String index()
  {
    return "Hello from " + ServiceConfig.getServiceConfig().getName();
  }
}

ServletInitializer.java

package com.example.test;

import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

public class ServletInitializer extends SpringBootServletInitializer {

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

}

ServiceConfig.java

package com.example.test;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("ServiceConfig")
public class ServiceConfig
{
  static private ServiceConfig serviceConfig = null;

  @Value("${spring.application.name}")
  private String name;

  public String getName()
  {
    return name;
  }

  private ServiceConfig() {}

  public static ServiceConfig getServiceConfig()
  {
    if (serviceConfig == null) {
      serviceConfig = new ServiceConfig();
    }

    return serviceConfig;
  }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>
  <groupId>com.example</groupId>
  <artifactId>test</artifactId>
  <version>1.0.0</version>
  <packaging>war</packaging>
  <name>test</name>
  <description>Spring Boot Test Application</description>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <start-class>com.example.test.TestApplication</start-class>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-tomcat</artifactId>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
      <exclusions>
        <exclusion>
          <groupId>org.junit.vintage</groupId>
          <artifactId>junit-vintage-engine</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

  <build>
    <plugins>
  <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
      <finalName>test</finalName>                   
    </configuration>
  </plugin>
    </plugins>
  </build>

</project>

application.properties

spring.application.name = Test-Service

部署步骤:

(1) Build the war file: mvn clean install
(2) Copy the generated test.war under Tomcat /webapps directory.
(3) Start tomcat using /bin/start.sh

问题:

当我浏览http://localhost:8080/test 时,我看到配置参数没有被填充:

Hello from null

但是,我尝试在 Spring Tool Suite 下运行,我看到服务在 http://localhost:8080 运行,并且代码有效:

Hello from Test-Service


更新分辨率



正如 Andy Wilkinson 所指出的,war 文件没有调用主类,因此没有加载配置。

我添加 ServiceConfig bean 的临时初始化的原因是 @Component 注释没有自动加载 ServiceConfig 类中的 application.properties。无论如何,我最终使用了 Java 的 @PostConstruct 注解,它既适用于独立的 Java 应用程序,也适用于基于战争的 Tomcat 应用程序:

这是更新后的代码:

TestApplication.java

package com.example.test;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationContext;

@SpringBootApplication
public class TestApplication extends SpringBootServletInitializer
{
    @Autowired
    private ApplicationContext appContext;

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

    @PostConstruct
    public void init()
    {       
        ServiceConfig bean = ServiceConfig.getServiceConfig();
        AutowireCapableBeanFactory factory = appContext.getAutowireCapableBeanFactory();
        factory.autowireBean( bean );
        factory.initializeBean( bean, "ServiceConfig" );
    }

    public static void main(String[] args)
    {
        SpringApplication.run(TestApplication.class, args);     

    }
}

再次感谢@Andy Wilkinson 的帮助!

最好的问候, MN

【问题讨论】:

  • 相对于您的 pom.xml 文件,application.properties 在哪里?它需要打包到war文件中的WEB-INF/classes中,所以它应该在src/main/resources中。

标签: spring-boot


【解决方案1】:

当部署为 war 文件时,不会调用应用程序的 main 方法。在这种情况下至关重要的是,这意味着不会调用以下代码:

ServiceConfig bean = ServiceConfig.getServiceConfig();
AutowireCapableBeanFactory factory = context.getAutowireCapableBeanFactory();
factory.autowireBean( bean );
factory.initializeBean( bean, "ServiceConfig" );

如果不调用此代码,TestController 检索的ServiceConfig 尚未初始化,因此其name 字段为null。

初始化 ServiceConfig 的代码对于 Spring Boot 应用程序来说是非常规的。我将完全删除它并依赖于 ServiceConfig bean 凭借其 @Component 注释自动创建和初始化。删除此代码后,您需要更新控制器以注入依赖项。我会为此使用构造函数注入:

@RestController
public class TestController
{
  private final ServiceConfig serviceConfig;

  TestController(ServiceConfig ServiceConfig) {
    this.serviceConfig = serviceConfig;
  }

  @GetMapping(value = "/")
  public String index()
  {
    return "Hello from " + this.serviceConfig.getName();
  }
}

【讨论】:

  • 非常感谢!是的,war 文件没有调用主类,因此没有加载配置。我添加 ServiceConfig bean 的临时初始化的原因是 Component 注释没有自动加载 ServiceConfig 类中的 application.properties。无论如何,我最终使用了 Java 的 PostConstruct 注解,它既适用于独立的 Java 应用程序,也适用于基于战争的 Tomcat 应用程序。
  • 顺便说一句,我仍然很好奇为什么 Component 和 Value 注释不能自动在 ServiceConfig.java 中工作。这是因为我正在使用单例模式创建一个 ServiceConfig 实例吗?在那种情况下,这种模式通常如何与 Spring Boot 一起使用?
  • 当您创建 ServiceConfig 实例而不是让 Spring 执行时,它们没有任何效果。 Spring bean 默认是单例的。正如我上面建议的那样,您应该删除创建和初始化 ServiceConfig 实例的代码,并将 Spring-creates 实例注入到您的控制器中。
  • 那么如果我需要从多个类中访问一个配置值,我需要在每个类中定义相同的属性并使用Value注解吗?
【解决方案2】:

将您的 application.properties 文件移动到 /webapps 目录将解决此问题。

【讨论】:

  • 这将使它可以通过 HTTP 远程访问。这样做是有风险的,尤其是因为它可能包含用户名和密码。
  • 感谢@Thomas 的建议。我尝试将 application.properties 移动到 /webapps 目录,但仍然看到 null。如果您有任何其他意见,请告诉我。
  • @Andy,你有什么建议吗?谢谢!
  • @MNWA 我昨天对这个问题发表了评论。我怀疑application.properties 没有被打包在你的.war 文件中,或者它被打包在错误的位置。
  • @Andy,抱歉,错过了该评论。是的,application.properties 在 /src/main/resources 下。我验证了该文件已打包在 WEB-INF\classes\ 下的 war 文件中。谢谢!
猜你喜欢
  • 1970-01-01
  • 2018-02-19
  • 2017-05-26
  • 1970-01-01
  • 2020-09-12
  • 2019-07-25
  • 2012-09-08
  • 2014-01-07
  • 2015-03-21
相关资源
最近更新 更多