使用 Spel 创建 Bean休眠
我们正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会尝试不同的方法:
appManager
并设置maxUploadSize
已解析并注入所有依赖项例如如下所示:
最大上传大小仍将仅在多部分解析器上设置一次 - 在应用程序上下文初始化期间。如果数据库中的值发生更改,则需要重新启动应用程序才能为新值重新配置解析器。
考虑一下您是否不需要像这样重写
CommonsFileUploadSupport#prepareFileUpload()
:I'd try a differnt approach:
org.springframework.web.multipart.commons.CommonsMultipartResolver
org.springframework.beans.factory.InitializingBean
or use@PostConstruct
to write a method that will call theappManager
and set themaxUploadSize
in beans initialization phase, after the configuration file was parsed and all dependencies were injectedFor example like this:
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:根据您的情况,还有另一个有用的选项。您可以扩展
PropertiesFactoryBean
或PropertyPlaceholderConfigurer
并从数据库中获取一些属性。There is another option that can be useful depending on your case. You can extend
PropertiesFactoryBean
orPropertyPlaceholderConfigurer
and obtain some of the properties from your database.