【问题标题】:@Autowired custom ReloadableResourceBundleMessageSource in @Controller@Controller 中的 @Autowired 自定义 ReloadableResourceBundleMessageSource
【发布时间】:2014-04-25 13:06:52
【问题描述】:

我已经为客户端构建了一个由 angularJs 和后端的 spring MVC 组成的应用程序。我的目的是将我的语言属性暴露给客户端,以便我可以在我的 JS 文件中使用它们。为此,我使用了库 angular-translate.min.js 并按照这些说明进行操作: (https://gist.github.com/rvillars/6422287)。

基本上这个想法是创建一个从 ReloadableResourceBundleMessageSource 扩展的自定义类,并覆盖 getAllProperties(Locale locale) 方法。然后,将该自定义类注入@controller,它将以 Json 格式向客户端返回属性列表。 到目前为止,我的问题是我无法在 @controller 中自动装配扩展 ReloadableResourceBundleMessageSource 的自定义类。我的应用程序在部署阶段总是因“没有符合条件的 bean of type CustomResourceBundleMessageSource”而崩溃。

我的环境: 春天MVC 3.2.3 角度 JS

请查找代码:

控制器类:

@Controller
@RequestMapping("/messageBundle")
public class SerializableMessageBundleController {

    @Autowired
    private CustomResourceBundleMessageSource messageSource;


    @RequestMapping(method = RequestMethod.GET)
    @ResponseBody
    public Properties list(@RequestParam String lang) {
        return messageSource.getAllProperties(new Locale(lang));
    }

自定义消息来源:

public class CustomResourceBundleMessageSource extends ReloadableResourceBundleMessageSource  {

     public Properties getAllProperties(Locale locale) {
            clearCacheIncludingAncestors();
            PropertiesHolder propertiesHolder = getMergedProperties(locale);
            Properties properties = propertiesHolder.getProperties();

            return properties;
        }

}
    }

ApplicationContext.xml:

<bean id="messageSource" class="com.contgo.CustomResourceBundleMessageSource">  
         <qualifier value="messageSource"/>
        <property name="basename" value="LanguageResources"/>  
</bean>

我也尝试添加 @qualifier("messageSource") 但不起作用。 也试试@Resource 和@Inject,也不管用。

有人做过吗?

【问题讨论】:

    标签: angularjs spring-mvc internationalization autowired


    【解决方案1】:

    我遇到了同样的问题,最后让它工作如下。 扩展 ReloadableResourceBundleMessageSource 的类是正确的。 您需要做的就是在您的 applicationContext 文件中正确配置它。

    您保留原来的 ReloadableResourceBundleMessageSource 配置:

    <bean class="org.springframework.context.support.ReloadableResourceBundleMessageSource" id="messageSource">  
            <property name="basename" value="LanguageResources"/>  
    </bean>
    

    然后在它下面添加以下内容,使用'parent'属性来引用原始配置:

    <bean class="com.contgo.CustomResourceBundleMessageSource" id="myCustomMessageSource" parent="messageSource"/>
    

    最后,在您的控制器中,您可以像这样自动装配它:

    @Autowired
    CustomResourceBundleMessageSource myCustomMessageSource;
    

    【讨论】:

      猜你喜欢
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 2017-06-25
      • 2022-01-21
      • 1970-01-01
      • 2022-07-22
      • 1970-01-01
      相关资源
      最近更新 更多