Spring @ Activated messageSource working in the controller, but not in other classes?

New updates: December 31, 2010 8:15 pm
A very dirty fix, but this is how I temporarily made messageSource work. I changed my controller class to pass "messageSource" to the Message class and can receive messages. Check out the class definition below and let me know more information you might need. I really appreciate all the help you provide.

December 31, 2010 15:00
Since I was unable to configure messageSource using annotations, I tried to configure the implementation of messageSource through servlet-context.xml. I still have messageSource as null. Please let me know if you need more details and I will provide. Thank you in advance for your help.

servlet-context.xml <beans:bean id="message" class="com.mycompany.myapp.domain.common.message.Message"> <beans:property name="messageSource" ref="messageSource" /> </beans:bean> 

Spring gives below an informational message about spring initialization.

 INFO : org.springframework.context.annotation.ClassPathBeanDefinitionScanner - JSR-330 'javax.inject.Named' annotation found and supported for component scanning INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Overriding bean definition for bean 'message': replacing [Generic bean: class [com.mycompany.myapp.domain.common.message.Message]; scope=singleton; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in file [C:\springsource\tc-server-developer-2.1.0.RELEASE\spring-insight-instance\wtpwebapps\myapp\WEB-INF\classes\com\mycompany\myapp\domain\common\message\Message.class]] with [Generic bean: class [com.mycompany.myapp.domain.common.message.Message]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]] INFO : org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor - JSR-330 'javax.inject.Inject' annotation found and supported for autowiring INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1c7caac5: defining beans [org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0,org.springframework.format.support.FormattingConversionServiceFactoryBean#0,org.springframework.validation.beanvalidation.LocalValidatorFactoryBean#0,org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter#0,org.springframework.web.servlet.handler.MappedInterceptor#0,org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter,org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0,org.springframework.web.servlet.handler.SimpleUrlHandlerMapping#0,xxxDao,message,xxxService,jsonDateSerializer,xxxController,homeController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,tilesViewResolver,tilesConfigurer,messageSource,org.springframework.web.servlet.handler.MappedInterceptor#1,localeResolver,org.springframework.web.servlet.view.ContentNegotiatingViewResolver#0,validator,resourceBundleLocator,messageInterpolator]; parent: org.springframework.beans.factory.support.DefaultListableBeanFactory@4f47af3 


I have a definition for a message source in 3 classes. In debug mode, I see that in the xxxController class, messageSource initialized to org.springframework.context.support.ReloadableResourceBundleMessageSource . I have an annotated message class with @Component and xxxHibernateDaoImpl with @Repository. I also included the definition of the context namespace in servlet-context.xml. But in the Message class and xxxHibernateDaoImpl class, the value of messageSource is still null.

Why doesn't spring initialize messageSource in two other classes, although in xxxController classes it is correctly initialized?

 @Controller public class xxxController{ @Autowired private ReloadableResourceBundleMessageSource messageSource; } @Component public class Message{ @Autowired private ReloadableResourceBundleMessageSource messageSource; } @Repository("xxxDao") public class xxxHibernateDaoImpl{ @Autowired private ReloadableResourceBundleMessageSource messageSource; } <beans:beans xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <beans:property name="basename" value="/resources/messages/messages" /> </beans:bean> <context:component-scan base-package="com.mycompany.myapp"/> </beans> 
+6
java spring annotations
source share
2 answers

Spring does not know about the classes from which you get a field with a null value. You can tell Spring about them by specifying them as beans in the application context or annotating classes like @Component

The reason your first class is working correctly is because this class is correctly annotated as @Controller

+4
source share

Try to clarify the basic package. Do not let spring check the entire class of the com.mycompany.myapp application (you will have a better start time).

Suppose you have a controller class in the package: com.mycompany.myapp.controller and your service class (annotated with @component, @repository) in the com.mycompany.myapp.services package

Add this to your [servlet name] -servlet.xml (Dispatcher context file)

 <context:component-scan base-package="com.mycompany.myapp.controller" /> 

On the other hand, open your servlet-context.xml (assuming this is the application context loaded by the contextloaderlistener) and add this:

 <context:component-scan base-package="com.mycompany.myapp.services" /> 

followed by an announcement of your message source, as you did.

You will have one message source that will be entered each time it is auto-updated. It should work because the material defined in the Dispatcher-context file can see other beans declared in the context application file, but not vice versa

+2
source share

All Articles