1

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:

  1. 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
  2. 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

Community
  • 1
  • 1
Laurent
  • 123
  • 1
  • 7
  • will need spring-webmvc.jar. I suggest you to read http://stackoverflow.com/questions/3977973/whats-the-difference-between-mvcannotation-driven-and-contextannotation – Sheetal Mohan Sharma Jun 03 '15 at 08:49
  • @Sheetal: Thank you for your comment. The ``spring-webmvc.jar`` is already included in a dependency of the current project. Moreover, the problem here is that the ``@Autowired`` is not working even if the ``component-scan`` is in the configuration. Moreover, following your link, the ``mvc:annotation-driven`` will only scan annotations that are not in this class if I am not wrong? – Laurent Jun 03 '15 at 09:04
  • Are all autowired fields null ? – John Jun 03 '15 at 09:47
  • @user3360241: Yes, there are all null in this class. Except with the ``@Component`` when it is scanned, then, they are all filled in. – Laurent Jun 03 '15 at 09:58
  • How did you detect that there are two instances when you annotate class with @Componenet. This should be singleton. – John Jun 03 '15 at 10:09
  • @user3360241: I agree it should be a singleton but it is not the case. I added a default constructor in my class in which I put a breakpoint during the startup of the server. I also added some log in the constuctor. The first to be created is the one with the null fields. Then the one with the filled fields is created. During the execution, I still have a NullPointerException meaning that the first created one is still used. – Laurent Jun 03 '15 at 10:47

0 Answers0