ResourceBundle not found for MessageSource when placed inside a folder

后端 未结 13 1294
星月不相逢
星月不相逢 2020-12-08 17:11

I am trying to use resource bundles with Spring\'s Message Source. Here is the way I am doing it:

@Component
public class MessageResolver implements MessageS         


        
13条回答
  •  误落风尘
    2020-12-08 17:51

    It's nearly 2015 now and I'm using Spring 4.1.2.RELEASE and there's definitely a problem with the way the messageSource bean needs to be configured so it picks up the target resource bundle.

    1) If the messageSource bean is of type ReloadableResourceBundleMessageSource it won't work:

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.support.ReloadableResourceBundleMessageSource;
    
    @Configuration
    @ComponentScan(basePackages = { "com.intertech.service" })
    //@ImportResource({"classpath:spring/applicationContext-i18n.xml"})
    public class AppConfig {
    
      @Bean(name = "messageSource")
      public ReloadableResourceBundleMessageSource getMessageSource() {
          ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
          messageSource.setBasename("config/messages");
          messageSource.setDefaultEncoding("UTF-8");
          messageSource.setUseCodeAsDefaultMessage(true);
          return messageSource;
      }
    
    //  @Bean(name = "messageSource")
    //  public ResourceBundleMessageSource getMessageSource() {
    //      ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    //      messageSource.setBasename("config/messages");
    //      messageSource.setDefaultEncoding("UTF-8");
    //      messageSource.setUseCodeAsDefaultMessage(true);
    //      return messageSource;
    //  }
    }
    

    2) If the messageSource bean is of type ResourceBundleMessageSource it will work:

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.support.ResourceBundleMessageSource;
    
    @Configuration
    @ComponentScan(basePackages = { "com.intertech.service" })
    //@ImportResource({"classpath:spring/applicationContext-i18n.xml"})
    public class AppConfig {
    
    //  @Bean(name = "messageSource")
    //  public ReloadableResourceBundleMessageSource getMessageSource() {
    //      ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    //      messageSource.setBasename("config/messages");
    //      messageSource.setDefaultEncoding("UTF-8");
    //      messageSource.setUseCodeAsDefaultMessage(true);
    //      return messageSource;
    //  }
    
      @Bean(name = "messageSource")
      public ResourceBundleMessageSource getMessageSource() {
          ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
          messageSource.setBasename("config/messages");
          messageSource.setDefaultEncoding("UTF-8");
          messageSource.setUseCodeAsDefaultMessage(true);
          return messageSource;
      }
    }
    

    3) If you're using an XML configuration file combined with a configuration class - it will work (notice how the base bundle is configured in a class like qualification manner i.e. 'config.messages' not 'config/messages'): (applicationContext-i18n.xml)

    
    
    
        
    
        
        
    
    

    and:

    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.ImportResource;
    
    @Configuration
    @ComponentScan(basePackages = { "com.intertech.service" })
    @ImportResource({"classpath:spring/applicationContext-i18n.xml"})
    public class AppConfig {
    
    //  @Bean(name = "messageSource")
    //  public ReloadableResourceBundleMessageSource getMessageSource() {
    //      ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    //      messageSource.setBasename("config/messages");
    //      messageSource.setDefaultEncoding("UTF-8");
    //      messageSource.setUseCodeAsDefaultMessage(true);
    //      return messageSource;
    //  }
    
    //  @Bean(name = "messageSource")
    //  public ResourceBundleMessageSource getMessageSource() {
    //      ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    //      messageSource.setBasename("config/messages");
    //      messageSource.setDefaultEncoding("UTF-8");
    //      messageSource.setUseCodeAsDefaultMessage(true);
    //      return messageSource;
    //  }
    }
    

    4) Most importantly... if you're using a WebApplicationInitializer (no web.xml), you've got to register the configuration class that defines the 'messageSource' bean in the root context, not in the dispatcher servlet's context:

    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRegistration;
    
    import org.springframework.web.WebApplicationInitializer;
    import org.springframework.web.context.ContextLoaderListener;
    import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
    import org.springframework.web.servlet.DispatcherServlet;
    
    public class WebAppInitializer implements WebApplicationInitializer {
    
        @Override
        public void onStartup(ServletContext container) throws ServletException {
        // Create the 'root' Spring application context
        AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
        rootContext.register(AppConfig.class);
        // Manage the lifecycle of the root application context
        container.addListener(new ContextLoaderListener(rootContext));
        // Create the dispatcher servlet's Spring application context
        AnnotationConfigWebApplicationContext dispatcherServlet = new AnnotationConfigWebApplicationContext();
        dispatcherServlet.register(MvcConfig.class);
        // Register and map the dispatcher servlet
        ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(
                dispatcherServlet));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("*.htm");
        }
    }
    

提交回复
热议问题