使用Spel + Hibernate创建Bean [英] Bean creation using Spel + hibernate

查看:151
本文介绍了使用Spel + Hibernate创建Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Spring MVC +,它支持上传文件。我想使用SpEL来设置最大的上传大小。问题是这个值来自我们的数据库。所以在我们旧的应用程序代码中,我们做了一个检查,一旦我们有文件上传与以下内容:
$ b $ pre $ app $ c $ appManager.getAppConfiguration() .getMaximumAllowedAttachmentSize();

然后检查文件是否大于此值,然后根据大小继续。



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



<$ p $ < code>< bean id =multipartResolverclass =org.springframework.web.multipart.commons.CommonsMultipartResolver>
< property name =maxUploadSizevalue =#{appManager。 getAppleContainer()。getMaximumAllowedAttachmentSize()}/>

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

  bean的初始化失败;嵌套的异常是org.springframework.beans。 factory.BeanExpressionException:表达式解析失败;嵌套异常是org.hibernate.LazyInitializationException:无法初始化代理 - 无会话



有什么办法可以达到这个目的?

解我想尝试一个不同的方法:
$ b $ ol
  • 扩展 org.springframework .web.multipart.commons.CommonsMultipartResolver

  • 添加 org.springframework.beans.factory.InitializingBean 或使用 @PostConstruct 编写一个调用 appManager 并设置 maxUploadSize 在bean初始化阶段,在分析配置文件并注入了所有的依赖关系之后

    例如像这样: / p>

      public class MyMultipartResolver extends CommonsMultipartResolver {
    $ b $ @Autowired $ b $ private AppManager appManager;

    @PostConstruct $ b $ public void init(){
    setMaxUploadSize(
    appManager.getAppConfiguration()。getMaximumAllowedAttachmentSize());






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



    考虑如果您不需要重写 CommonsFileUploadSupport#prepareFileUpload()就像这样:

      public class MyMultipartResolver extends CommonsMultipartResolver {

    @Autowired
    私人AppManager appManager;

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


    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:

    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;
        }
    }
    

    这篇关于使用Spel + Hibernate创建Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆