英文:
Spring boot project war running in External TOMCAT fails
问题
以下是翻译好的部分:
我刚接触 Spring Boot,创建了一个新项目,并生成了相应的 WAR 文件。
在使用 Eclipse 并运行主类时,一切都运行正常。
public class Java3Application {
private static final Logger logger = LogManager.getLogger(Java3Application.class);
public static void main(String[] args) {
SpringApplication.run(Java3Application.class, args);
}
}
但是,当我将同样的 WAR 文件部署到外部的 TOMCAT 时,应用程序会抛出以下异常:
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)
...
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)
...
framework.properties
是我的属性文件,在通过主类运行应用程序时首次加载。
@RestController
public class ConfigServlet {
@Autowired
private Cache cache;
@Autowired
private StartupBean startupbean;
@PostConstruct
public void init() {
}
}
在这里,我使用了自动装配 Cache
类,它会加载 Framework.properties
。
@Component
@Configuration
@PropertySource("Framework.properties")
public class Cache {
}
注意:我没有使用 web.xml
文件,而是使用 @PostConstruct
来启动应用程序。
英文:
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.
答案1
得分: 0
我解决了这个问题,
i)需要在通过@propertysource
读取的每个属性文件名之前添加前缀classpath:
,否则会抛出缺少属性文件的异常。
ii)移除@component
,我不必要地添加了一些注解。
按照这些步骤进行后,属性文件能够在外部TOMCAT中加载,并且war包得以部署。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论