Announcement Announcement Module
Collapse
No announcement yet.
getBean() returns DelegatingMessageSource instead of my MessageSource implementation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • getBean() returns DelegatingMessageSource instead of my MessageSource implementation

    Java 1.6
    Tomcat 6
    Spring 3.0.0

    I've implemented my own database driven MessageSource and everything is working perfectly, until I need to refresh it. I can't figure out why when I call the following from a servlet, I'm receiving the ClassCastException:

    java.lang.ClassCastException: org.springframework.context.support.DelegatingMess ageSource cannot be cast to com.soundstrue.i18n.DatabaseMessageSource


    Code:
    WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
    DatabaseMessageSource ms = (DatabaseMessageSource) context.getBean("messageSource");
    Here is my spring configuration:

    Code:
      <bean id="databaseMessageSource"
            lazy-init="false"
            init-method="loadLocalizedProperties"
            class="com.soundstrue.i18n.DatabaseMessageSource"
            p:reloadRequired="true"/>
    
      <bean id="messageSource"
            class="org.springframework.context.support.ReloadableResourceBundleMessageSource"
            p:parentMessageSource-ref="databaseMessageSource"/>
    And my DatabaseMessageSource class:

    Code:
    public class DatabaseMessageSource extends AbstractMessageSource {
    
        private Locale DEFAULT_LOCALE = Locale.US;
        private Map<String, Map<String, String>> localeToMessagesMap;
        private boolean reloadRequired;
    
        @Autowired
        private MessageResourceDao messageResourceDao;
    
        @Override
        protected MessageFormat resolveCode(String code, Locale locale) {
            if(reloadRequired) {
                loadLocalizedProperties();
            }
    
            if(locale == null) locale = DEFAULT_LOCALE;
            
            String msg = localeToMessagesMap.get(locale.getCountry()).get(code);
            return createMessageFormat(msg, locale);
        }
    
        public void loadLocalizedProperties() {
            localeToMessagesMap = messageResourceDao.findAllMessageResources();
            reloadRequired = Boolean.FALSE;
        }
    
        public void setReloadRequired(boolean reload) {
            this.reloadRequired = reload;
        }
    }
    Have I mis-configured my spring-config.xml file?

    Thanks for any help!

  • #2
    Hello BillyBacon,

    You should use RequestContextUtils to deal with WAC of the dispatcher servlet (spring-servlet.xml) as below.
    [CODE]
    WebApplicationContext context = RequestContextUtils.getWebApplicationContext(getSe rvletContext());
    DatabaseMessageSource ms = (DatabaseMessageSource) context.getBean("messageSource");
    [CODE]

    You should use WebApplicationContextUtils just when you want to deal with Root web application context that loaded by org.springframework.web.context.ContextLoaderListe ner.

    Hope it work.
    Thinh Vo

    Comment

    Working...
    X