【发布时间】:2010-12-31 01:32:01
【问题描述】:
新的更新:
2010 年 12 月 31 日晚上 8:15
非常肮脏的修复,但这就是我暂时使 messageSource 工作的方式。我更改了我的 Controller 类以将“messageSource”传递给 Message 类,并且能够检索消息。请查看下面的类定义,让我知道您可能需要帮助的更多信息。非常感谢您提供的所有帮助。
2010 年 12 月 31 日下午 3 点
由于无法通过注解配置messageSource,我尝试通过servlet-context.xml 配置messageSource 注入。我仍然有 messageSource 为空。如果您需要任何更具体的信息,请告诉我,我会提供。提前感谢您的帮助。
servlet-context.xml
<beans:bean id="message"
class="com.mycompany.myapp.domain.common.message.Message">
<beans:property name="messageSource" ref="messageSource" />
</beans:bean>
Spring 给出以下有关 Spring 初始化的信息。
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
我对 3 个类中的消息源有以下定义。在调试模式下,我可以看到在xxxController 类中,messageSource 被初始化为org.springframework.context.support.ReloadableResourceBundleMessageSource。我用@Component 注释了Message 类,用@Repository 注释了xxxHibernateDaoImpl。我还在 servlet-context.xml 中包含了上下文命名空间定义。但在Message 类和xxxHibernateDaoImpl 类中,messageSource 仍然为空。
为什么 Spring 没有在其他两个类中初始化 messageSource 而在 xxxController 类中初始化正确?
@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>
【问题讨论】:
-
你是如何向 Spring 声明这些类的?
-
是的。我已将这些课程宣布为春季。请参阅上面的更新定义。
标签: java spring annotations