Felipe Arenales
Felipe Arenales

Reputation: 991

MessageSource is not being injected by Spring

I'm having an issue using MessageSource and Spring. For some unknown (for me, at least) reason, my messageSource object is not being instantiated.

I have the following components:

applicationContext.xml

...
<import resource="messageSource-ctx.xml" />
...
<context:component-scan base-package="com.foo.bar" />
...  


messageSource.xml

<beans ...>
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>backendErrorCodesMapping</value>
                <value>restErrorCodesMapping</value>
            </list>
        </property>
    </bean> 
</beans>


MessageSourceHelper.java

package com.foo.bar.util;

@Component
public class MessageSourceHelper {

    @Autowired
    private MessageSource messageSource;

    ...
}


GenericRestExceptionMapper.java

package com.foo.bar.rest.exception

@Component
public abstract class GenericRestExceptionMapper{

    @Resource
    private MessageSourceHelper msh;

    ....
}


InvalidRequestExceptionMapper.java

package com.foo.bar.rest.exception

@Provider
public class InvalidRequestExceptionMapper extends GenericRestExceptionMapper implements ExceptionMapper<InvalidRequestException> {

...

}

Once an InvalidRequestException occurs, it will be "cought" by the InvalidRequestExceptionMapper, that uses the inherited msh (MessageSourceHelper). The issue is that this object is null, i.e., it is not being injected by Spring.

What should be the problem? I appreciate all the help!

Thanks in advance.

Upvotes: 3

Views: 3949

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388446

I think your InvalidRequestExceptionMapper should have @Component and private MessageSourceHelper msh; should have @Autowired

Upvotes: 4

Related Questions