messageSource未由spring自动连接
我正在尝试从邮件属性文件中获取邮件messageSource未由spring自动连接,spring,Spring,我正在尝试从邮件属性文件中获取邮件 <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="basenames"> <list> <value>mymessage</value> </list> </property> </bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>mymessage</value>
</list>
</property>
</bean>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
public class Test{
@Autowired
private MessageSource messageSource;
public MessageSource getMessageSource() {
return messageSource;
}
public void setMessageSource(MessageSource messageSource) {
this.messageSource = messageSource;
}
public void draw()
{
System.out.println(this.messageSource.getMessage("name",null,"default",null));`enter code here`
}}
我的消息
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.context.MessageSource;
公开课考试{
@自动连线
私有消息源;
public MessageSource getMessageSource(){
返回消息源;
}
public void setMessageSource(MessageSource MessageSource){
this.messageSource=messageSource;
}
公众抽签()
{
System.out.println(this.messageSource.getMessage(“name”,null,“default”,null));`在这里输入代码`
}}
因为我得到了空指针异常
但是,当我更改代码并在测试类中实现MessageSourceAware时,它就可以正常工作了。甚至在我删除@autowired注释和bean id定义本身之后 这里是我在dispatcher上下文“servlet context.xml”中的声明:
将类名从class=“org.springframework.context.support.ResourceBundleMessageSource
更改为class=“org.springframework.context.support.ReloadableResourceBundleMessageSource
为什么basename属性为空?但始终为默认值
<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<beans:property name="basename" value="classpath:messages" />
<beans:property name="defaultEncoding" value="UTF-8"/>
<beans:property name="fileEncodings" value="UTF-8"/>
</beans:bean>