Spring 外部 TOMCAT 中的引导项目 war 运行 失败

Spring boot project war running in External TOMCAT fails

我是 Spring Boot 的新手,我创建了一个新项目并生成了相同的 war。 当我在 Eclipse 上 运行 和主要 class 时,它 运行 很好。

public class Java3Application {

    private static final Logger logger = LogManager.getLogger(Java3Application.class);
    public static void main(String[] args) {
        SpringApplication.run(Java3Application.class, args);

    }
}

但是当我在外部 TOMCAT 中部署与 war 相同的内容时,应用程序会抛出这样的异常,

08-Oct-2020 11:43:51.476 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start: 
    org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/UMS_JAVA3-0.0.1-SNAPSHOT]]
        at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:198)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:743)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:719)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:705)
        at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:970)
        at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1840)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.sixdee.UmsJava3Application]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/Framework.properties]
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:188)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:319)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:236)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:280)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:96)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:707)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:533)
        at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:143)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750)

framework.properties 是我的 属性 文件,它是在我 运行 通过主 class 应用程序时第一次加载的。

@RestController
public class ConfigServlet {

    @Autowired
    private Cache cache;
    
    @Autowired
    private StartupBean startupbean;
    
    @PostConstruct
    public void init() {
}

在这里,我正在自动连接 class“缓存”,它正在加载“Framework.properties”

@Component
@Configuration
@PropertySource("Framework.properties")
public class Cache {
}

注意:我没有使用 web.xml 文件,我正在使用 @Postconstruct 来启动应用程序。

我解决了这个,

i)需要在我们通过 @propertysource 读取的每个 属性 文件名之前添加前缀 classpath:,否则它会抛出缺少属性文件异常。

ii) 删除 @component ,我放了一些注释 un-necessarily.

执行这些步骤后,属性文件能够加载到外部 TOMCAT 并且 war 已部署。