春天@Autowired为messageSource的控制器,但不是在其他类的工作吗? [英] Spring @Autowired messageSource working in Controller but not in other classes?

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

问题描述

新的更新:
2010年12月31日8:15 PM结果
很脏修复,但我这是怎么暂时取得了为messageSource工作。我改变了我的控制器类传递为messageSource到Message类是能够检索的消息。请检查类的定义如下,让我知道任何你可能需要帮助更多的信息。我真的AP preciate所有帮助你提供。

2010年12月31日下午3点结果
因为我无法通过注解配置为messageSource成功,我试图通过servlet的context.xml的配置为messageSource注入。我仍然为messageSource为空。请让我知道如果您需要任何更具体的信息,我会提供。感谢您的帮助提前。

 的servlet-context.xml中
<豆类:豆类的id =消息
    类=com.mycompany.myapp.domain.common.message.Message>
    <豆类:属性名=为messageSourceREF =为messageSource/>
< /豆:豆>

春季给出关于春天的初始化以下信息消息。

 信息:org.springframework.context.annotation.ClassPathBeanDefinitionScanner  -  JSR-330javax.inject.Named注释发现支持组件扫描INFO:org.springframework.beans.factory.support.DefaultListableBeanFactory  - 重写bean定义为豆消息:更换[通用豆:类[com.mycompany.myapp.domain.common.message.Message]范围=单;抽象= FALSE; lazyInit = FALSE; autowireMode = 0; dependencyCheck = 0; autowireCandidate = TRUE;主要= FALSE; factoryBeanName = NULL; factoryMethodName = NULL; initMethodName = NULL; destroyMethodName = NULL;在文件中定义[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]]与[通用豆:类[com.mycompany.myapp.domain.common.message.Message]范围=;抽象= FALSE; lazyInit = FALSE; autowireMode = 0; dependencyCheck = 0; autowireCandidate = TRUE;主要= FALSE; factoryBeanName = NULL; factoryMethodName = NULL; initMethodName = NULL; destroyMethodName = NULL;在ServletContext的资源定义[/WEB-INF/spring/appServlet/servlet-context.xml]INFO:org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor ​​ -  JSR-330javax.inject.Inject注释发现支持自动装配INFO:org.springframework.beans.factory.support.DefaultListableBeanFactory  - 在org.springframework.beans.factory.support.DefaultListableBeanFactory@1c7caac5 pre-实例单身:定义豆[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.Htt$p$pquestHandlerAdapter,org.springframework.web.servlet.resource.ResourceHtt$p$pquestHandler#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];父:org.springframework.beans.factory.support.DefaultListableBeanFactory@4f47af3



我在3类消息源下面的定义。在调试模式下,我可以看到,在类 xxxController 为messageSource 初始化为组织。 springframework.context.support.ReloadableResourceBundleMessageSource 。我已经注明消息类与@Component和xxxHibernateDaoImp​​l与@Repository。我也包含在servlet的context.xml中的上下文命名空间定义。但在消息类和 xxxHibernateDaoImp​​l 类,在为messageSource 是还是空。

为什么不春季在其他两个类初始化为messageSource尽管在 xxxController 类,它正确初始化?

  @Controller
公共类xxxController {
    @Autowired
    私人ReloadableResourceBundleMessageSource为messageSource;
}@零件
公共类信息{
 @Autowired
 私人ReloadableResourceBundleMessageSource为messageSource;
}@Repository(xxxDao)
公共类xxxHibernateDaoImp​​l {
 @Autowired
 私人ReloadableResourceBundleMessageSource为messageSource;
}<豆类:豆类
    的xmlns:上下文=htt​​p://www.springframework.org/schema/context
    XSI:的schemaLocation =
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd\"><豆类:豆类ID =为messageSource级=org.springframework.context.support.ReloadableResourceBundleMessageSource>
            <豆类:属性名=基名价值=/资源/信息/邮件/>
< /豆:豆>    <背景:组件扫描基包=com.mycompany.myapp/>
< /豆>


解决方案

春季不知道那些你从得到一个空值字段类。您可以通过定义它们作为你的应用程序上下文豆或标注类为 @Component

告诉Spring关于他们

为什么你的第一类是越来越正确自动装配的原因是该类正确标注为 @Controller

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.

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 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



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.

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 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

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

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

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