Spring匿名/回调类对数据持久性的影响 [英] Hard time with Spring anonymous/callback classes for data persistence

查看:55
本文介绍了Spring匿名/回调类对数据持久性的影响的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试适应Spring JDBC,但令我烦恼的是使用这些匿名类,在这些匿名类中,除非局部变量很容易安排,否则它们不能传递任何局部变量,但是如果我需要循环处理该怎么办呢?数组还是集合? 我无法将"FedModel fm"声明为最终的,因为它在循环中被重新初始化,但是我需要调用execute方法100次.这是我遇到问题的具体方案,原因是我不知道如何将BLOB插入数据库.

I'm trying to accommodate to Spring JDBC, but what bugs me is using these anonymous classes, where we cannot pass any local variables unless they are final which might be easy to arrange, but what about if I need to loop an array or collection ? I cannot declare "FedModel fm" to be final as it gets reinitialized in the loop, but I need to call the execute method 100 times. This is concrete scenario where I have the problem, cause I don't know any other way how to insert BLOB into a database.

 for (int i = 0; i < fedModels.size(); i++) {
        FedModel fm = fedModels.get(i);

        jdbcTemplate.execute("INSERT INTO items (name, video_blob) VALUES (?, ?)",
                  new AbstractLobCreatingPreparedStatementCallback(lobHandler) {
                  protected void setValues(PreparedStatement ps, LobCreator lobCreator) 
                        throws SQLException {
                            ps.setString(1, fm.getName());
                            ps.setString(2, fm.getDifficultyLevel());
                            ps.setString(3, fm.getEquipment());
                            lobCreator.setBlobAsBinaryStream(ps, paramIndex, contentStream, contentLength);                                                     
                          }
                });

} 

我唯一能想到的就是创建一个静态的嵌套类,该类扩展了AbstractLobCreatingPreparedStatementCallback并添加了fedModels的构造函数,以便我可以在其中进行循环.但是只使用JDBC会更容易.

The only thing I can think of, is creating a static Nested class that extends AbstractLobCreatingPreparedStatementCallback and adds constructor for fedModels so that I could do the loop inside. But it would be easier using only JDBC.

推荐答案

...但是让我烦恼的是使用这些匿名类,在这些类中,除非它们是最终的,否则我们不能传递任何局部变量...

... but what bugs me is using these anonymous classes, where we cannot pass any local variables unless they are final ...

这不是Spring的错.它是Java编程语言的属性.

This is not Spring's fault. It is property of the Java programming language.

基本上,Java语言(至少Java 6和更早版本)不支持闭包,这将允许匿名类实例访问和更新实例的封闭范围内的局部变量.解决方法是,该语言允许您访问包含final的局部变量.通过将各个变量的值复制到匿名类实例中,并将它们存储在隐藏变量中来实现(无闭包).

Basically, the Java language (at least Java 6 and earlier) does not support closures which would allow the anonymous class instance to access and update local variables in the instance's enclosing scopes. As a workaround, the language allows you to access enclosing local variables that are final. This is implemented (without closures) by copying the respective variable's values into the anonymous class instance, and storing them in hidden variables.

在您的特定情况下,简单的解决方案是将fm声明为final.如果需要访问匿名类中的循环变量,则可以声明最终副本;例如

In your particular case, the simple solution is to declare fm as final. If you needed to access the loop variable in the anonymous class, you could declare a final copy; e.g.

for (int i = 0; i < 10; i++) {
    final int ii = i;
    new Thread(new Runnable(){
        public void run() {
            System.err.println(ii);
        }
    }).start();
}

这篇关于Spring匿名/回调类对数据持久性的影响的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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