Spring does not enter ServletContext

I am using Spring 3.0.7 and for some reason in our JBOSS web application we cannot get ServletContext in our bean:

 @Component("assembler") public class DefaultAssemblerStrategy implements AssemblerStrategy//, ServletContextAware { @Autowired//(required=false) private ServletContext servletCtxt; public void setServletContext(ServletContext servletContext) { System.out.println("~~~~~~~~~~~~setServletContext"); servletCtxt = servletContext; } 

Note that this is not on the web server itself during the launch of the JUnit test case.

If I try Autwired, I get this error:

  org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'assembler': Injection of autowired dependencies failed;  nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.servlet.ServletContext com.ibm.retail.xc.maestro.web.theme.impl.DefaultAssemblerStrategy.servletCtxt;  nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.servlet.ServletContext] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.  Dependency annotations: {@ org.springframework.beans.factory.annotation.Autowired (required = true)}
             at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues ​​(AutowiredAnnotationBeanPostProcessor.java:287)
             at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean (AbstractAutowireCapableBeanFactory.java:1073)
             at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (AbstractAutowireCapableBeanFactory.java∗16)
             at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean (AbstractAutowireCapableBeanFactory.java:455)

And if I implement ServletContextAware , it's just null ...

+4
source share
2 answers

It looks like you are not using ContextLoaderListener . ServletContextAware relies on a BeanFactoryPostProcessor , which is set by AbstractRefreshableWebApplicationContext inside refresh() . Context relies on ContextLoader to inject a ServletContext into it. Therefore, you can solve this problem by using ContextLoaderListener in your application or (possibly just) calling AbstractRefreshableWebApplicationContext#setServletContext() before updating the context.

The ServletContext bean definition may also work, but conceptually, the servlet context is “outside” the Spring context - it is indeed the servlet context that controls the Spring context, and not vice versa. Fortunately, this approach is completely unnecessary.

0
source

madth3 pointed out a possible reason: namely, that the bean is defined with the name applicationContext. This makes sense, for example, for testing with a MockApplicationContext, but not in the application itself.

0
source

Source: https://habr.com/ru/post/1400604/


All Articles