【发布时间】: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