SpringBoot使用SpringBootServletInitializer启动war,servletContext.setInitParameter方式设置contextConfigLocation无效

noob_fly 发布于 2017/08/03 16:28
阅读 14K+
收藏 0

版本: tomcat8、springboot 1.3.6.RELEASE; spring-context 4.2.7.RELEASE; 
原web.xml配置方式启动没问题:

<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath*:spring/applicationContext-per.xml
    </param-value>
</context-param>
<filter>
	<filter-name>loginAndAuthFilter</filter-name>
	<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
	<init-param>
		<param-name>targetFilterLifecycle</param-name>
		<param-value>true</param-value>
	</init-param>
</filter>
<filter-mapping>
	<filter-name>loginAndAuthFilter</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping> 

现在改为无web.xml方式启动:

@SpringBootApplication(scanBasePackages = { "com.iboxpay.loan.route" })
//@ImportResource(locations = "classpath*:spring/applicationContext-per.xml")
@MapperScan(basePackages = "com.iboxpay.loan.route.dao")
@Slf4j
public class RouteConsoleBootstrap extends SpringBootServletInitializer {

	private static Class<RouteConsoleBootstrap> applicationClass = RouteConsoleBootstrap.class;

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(applicationClass);
	}

	/**
	 * 还有一种:in a single line in application.properties:
	 * server.context_parameters.p-name=-value
	 * 
	 */
	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		servletContext.setInitParameter("contextConfigLocation","classpath*:spring/applicationContext-per.xml");
		servletContext.setInitParameter("failUrl", "/");
		servletContext.setInitParameter("unauthorizedUrl", "/deny.jsp");
		servletContext.setInitParameter("notFilterUrl", "");
		super.onStartup(servletContext);
	}

启动后的效果:

contextConfigLocation在设置前或者设置后都是这个值, 无法读取配置的资源文件。


改成注解@ImportResource方式:

@SpringBootApplication(scanBasePackages = { "com.iboxpay.loan.route" })
@ImportResource(locations = "classpath*:spring/applicationContext-per.xml")
@MapperScan(basePackages = "com.iboxpay.loan.route.dao")
@Slf4j
public class RouteConsoleBootstrap extends SpringBootServletInitializer {

	private static Class<RouteConsoleBootstrap> applicationClass = RouteConsoleBootstrap.class;

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		return builder.sources(applicationClass);
	}

	/**
	 * 还有一种:in a single line in application.properties:
	 * server.context_parameters.p-name=-value
	 * 
	 */
	@Override
	public void onStartup(ServletContext servletContext) throws ServletException {
		// servletContext.setInitParameter("contextConfigLocation","classpath*:spring/applicationContext-per.xml");
		servletContext.setInitParameter("failUrl", "/");
		servletContext.setInitParameter("unauthorizedUrl", "/deny.jsp");
		servletContext.setInitParameter("notFilterUrl", "");
		super.onStartup(servletContext);
	}

可以读入资源文件,实例化bean。
该资源文件中集成了Shiro,使用DelegatingFilterProxy管理FilterChain

	@Bean
	public FilterRegistrationBean registerLoginAndAuthFilter() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new DelegatingFilterProxy());
		filterRegistrationBean.addInitParameter("targetFilterLifecycle", "true");
		filterRegistrationBean.addInitParameter("targetBeanName", "loginAndAuthFilter");
		return filterRegistrationBean;
	}

当服务启动后报错:

2017-08-03 16:22:36.309  INFO 4824 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/test/method1.json]}" onto public java.lang.String com.iboxpay.loan.route.web.controller.TestConstroller.test()
2017-08-03 16:22:36.316  INFO 4824 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-08-03 16:22:36.316  INFO 4824 --- [ost-startStop-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-08-03 16:22:36.357  INFO 4824 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-03 16:22:36.357  INFO 4824 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-03 16:22:36.427  INFO 4824 --- [ost-startStop-1] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-08-03 16:22:39.563  INFO 4824 --- [ost-startStop-1] o.s.ui.freemarker.SpringTemplateLoader   : SpringTemplateLoader for FreeMarker: using resource loader [org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2d2b415e: startup date [Thu Aug 03 16:22:32 CST 2017]; root of context hierarchy] and template loader path [classpath:/templates/]
2017-08-03 16:22:39.564  INFO 4824 --- [ost-startStop-1] o.s.w.s.v.f.FreeMarkerConfigurer         : ClassTemplateLoader for Spring macros added to FreeMarker configuration
2017-08-03 16:22:39.856  INFO 4824 --- [ost-startStop-1] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-08-03 16:22:39.858  INFO 4824 --- [ost-startStop-1] o.s.j.e.a.AnnotationMBeanExporter        : Bean with name 'dataSource' has been autodetected for JMX exposure
2017-08-03 16:22:39.866  INFO 4824 --- [ost-startStop-1] o.s.j.e.a.AnnotationMBeanExporter        : Located MBean 'dataSource': registering with JMX server as MBean [com.alibaba.druid.pool:name=dataSource,type=DruidDataSource]
2017-08-03 16:22:39.872  INFO 4824 --- [ost-startStop-1] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 2147483647
2017-08-03 16:22:39.872  INFO 4824 --- [ost-startStop-1] o.s.s.quartz.SchedulerFactoryBean        : Starting Quartz Scheduler now
2017-08-03 16:22:39.872  INFO 4824 --- [ost-startStop-1] org.quartz.core.QuartzScheduler          : Scheduler org.springframework.scheduling.quartz.SchedulerFactoryBean#0_$_NON_CLUSTERED started.
2017-08-03 16:22:39.883  INFO 4824 --- [ost-startStop-1] c.i.l.r.w.access.RouteConsoleBootstrap   : Started RouteConsoleBootstrap in 8.51 seconds (JVM running for 12.837)
2017-08-03 16:22:39.917  INFO 4824 --- [ost-startStop-1] c.iboxpay.per.client.init.AuthenURLInit  : >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>鍒濆鍖朿as server 鎵?闇?鍦板潃銆愬紑濮嬨??
2017-08-03 16:22:40.030  INFO 4824 --- [ost-startStop-1] c.iboxpay.per.client.init.AuthenURLInit  : >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>鍒濆鍖朿as server 鎵?闇?鍦板潃銆愮粨鏉熴??
2017-08-03 16:22:40.185  INFO 4824 --- [ost-startStop-1] org.quartz.simpl.SimpleThreadPool        : Job execution threads will use class loader of thread: localhost-startStop-1
2017-08-03 16:22:40.189  INFO 4824 --- [ost-startStop-1] org.quartz.core.SchedulerSignalerImpl    : Initialized Scheduler Signaller of type: class org.quartz.core.SchedulerSignalerImpl
2017-08-03 16:22:40.190  INFO 4824 --- [ost-startStop-1] org.quartz.core.QuartzScheduler          : Quartz Scheduler v.1.6.5 created.
2017-08-03 16:22:40.190  INFO 4824 --- [ost-startStop-1] org.quartz.simpl.RAMJobStore             : RAMJobStore initialized.
2017-08-03 16:22:40.190  INFO 4824 --- [ost-startStop-1] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler 'DefaultQuartzScheduler' initialized from default resource file in Quartz package: 'quartz.properties'
2017-08-03 16:22:40.190  INFO 4824 --- [ost-startStop-1] org.quartz.impl.StdSchedulerFactory      : Quartz scheduler version: 1.6.5
2017-08-03 16:22:40.643  INFO 4824 --- [ost-startStop-1] o.j.c.c.session.SingleSignOutFilter      : Property [artifactParameterName] not found.  Using default value [ticket]
2017-08-03 16:22:40.651 ERROR 4824 --- [ost-startStop-1] a.c.c.C.[.[.[/loan-routing-platform-web] : Exception starting filter cookiesFilter

javax.naming.NameNotFoundException: Name [com.iboxpay.per.client.filter.auth.AuthenCookiesLoginFilter/iucCacheManager] is not bound in this Context. Unable to find [com.iboxpay.per.client.filter.auth.AuthenCookiesLoginFilter].
	at org.apache.naming.NamingContext.lookup(NamingContext.java:818) ~[catalina.jar:8.0.30]
	at org.apache.naming.NamingContext.lookup(NamingContext.java:166) ~[catalina.jar:8.0.30]
	at org.apache.catalina.core.DefaultInstanceManager.lookupFieldResource(DefaultInstanceManager.java:573) ~[catalina.jar:8.0.30]
	at org.apache.catalina.core.DefaultInstanceManager.processAnnotations(DefaultInstanceManager.java:461) ~[catalina.jar:8.0.30]
	at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:144) ~[catalina.jar:8.0.30]
	at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:136) ~[catalina.jar:8.0.30]
	at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108) ~[catalina.jar:8.0.30]
	at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4659) [catalina.jar:8.0.30]
	at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5281) [catalina.jar:8.0.30]
	at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [catalina.jar:8.0.30]
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408) [catalina.jar:8.0.30]
	at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398) [catalina.jar:8.0.30]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_45]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_45]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_45]
	at java.lang.Thread.run(Thread.java:745) [na:1.8.0_45]

2017-08-03 16:22:40.651 ERROR 4824 --- [ost-startStop-1] o.apache.catalina.core.StandardContext   : One or more Filters failed to start. Full details will be found in the appropriate container log file
2017-08-03 16:22:40.651 ERROR 4824 --- [ost-startStop-1] o.apache.catalina.core.StandardContext   : Context [/loan-routing-platform-web] startup failed due to previous errors
2017-08-03 16:22:40.716  INFO 4824 --- [ost-startStop-1] a.c.c.C.[.[.[/loan-routing-platform-web] : Closing Spring root WebApplicationContext
2017-08-03 16:22:40.716  INFO 4824 --- [ost-startStop-1] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@2d2b415e: startup date [Thu Aug 03 16:22:32 CST 2017]; root of context hierarchy
2017-08-03 16:22:40.718  INFO 4824 --- [ost-startStop-1] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 2147483647
2017-08-03 16:22:40.719  INFO 4824 --- [ost-startStop-1] org.quartz.core.QuartzScheduler          : Scheduler org.springframework.scheduling.quartz.SchedulerFactoryBean#0_$_NON_CLUSTERED paused.
2017-08-03 16:22:40.719  INFO 4824 --- [ost-startStop-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2017-08-03 16:22:40.720  INFO 4824 --- [ost-startStop-1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans
2017-08-03 16:22:40.721  INFO 4824 --- [ost-startStop-1] o.s.s.quartz.SchedulerFactoryBean        : Shutting down Quartz Scheduler
2017-08-03 16:22:40.721  INFO 4824 --- [ost-startStop-1] org.quartz.core.QuartzScheduler          : Scheduler org.springframework.scheduling.quartz.SchedulerFactoryBean#0_$_NON_CLUSTERED shutting down.
2017-08-03 16:22:40.721  INFO 4824 --- [ost-startStop-1] org.quartz.core.QuartzScheduler          : Scheduler org.springframework.scheduling.quartz.SchedulerFactoryBean#0_$_NON_CLUSTERED paused.
2017-08-03 16:22:40.721  INFO 4824 --- [ost-startStop-1] org.quartz.core.QuartzScheduler          : Scheduler org.springframework.scheduling.quartz.SchedulerFactoryBean#0_$_NON_CLUSTERED shutdown complete.
2017-08-03 16:22:40.725  INFO 4824 --- [ost-startStop-1] org.quartz.core.QuartzScheduler          : Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutting down.
2017-08-03 16:22:40.725  INFO 4824 --- [ost-startStop-1] org.quartz.core.QuartzScheduler          : Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED paused.
2017-08-03 16:22:40.725  INFO 4824 --- [ost-startStop-1] org.quartz.core.QuartzScheduler          : Scheduler DefaultQuartzScheduler_$_NON_CLUSTERED shutdown complete.
2017-08-03 16:22:40.725  INFO 4824 --- [ost-startStop-1] a.s.s.m.AbstractValidatingSessionManager : Disabled session validation scheduler

测试显示: 生成webApplicationContext没有问题,但启动filters错误。

加载中
返回顶部
顶部