Search code examples
javaspringspring-boottomcat8

Spring boot project war running in External TOMCAT fails


I am new to Spring Boot and I created a new project and generated war of the same. When I am running on the Eclipse with the main class,it is running fine.

public class Java3Application {

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

    }
}

But when I am deploying the same as war in an External TOMCAT, the application throws an exception like this,

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)

The framework.properties is my property file, which is getting loading first time while i am running the application via main class.

@RestController
public class ConfigServlet {

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

Here, I am auto wiring the class "Cache", and it is loading "Framework.properties"

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

Note: I am not using web.xml file, i am using @Postconstruct to start the application.


Solution

  • I solved this one,

    i)need to add the prefix classpath: before every property file name that we are reading through @propertysource, other wise it throw missing properties file exception.

    ii) removing @component , i put few annotations un-necessarily.

    After following these steps, properties file was able to load in the External TOMCAT and war got deployed.