簡體   English   中英

Spring @Autowired messageSource在Controller中工作,但不在其他類中工作嗎?

[英]Spring @Autowired messageSource working in Controller but not in other classes?

新更新:2010年12月31日晚上8:15
非常臟的修復程序,但這是我暫時使messageSource工作的方式。 我更改了Controller類,以將'messageSource'傳遞給Message類,並且能夠檢索消息。 請查看下面的類定義,讓我知道您可能需要幫助的更多信息。 我非常感謝您提供的所有幫助。

2010年12月31日下午3點
由於無法通過注釋成功配置messageSource,因此嘗試通過servlet-context.xml配置messageSource注入。 我仍然將messageSource設置為null。 如果您需要更多具體信息,請告訴我,我們會提供。 感謝您的幫助。

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仍然為null。

為什么在xxxController類中,Spring不能在另外兩個類中初始化messageSource,但它可以正確初始化?

@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關於它們的信息。

您的第一個類正確進行自動連線的原因是因為該類已正確標注為@Controller

嘗試優化您的基本包裝。 不要讓spring掃描所有的應用程序類com.mycompany.myapp(您將有一個更好的啟動時間)。

假設您的控制器類包含在com.mycompany.myapp.controller包中,服務類(以@ component,@ repository注釋)位於com.mycompany.myapp.services包中

將此添加到您的[servlet-name] -servlet.xml(調度程序上下文文件)中

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

另一方面,打開您的servlet-context.xml(假定是由contextloaderlistener加載的應用程序上下文)並添加以下內容:

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

然后像您一樣聲明messageSource。

您將在自動連線的每個位置注入一個messageSource。 它應該起作用,因為在Dispatcher-context-file中定義的內容可以看到在application-context文件中聲明的其他bean,但不是相反

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM