使用 Spel 创建 Bean休眠

发布于 2024-11-02 15:23:59 字数 862 浏览 7 评论 0原文

我们正在使用 Spring MVC + 其内置的上传文件支持。我想利用 SpEL 设置最大上传大小。问题是这个值来自我们的数据库。因此,在我们旧的应用程序代码中,一旦上传文件,我们就会执行以下检查:

appManager.getAppConfiguration().getMaximumAllowedAttachmentSize();

然后检查文件以查看它是否大于此值,并根据大小继续操作。

我想在 servlet 配置中用以下调用替换该代码,如下所示:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver>
    <property name="maxUploadSize" value="#{appManager.getAppConfiguration().getMaximumAllowedAttachmentSize()}" />
</bean>

问题是在初始化时我收到以下异常:

Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.hibernate.LazyInitializationException: could not initialize proxy - no Session

有什么方法可以实现此目的吗?

We are using Spring MVC + its built in support for uploading files. I want to set the maximum upload size utilizing SpEL. The problem is this value comes from our database. So in our old application code, we do a check once we have the file uploaded with the following:

appManager.getAppConfiguration().getMaximumAllowedAttachmentSize();

We then check the file to see if it is larger than this, and proceed based upon the size.

I'd like to replace that code with the following call in our servlet configuration like such:

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver>
    <property name="maxUploadSize" value="#{appManager.getAppConfiguration().getMaximumAllowedAttachmentSize()}" />
</bean>

The problem is that upon initialization I receive the following exception:

Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.hibernate.LazyInitializationException: could not initialize proxy - no Session

Is there any way to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼波传意 2024-11-09 15:23:59

我会尝试不同的方法:

  1. 扩展 org.springframework.web.multipart.commons.CommonsMultipartResolver
  2. 添加 org.springframework.beans.factory.InitializingBean 或使用@PostConstruct 编写一个方法,在配置文件完成后,在 beans 初始化阶段调用 appManager 并设置 maxUploadSize已解析并注入所有依赖项

例如如下所示:

public class MyMultipartResolver extends CommonsMultipartResolver {

    @Autowired
    private AppManager appManager;

    @PostConstruct
    public void init() {
        setMaxUploadSize(
            appManager.getAppConfiguration().getMaximumAllowedAttachmentSize());
    }
}

最大上传大小仍将仅在多部分解析器上设置一次 - 在应用程序上下文初始化期间。如果数据库中的值发生更改,则需要重新启动应用程序才能为新值重新配置解析器。

考虑一下您是否不需要像这样重写 CommonsFileUploadSupport#prepareFileUpload()

public class MyMultipartResolver extends CommonsMultipartResolver {

    @Autowired
    private AppManager appManager;

    @Override
    protected FileUpload prepareFileUpload(String encoding) {
        FileUpload fileUpload = super.prepareFileUpload(encoding);
        fileUpload.setSizeMax(
            appManager.getAppConfiguration().getMaximumAllowedAttachmentSize());
        return fileUpload;
    }
}

I'd try a differnt approach:

  1. extend the org.springframework.web.multipart.commons.CommonsMultipartResolver
  2. add org.springframework.beans.factory.InitializingBean or use @PostConstruct to write a method that will call the appManager and set the maxUploadSize in beans initialization phase, after the configuration file was parsed and all dependencies were injected

For example like this:

public class MyMultipartResolver extends CommonsMultipartResolver {

    @Autowired
    private AppManager appManager;

    @PostConstruct
    public void init() {
        setMaxUploadSize(
            appManager.getAppConfiguration().getMaximumAllowedAttachmentSize());
    }
}

Still the maximim upload size will be set on the multipart resolver only once - during the application context initialization. If the value in database changes it will require an application restart to reconfigure the resolver for the new value.

Consider if You don't need to override the CommonsFileUploadSupport#prepareFileUpload() like this instead:

public class MyMultipartResolver extends CommonsMultipartResolver {

    @Autowired
    private AppManager appManager;

    @Override
    protected FileUpload prepareFileUpload(String encoding) {
        FileUpload fileUpload = super.prepareFileUpload(encoding);
        fileUpload.setSizeMax(
            appManager.getAppConfiguration().getMaximumAllowedAttachmentSize());
        return fileUpload;
    }
}
故人的歌 2024-11-09 15:23:59

根据您的情况,还有另一个有用的选项。您可以扩展 PropertiesFactoryBeanPropertyPlaceholderConfigurer 并从数据库中获取一些属性。

There is another option that can be useful depending on your case. You can extend PropertiesFactoryBean or PropertyPlaceholderConfigurer and obtain some of the properties from your database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文