框架:Spring 3。
我真的无法理解为什么bean中的消息源injectend最终总是为NULL。
以下是片段:
servlet.xml
<context:annotation-config />
<context:component-scan base-package="com.myproject.controllers" />
<mvc:annotation-driven />
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="/WEB-INF/messages/messages" />
<property name="cacheSeconds" value="0" />
</bean>
注入messageSource的类
import com.myproject.controllers.forms.RegistrationForm;
@Component
public class RegistrationFormValidator implements Validator {
@Autowired
@Qualifier("messageSource")
private MessageSource messageSource;
//other stuff here...
}
这是控制器
@Controller
@SessionAttributes("userSearchForm")
public class UsersController extends PaginationController<ProfiledUser>{
@InitBinder(value="registrationForm")
public void initBinder(WebDataBinder binder)
{
binder.setValidator(new RegistrationFormValidator());
}
我已经尝试了以下内容:
ReloadableresourceBundleMessageSource
而不是使用接口MessageSource
答案 0 :(得分:1)
我认为您正在检查CGLIB类的字段值
见spring singleton bean fields are not populated
击>
更新
@Autowired
注释由AutowiredAnnotationBeanPostProcessor
处理,可以通过在相应的spring配置文件中指定<context:annotation-config />
注释来注册(bean后处理器基于每个容器工作,因此您需要具有不同的后处理器servlet和应用程序根上下文=&gt;您需要将<context:annotation-config />
放到Web应用程序上下文和调度程序servlet配置中。
请注意,@Autowired
注释具有required
属性,默认设置为true,这意味着如果自动装配过程发生,Spring将检查是否存在指定bean的一个实例。如果那些用@Autowired
注释的字段的bean是单例,那么将在应用程序初始化期间执行检查。
更新在这个特定问题中,Validator
实例根本不是由Spring创建的,这就是为什么没有执行自动装配并且没有抛出初始化异常的原因。
答案 1 :(得分:0)
将basename更改为以下内容:
<property name="basename" value="classpath:messages/messages" />
答案 2 :(得分:0)
当我尝试在自定义日期格式化程序中使用MessageSource时,我遇到了类似的问题。
代码AppDateFormatter
public class AppDateFormatter implements Formatter<Date> {
@Autowired
private MessageSource messageSource;
other stuff.....
private SimpleDateFormat createDateFormat(final Locale locale) {
final String format = this.messageSource.getMessage("date.format", null, locale);
final SimpleDateFormat dateFormat = new SimpleDateFormat(format);
dateFormat.setLenient(false);
return dateFormat;
}
}
这对我有用:
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
other stuff.....
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("Messages/Messages", "Messages/Labels");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(1);
return messageSource;
}
@Bean
public AppDateFormatter appDateFormatter(){
return new AppDateFormatter();
}
@Bean
public FormattingConversionService mvcConversionService() {
FormattingConversionService conversionService = new DefaultFormattingConversionService();
conversionService.addFormatter(appDateFormatter());
return conversionService;
}
}
答案 3 :(得分:0)
在java configs类中添加以下bean。
$("a[data-spinner='true']").click(function() {
$("a[data-spinner='true']").attr("data-spinner",false);
alert("clicked");
});
然后可以按如下方式注入@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasename("messages");
return messageSource;
}
@Bean
public LocaleResolver localeResolver() {
SessionLocaleResolver resolver = new SessionLocaleResolver();
resolver.setDefaultLocale(Locale.ENGLISH);
return resolver;
}
@Bean
public MessageSourceAccessor messageSourceAccessor(MessageSource messageSource){
return new MessageSourceAccessor(messageSource, Locale.ENGLISH );
}
bean
MessageSourceAccessor
获取消息字符串,如下所示,
@Autowired
private MessageSourceAccessor msgs;
msgs.getMessage("controller.admin.save.success")
文件应位于message_en.properties
文件夹中。为其他语言添加必要的属性文件。
希望这有帮助。