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?
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: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.