Java Spring正确调用属性文件
我有一个SpringMVC应用程序,我正在使用扩展AbstractPdfView的类呈现一些PDF。我有几个PDF,我认为创建一个helper类来放置一些公共功能是有意义的。然后我决定将任何输出文本添加到messages_en.properties文件中。如何从助手类访问此文件?现在我正在手动创建助手类的实例。看起来像这样:Java Spring正确调用属性文件,java,spring,properties,messages,Java,Spring,Properties,Messages,我有一个SpringMVC应用程序,我正在使用扩展AbstractPdfView的类呈现一些PDF。我有几个PDF,我认为创建一个helper类来放置一些公共功能是有意义的。然后我决定将任何输出文本添加到messages_en.properties文件中。如何从助手类访问此文件?现在我正在手动创建助手类的实例。看起来像这样: public class PdfEarningsRecordView extends AbstractPdfView { @Override pro
public class PdfEarningsRecordView extends AbstractPdfView {
@Override
protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer,
HttpServletRequest request, HttpServletResponse response) throws Exception {
HelperClass helper = new HelpderClass();
......
在手动创建HelperClass时,我觉得我没有正确使用Spring。任何提示都将不胜感激
谢谢你
AbstractPdfView
是ApplicationObject Support
的一个子类,它有一个有用的getMessageSourceAccessor()
方法,返回一个MessageSourceAccessor
,这是从框架获取消息的最简单方法。只需将其传递给助手类:
public class PdfEarningsRecordView extends AbstractPdfView {
@Override
protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception {
HelperClass helper = new HelperClass(getMessageSourceAccessor());
公共类PDFview扩展了AbstractPdfView{
@凌驾
受保护的void buildPdfDocument(映射模型、文档文档、PdfWriter编写器、HttpServletRequest请求、HttpServletResponse响应)引发异常{
HelperClass helper=新的HelperClass(getMessageSourceAccessor());
然后助手可以相应地使用它
请注意,为了使其正常工作,必须正确初始化
PdfEarningsRecordView
对象。Spring通常会通过调用其ApplicationObjectSupport.setApplicationContext()为您执行此操作
在启动过程中,但如果您自己实例化了一个PdfEarningsRecordView
,无论出于何种原因,您都必须自己调用该方法。太棒了!这正是我所需要的。非常感谢您
public class PdfEarningsRecordView extends AbstractPdfView {
@Override
protected void buildPdfDocument(Map<String, Object> model, Document document, PdfWriter writer, HttpServletRequest request, HttpServletResponse response) throws Exception {
HelperClass helper = new HelperClass(getMessageSourceAccessor());