Java有using语句吗?
Java有没有可以在hibernate中打开会话时使用的using语句?
在 C# 中,它类似于:
using (var session = new Session())
{
}
因此对象超出范围并自动关闭。
Does Java have a using statement that can be used when opening a session in hibernate?
In C# it is something like:
using (var session = new Session())
{
}
So the object goes out of scope and closes automatically.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
Java 7 引入了自动资源块管理,将此功能引入了 Java 平台。 Java 的早期版本没有类似
using
的内容。例如,您可以通过以下方式使用实现
java.lang.AutoCloseable
的任何变量:Java 的
java.io.Closeable
接口,由流实现,自动扩展AutoCloseable
,因此您已经可以在try
块中使用流,就像在 C#using
块中使用流一样。这相当于 C# 的using
。从 版本 5.0 开始,Hibernate 会话实现
AutoCloseable
并且可以在ARM块中自动关闭。在以前版本的 Hibernate Session 中不实现AutoCloseable
。因此,您需要使用 Hibernate >= 5.0 才能使用此功能。Java 7 introduced Automatic Resource Block Management which brings this feature to the Java platform. Prior versions of Java didn't have anything resembling
using
.As an example, you can use any variable implementing
java.lang.AutoCloseable
in the following way:Java's
java.io.Closeable
interface, implemented by streams, automagically extendsAutoCloseable
, so you can already use streams in atry
block the same way you would use them in a C#using
block. This is equivalent to C#'susing
.As of version 5.0, Hibernate Sessions implement
AutoCloseable
and can be auto-closed in ARM blocks. In previous versions of Hibernate Session did not implementAutoCloseable
. So you'll need to be on Hibernate >= 5.0 in order to use this feature.在 Java 7 之前,Java 中没有这样的功能(对于 Java 7 及更高版本,请参阅 Asaph关于ARM的回答)。
您需要手动执行此操作,这很痛苦:
这就是当
// Great code
和hooray.close()
都不是时的代码可以抛出任何异常。如果您真的只想限制变量的范围,那么一个简单的代码块就可以完成这项工作:
但这可能不是您的意思。
Before Java 7, there was no such feature in Java (for Java 7 and up see Asaph's answer regarding ARM).
You needed to do it manually and it was a pain:
And that's just the code when neither
// Great code
norhooray.close()
can throw any exceptions.If you really only want to limit the scope of a variable, then a simple code block does the job:
But that's probably not what you meant.
从 Java 7 开始,它是: http://blogs.oracle.com/darcy/entry/project_coin_updated_arm_spec
问题中代码的语法为:
请注意,
Session
需要实现AutoClosable
或其(许多)子接口之一。Since Java 7 it does: http://blogs.oracle.com/darcy/entry/project_coin_updated_arm_spec
The syntax for the code in the question would be:
Note that
Session
needs to implementAutoClosable
or one of its (many) sub-interfaces.技术上:
Technically:
最接近的java等效项是
The closest java equivalent is
截至目前,没有。
然而,有一个 ARM 的提案Java 7.
As of now, no.
However there is a proposal of ARM for Java 7.
如果您对资源管理感兴趣,Project Lombok 提供了
@Cleanup
注释。直接取自他们的网站:Vanilla Java
使用 Lombok
If you're interested in resource management, Project Lombok offers the
@Cleanup
annotation. Taken directly from their site:Vanilla Java
With Lombok
不,Java 没有等效的
using
语句。No, Java has no
using
statement equivalent.在java 8中你可以使用try。请参阅下页。 http://docs.oracle.com/javase/tutorial/essential/exceptions /tryResourceClose.html
In java 8 you can use try. Please refer to following page. http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
请参阅此Java 关键字列表。
using
关键字不在列表中。using
关键字与任何其他关键字之间也不存在等价关系。要模仿这种“using”行为,您必须使用 try...catch...finally 块,您可以在其中处理
中的资源最后
。Please see this List of Java Keywords.
using
keyword is unfortunately not part of the list.using
keyword through any other keyword as for now in Java.To imitate such
"using"
behaviour, you will have to use atry...catch...finally
block, where you would dispose of the resources withinfinally
.ARM 块,来自 project coin 将出现在 Java 7 中。此功能旨在为 Java 带来与 . Net 使用语法。
ARM blocks, from project coin will be in Java 7. This is feature is intended to bring similar functionality to Java as the .Net using syntax.
回答有关限制变量范围的问题,而不是谈论自动关闭/处置变量。
在 Java 中,您可以使用大括号定义封闭的匿名作用域。这非常简单。
变量
hooray
仅在此范围内可用,而在其之外则不可用。如果您有临时的重复变量,这可能很有用。
例如,每个都有索引。就像
item
变量在 for 循环中封闭(即仅在循环内部可用)一样,index
变量在匿名作用域中封闭。如果您没有 for 循环来提供变量范围,但您想使用通用变量名称,有时我也会使用它。
To answer the question regarding limiting scope of a variable, instead of talking about automatically closing/disposing variables.
In Java you can define closed, anonymous scopes using curly brackets. It's extremely simple.
The variable
hooray
is only available in this scope, and not outside it.This can be useful if you have repeating variables which are only temporary.
For example, each with index. Just like the
item
variable is closed over the for loop (i.e., is only available inside it), theindex
variable is closed over the anonymous scope.I also use this sometimes if you don't have a for loop to provide variable scope, but you want to use generic variable names.