我有一个自定义异常类,需要从属性文件中读取一些错误消息。但它似乎继续为空。我什至尝试添加 @Component 但仍然无法正常工作
@Component
public class FormValidationFailExceptionHolder {
@Autowired
private MessageSource messageSource;
...
public void someMethod(){
....
String message = messageSource.getMessage(errorid, msgargs, Locale.ENGLISH);
....
}
}
这是我的弹簧配置
....
<context:component-scan base-package="simptex.my" />
....
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="useCodeAsDefaultMessage" value="false" />
<property name="basenames">
<list>
<value>classpath:mgmtlabels</value>
<value>classpath:error</value>
<value>classpath:PWMResources</value>
</list>
</property>
<property name="defaultEncoding" value="UTF-8" />
</bean>
我什至尝试在 spring config xml 中显式声明 bean,但仍然返回 null
<bean id="formValidationFailException" class="simptex.my.core.exception.FormValidationFailException">
<property name="messageSource" ref="messageSource" />
</bean>
这是我如何调用 FormValidationFailException
public class Foo{
private HttpSession session;
....
public void fooMethod() {
session.setAttribute(CommonSessionKeys.FORM_VALIDATION_EXECEPTION_HOLDER, new FormValidationFailExceptionHolder("roledd", "E0001"));
}
}
@Controller
public class FooController {
@RequestMapping(value = "/xxxx")
public void fooMethodxxx() {
Foo foo = new Foo(session);
foo.fooMethod();
}
}