Spring @Autowired messageSource 在 Controller 中工作但不在其他类中? [英] Spring @Autowired messageSource working in Controller but not in other classes?

查看:49
本文介绍了Spring @Autowired messageSource 在 Controller 中工作但不在其他类中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新更新:2010 年 12 月 31 日晚上 8:15
非常脏的修复,但这就是我暂时使 messageSource 工作的方式.我更改了我的 Controller 类以将messageSource"传递给 Message 类,并且能够检索消息.请查看下面的类定义,让我知道您可能需要帮助的更多信息.我非常感谢您提供的所有帮助.

New updates: Dec 31 2010 8:15 PM
Very dirty fix but this is how I have temporarily made the messageSource to work. I changed my Controller class to pass the 'messageSource' to Message class and am able to retrieve the messages. Please review the class definition below and let me know any more info that you may need to help. I really appreciate all the help you are providing.

2010 年 12 月 31 日下午 3 点
由于我无法通过注解成功配置 messageSource,我尝试通过 servlet-context.xml 配置 messageSource 注入.我仍然将 messageSource 设为 null.如果您需要更具体的信息,请告诉我,我会提供.提前感谢您的帮助.

Dec 31 2010 3 PM
As I could not succeed in configuring messageSource through annotations, I attempted to configure messageSource injection through servlet-context.xml. I still have messageSource as null. Please let me know if you need any more specific info, and I will provide. Thanks for your help in advance.

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 初始化的信息消息.

Spring gives the below information 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

<小时><小时>

我对 3 个类中的消息源有以下定义.在调试模式下,我可以看到在xxxController 类中,messageSource 被初始化为org.springframework.context.support.ReloadableResourceBundleMessageSource.我用@Component 注释了Message 类,用@Repository 注释了xxxHibernateDaoImpl.我还在 servlet-context.xml 中包含了上下文命名空间定义.但是在Message类和xxxHibernateDaoImpl类中,messageSource还是null.



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

为什么 Spring 没有在其他两个类中初始化 messageSource 而在 xxxController 类中,它已正确初始化?

Why is Spring not initializing messageSource in the two other classes though in xxxController classes, it initializes correctly?

@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 不知道您从中获取空值字段的那些类.您可以通过在应用程序上下文中将它们定义为 bean 或将类注释为 @Component

Spring does not know about those classes that you are getting a null value field from. You can tell Spring about them by defining them as beans in your app context or annotating the classes as @Component

您的第一个类正确自动装配的原因是该类被正确注释为 @Controller

The reason why your first class is getting autowired correctly is because that class is correctly annotated as @Controller

这篇关于Spring @Autowired messageSource 在 Controller 中工作但不在其他类中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆