following the answer given in this question, I have removed the spring-webmvc.jar
file from my lib to avoid a repetition with the one in the core project. However, when I do this, it seems that the @Autowired
for at least a bean does not work any more.
The class having the @Autowired
is the following (in which none of the field is filled):
public class SecurityUserCheckBeforeControllerHandler implements BeforeControllerHandler
{
@Resource(name = "userService")
private UserService userService;
@Autowired
private CMSPageContextService cmsPageContextService;
@Override
public boolean beforeController(final HttpServletRequest request, final HttpServletResponse response,
final HandlerMethod handler) throws IOException
{
// Code where the autowired fields are used (-> produces null pointer)
}
}
The spring configuration can be summarized as follows:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<context:component-scan base-package="my.package" scope-resolver="de.hybris.platform.spring.IgnoreTenantScopeMetadataResolver" />
<mvc:annotation-driven ignore-default-model-on-redirect="true" validator="validator">
<mvc:message-converters>
<bean class="org.springframework.http.converter.ResourceHttpMessageConverter"/>
<bean class="org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<alias name="defaultBeforeControllerHandlersList" alias="beforeControllerHandlersList" />
<util:list id="defaultBeforeControllerHandlersList" >
<bean class="be.sbh.site.storefront.interceptors.beforecontroller.SecurityUserCheckBeforeControllerHandler" />
<!-- other beans in the list -->
</util:list>
<alias alias="cmsPageContextService" name="defaultCMSPageContextService" />
<bean id="defaultCMSPageContextService"
class="de.hybris.platform.acceleratorcms.services.impl.DefaultCMSPageContextService">
<!-- Properties -->
</bean>
<alias alias="userService" name="defaultUserService"/>
<bean id="defaultUserService" class="de.hybris.platform.servicelayer.user.impl.DefaultUserService" parent="abstractBusinessService">
<!-- Properties -->
</bean>
</beans>
If I follow the advise given in most of the similar questions (i.e. adding an @Component
above the class that would be scanned), the bean will be created twice:
- with the list given in the config file:
The autowired fields will be null which will still give a
NullPointerException
when the bean in the list is used - by the
component-scan
: The fields are correctly autowired but the bean is not used in the list.
Strangely, if I put back the spring-webmvc.jar
previously removed because of this question, the @Autowired
will work as expected.
Trying to compare the stacktraces between the two configurations, I saw that the beans are created at different moments in the class org.springframework.context.support.AbstractApplicationContext
during the startup of the server.
Last point: there is no error during the compilation and the startup of the server.
Do you have any idea for a solution please?
Thank you for reading me,
Laurent