我正在使用以下应用程序类运行spring boot应用程序:
@EnableAutoConfiguration
@Configuration
public class FakeAppBooter extends SpringBootServletInitializer{
public static void main(String args[]) {
SpringApplication.run(FakeAppBooter.class, args);
System.out.println("Test");
}
@Bean
public EmbeddedServletContainerFactory servletContainer() {
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
factory.setTomcatContextCustomizers(Arrays.asList(new CustomCustomizer()));
factory.addErrorPages(
new ErrorPage(HttpStatus.UNAUTHORIZED,"/WEB-INF/views/unprot/common/errors/containerError.jsp"),
new ErrorPage(HttpStatus.NOT_FOUND,"/WEB-INF/views/unprot/common/errors/containerError.jsp"),
new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR,"/WEB-INF/views/unprot/common/errors/containerError.jsp"));
MimeMappings mappings = new MimeMappings(MimeMappings.DEFAULT);
mappings.add("xsd", "text/xml");
mappings.add("wsdl", "text/xml");
factory.setMimeMappings(mappings);
return factory;
}
static class CustomCustomizer implements TomcatContextCustomizer {
@Override
public void customize(Context context) {
context.setUseHttpOnly(true);
context.addWelcomeFile("home.html");
}
}
@Bean
public ServletContextInitializer initializer() {
return new ServletContextInitializer() {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
servletContext.setInitParameter("parentContextKey", "globalContext");
servletContext.setInitParameter("contextConfigLocation", "classpath*:com/my/**/fakeContext.xml");
}
};
}
@Bean
public ServletRegistrationBean mainServlet(){
ServletRegistrationBean servlet = new ServletRegistrationBean();
servlet.setName("main");
servlet.setServlet(new DispatcherServlet());
servlet.addUrlMappings("*.html","*.htmlf","*.json",
"/my/issue","/my/query","/my/adjust","/my/echo");
return servlet;
}
@Bean
public ServletRegistrationBean cxfServlet(){
ServletRegistrationBean servlet = new ServletRegistrationBean();
servlet.setName("CXFServlet");
servlet.setServlet(new CXFServlet());
servlet.setLoadOnStartup(1);
servlet.addUrlMappings("/services/*");
return servlet;
}
@Bean
public ServletRegistrationBean axisServlet(){
ServletRegistrationBean servlet = new ServletRegistrationBean();
servlet.setName("AxisServlet");
servlet.setServlet(new AxisServlet());
servlet.addInitParameter("axis.servicesPath", "/axis/");
servlet.addUrlMappings("/axis/*","*.jws");
return servlet;
}
@Bean
public FilterRegistrationBean httpMethodFilter(){
FilterRegistrationBean filter = new FilterRegistrationBean();
filter.setName("httpMethod");
filter.setFilter(new HiddenHttpMethodFilter());
filter.addUrlPatterns("*.html","*.htmlf","*.json");
return filter;
}
@Bean
public FilterRegistrationBean sitemeshFilter(){
FilterRegistrationBean filter = new FilterRegistrationBean();
filter.setName("sitemesh");
filter.setFilter(new SiteMeshFilter());
filter.addUrlPatterns("*.html");
return filter;
}
}
我的布局如下
我尝试使用上面的布局,以及将WEB-INF文件夹移动到资源文件夹
如果我在weabpp下将项目打包为带有WEB-INF的war文件,我会在war文件的根目录中获取WEB-INF文件夹,但是在启动时我得到:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Thread.java:722)
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [WEB-INF/main-servlet.xml]; nested exception is java.io.FileNotFoun
dException: class path resource [WEB-INF/main-servlet.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsFromImportedResources(ConfigurationClassBeanDefinitionReader.java:313)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:138)
at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:116)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:330)
at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:611)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
at boot.FakeAppBooter.main(FakeAppBooter.java:73)
... 6 more
Caused by: java.io.FileNotFoundException: class path resource [WEB-INF/main-servlet.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
... 25 more
如果我将WEB-INF文件夹移动到资源文件夹并打包为jar文件,我在启动时没有错误,但是当我尝试加载网页时仍然会收到FileNotFoundException,如下所示:
java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/main-servlet.xml]
at org.springframework.web.context.support.ServletContextResource.getInputStream(ServletContextResource.java:141)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:125)
at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:94)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:542)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624)
at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672)
at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543)
at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484)
at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
at javax.servlet.GenericServlet.init(GenericServlet.java:244)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1284)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:884)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1736)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1695)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:722)
答案 0 :(得分:3)
除非您将应用程序作为WAR运行,否则src/main/webapp
文件夹将不会包含在输出中,如文档中所述:
如果您的应用程序将打包为jar,请不要使用
src/main/webapp
文件夹。虽然这个文件夹是一个通用标准,但它只适用于war包装,如果你生成一个jar,它将被大多数构建工具默默忽略。
这可能是你的问题。
要修复它,请尝试显式为主servlet设置配置位置:
servlet.addInitParameter("contextConfigLocation", "classpath:main-servlet.xml");
并将main-servlet.xml
文件放入src/main/resources
。
希望这有帮助。
答案 1 :(得分:0)
我也遇到了同样的问题,即在测试流程中找不到.properties文件。 添加“ classpath:file.extension”对我有用。