Java Reference
In-Depth Information
nally
block to perform this task. But, with the
try-with-resources
statement
introduced in Java SE 7, EJB 3.2 added the implementation of the
java.lang.AutoCloseable
interface in the
EJBContainer
class to free the de-
veloper from a task that could easily be forgotten and have negative repercussions
on the performance of a machine. Now, the embeddable
EJBContainer
will be
automatically closed at the end of a statement, provided that it is declared as a re-
sourceina
try-with-resources
statement. Thus, wenolongerneeda
finally
block like in the earlier example, which simplifies the code. The following example
demonstrateshowtotakeadvantageofthe
try-with-resources
statementwhile
testing EJB with an embeddable
EJBContainer
:
@Test
public void testAddition(){
//...
try(EJBContainer container =
javax.ejb.embeddable.EJBContainer.createEJBContainer(properties);)
{
//...
} catch (NamingException ex) {
Logger.getLogger(AppTest.class.getName()).log(Level.FINEST,
null, ex);
}
}
The final improvement of this specification concerns removal of the restriction on ob-
taining the current class loader when you want to access files or directories in the file
system from a bean.