-
@Target(value=PARAMETER) @Retention(value=RUNTIME) @Documented public @interface Disposes
Identifies the disposed parameter of a disposer method. May be applied to a parameter of a method of a bean class.
public class UserDatabaseEntityManager { @Produces @ConversationScoped @UserDatabase public EntityManager create(EntityManagerFactory emf) { return emf.createEntityManager(); } public void close(@Disposes @UserDatabase EntityManager em) { em.close(); } }
public class Resources { @PersistenceContext @Produces @UserDatabase private EntityManager em; public void close(@Disposes @UserDatabase EntityManager em) { em.close(); } }
A disposer method allows the application to perform customized cleanup of an object returned by a producer method or producer field.
A disposer method must be a non-abstract method of a managed bean class or session bean class. A disposer method may be either static or non-static. If the bean is a session bean, the disposer method must be a business method of the EJB or a static method of the bean class.
A bean may declare multiple disposer methods.
Each disposer method must have exactly one disposed parameter, of the same type as the corresponding producer method or producer field return type. When searching for disposer methods for a producer method or producer field, the container considers the type and qualifiers of the disposed parameter. If a disposed parameter resolves to a producer method or producer field declared by the same bean class, the container must call this method when destroying any instance returned by that producer method or producer field.
In addition to the disposed parameter, a disposer method may declare additional parameters, which may also specify qualifiers. These additional parameters are injection points.
public void close(@Disposes @UserDatabase EntityManager em, Logger log) { ... }
A disposer method may resolve to multiple producer methods or producer fields declared by the bean class, in which case the container must call it when destroying any instance returned by any of these producer methods or producer fields.
Disposer methods are not inherited by bean subclasses.
Interceptors and decorators may not declare disposer methods.
- Author:
- Gavin King, Pete Muir
- See Also:
@Produces
Document created the 11/06/2005, last modified the 18/08/2025
Source of the printed document:https://www.gaudry.be/en/java-api-javaee-rf-javax/enterprise/inject/disposes.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.
References
These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author of this site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.