Does Java have a using statement?

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.

96442 次浏览

Java7引入了 自动资源块管理,它为 Java 平台带来了这个特性。Java 的早期版本没有任何类似于 using的东西。

例如,您可以按以下方式使用任何实现 java.lang.AutoCloseable的变量:

try(ClassImplementingAutoCloseable obj = new ClassImplementingAutoCloseable())
{
...
}

Java's java.io.Closeable interface, implemented by streams, automagically extends AutoCloseable, so you can already use streams in a try block the same way you would use them in a C# using block. This is equivalent to C#'s using.

作为 version 5.0, Hibernate Sessions implement AutoCloseable和可以在 ARM 块自动关闭。在早期版本的 Hibernate 会话没有实现 AutoCloseable中。因此,要使用这个特性,您需要使用 Hibernate > = 5.0。

不,Java 没有等效的 using语句。

技术上来说:

DisposableObject d = null;
try {
d = new DisposableObject();
}
finally {
if (d != null) {
d.Dispose();
}
}

目前为止,没有。

然而,有一个针对 Java7的 手臂提案。

在 Java7 之前,Java 中就有 没有这样的特性(对于 Java7以及更高的版本,请参阅关于 手臂亚萨的回答)。

你需要手动操作,它 很痛苦:

AwesomeClass hooray = null;
try {
hooray = new AwesomeClass();
// Great code
} finally {
if (hooray!=null) {
hooray.close();
}
}

这只是当 // Great codehooray.close()都不能抛出任何异常时的代码。

如果 really只想限制变量的作用域,那么一个简单的代码块就可以完成这项工作:

{
AwesomeClass hooray = new AwesomeClass();
// Great code
}

但你可能不是这个意思。

最接近的 Java 等价物是

AwesomeClass hooray = new AwesomeClass();
try{
// Great code
} finally {
hooray.dispose(); // or .close(), etc.
}

请看这个 Java 关键字列表

  1. 遗憾的是,using关键字不在列表中。
  2. 而且 C # using关键字通过任何其他关键字也没有等价性,就像现在在 Java 中一样。

要模仿这样的 "using"行为,您必须使用一个 try...catch...finally块,在这里您将释放 finally中的资源。

来自 硬币计划的 ARM 块 将在 Java7中。这个特性的目的是为 Java 带来类似于。使用语法的网络。

如果您对资源管理感兴趣,龙目岛计划提供了 @Cleanup注释:

可以使用 @Cleanup来确保给定的 资源被自动清理 在代码执行路径退出之前 你目前的范围。你做到这一点 注释任何局部变量 与 @Cleanup的声明 注释如下:

@Cleanup InputStream in = new FileInputStream("some/file");

作为一个 结果,在作用域的末端,您是 在,in.close()调用。这个调用是 保证通过 Try/finally 构造 下面的例子看看这是如何工作的。

如果您想要的对象类型 清理没有 close() method, but some other no-argument 方法时,可以指定 这种方法是这样的:

@Cleanup("dispose") org.eclipse.swt.widgets.CoolBar bar = new CoolBar(parent, 0);

默认情况下,清理方法假定为 close(). A cleanup method that takes 论点不能被称为通过 @Cleanup.

香草咖啡

import java.io.*;


public class CleanupExample {
public static void main(String[] args) throws IOException {
InputStream in = new FileInputStream(args[0]);
try {
OutputStream out = new FileOutputStream(args[1]);
try {
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
} finally {
out.close();
}
} finally {
in.close();
}
}
}

和龙目岛一起

import lombok.Cleanup;
import java.io.*;


public class CleanupExample {
public static void main(String[] args) throws IOException {
@Cleanup InputStream in = new FileInputStream(args[0]);
@Cleanup OutputStream out = new FileOutputStream(args[1]);
byte[] b = new byte[10000];
while (true) {
int r = in.read(b);
if (r == -1) break;
out.write(b, 0, r);
}
}
}

Since Java 7 it does: http://blogs.oracle.com/darcy/entry/project_coin_updated_arm_spec

问题中代码的语法是:

try (Session session = new Session())
{
// do stuff
}

Note that Session needs to implement AutoClosable or one of its (many) sub-interfaces.

回答有关限制变量作用域的问题,而不是讨论自动关闭/处置变量。

在 Java 中,你可以使用花括号来定义封闭的匿名作用域,这非常简单。

{
AwesomeClass hooray = new AwesomeClass()
// Great code
}

变量 hooray只能在此范围内使用,而不能在其外部使用。

如果有重复的变量,而这些变量只是临时的,那么这将非常有用。

例如,每个都带有索引。就像 item变量在 for 循环上是关闭的(即,只在其中可用) ,index变量在匿名作用域上也是关闭的。

// first loop
{
Integer index = -1;
for (Object item : things) {index += 1;
// ... item, index
}
}


// second loop
{
Integer index = -1;
for (Object item : stuff) {index += 1;
// ... item, index
}
}

如果您没有 for 循环来提供变量作用域,但是希望使用通用变量名,那么我有时也会使用这种方法。

{
User user = new User();
user.setId(0);
user.setName("Andy Green");
user.setEmail("andygreen@gmail.com");
users.add(user);
}


{
User user = new User();
user.setId(1);
user.setName("Rachel Blue");
user.setEmail("rachelblue@gmail.com");
users.add(user);
}

在 java 8中你可以使用 try。请参考下面的页面