users@jpa-spec.java.net

[jpa-spec users] Re: JPA_SPEC-109: Allow AttributeConverters to be CDI injectable

From: arjan tijms <arjan.tijms_at_gmail.com>
Date: Wed, 26 Apr 2017 22:20:58 +0200

Hi,

Big +1 for this. After repeating annotations this is probably the lowest
hangest fruit ;)

*>An attribute converter is a non-contextual object. In supporting
injection into attribute converter, the persistence*
*provider must behave as if it carries out the following steps involving
the use of the CDI SPI.*

In JSF 2.3 (referenced from the issue), we bit the bullet and went a step
further; the entire converter is a CDI managed bean and the runtime only
fetches it from CDI, without putting any demands on the JSF runtime
(everything is managed by CDI). Backwards compatibility is ensured by
treating the existing @Converter annotation as a qualifier and introducing
a new attribute for it that we use to select on.

If such bean is not returned by CDI, we proceed in the old way.

For an example in code, see the RI implementation:

https://github.com/jsf-spec/mojarra/blob/master/impl/src/main/java/com/sun/faces/cdi/CdiUtils.java#L113

By doing it this way, you get all additional CDI features such as
interceptors, decorators, bean enablement (alternatives, priority, etc) for
free as well.

Other than that, I spotted a number of typos in the text, mostly to do with
articles. E.g:

> If the CDI is not enabled, the persistence provider must not invoke
attribute converter
that depend upon CDI injection.

If CDI is not enabled, the persistence provider must not invoke an
attribute converter
that depends upon CDI injection.

Hope this helps ;)

Kind regards,
Arjan Tijms


On Wed, Apr 26, 2017 at 8:23 PM, Lukas Jungmann <lukas.jungmann_at_oracle.com>
wrote:

> Hi,
>
> In JPA 2.1, injection support has been added to instances of
> EntityListeners. Another candidates for injection support are instances of
> AttributeConverter[1] interface. I'd like to propose adding following text
> to the section 3.8 Type Conversion of Basic Attributes. The text itself is
> almost identical to the one in section 3.5.1 Entity Listeners covering
> injection handling in listeners in the JPA 2.1 spec (with s/Entity
> listener/Attribute converter/g applied).
>
> *3.8 **Type Conversion of Basic Attributes*
>
> ...
> *Attribute converter classes in Java EE environments support dependency
> injection through the Contexts and*
> *Dependency Injection API (CDI) when CDI is enabled. An attribute
> converter class that makes use*
> *of CDI injection may also define life-cycle callback methods annotated
> with the PostConstruct and*
> *PreDestroy annotations. These methods will be invoked after injection has
> taken place and before*
> *the attribute converter instance is destroyed respectively.*
>
> *The persistence provider is responsible for using the CDI SPI to create
> instances of the attribute converter*
> *class; to perform injection upon such instances; to invoke their
> PostConstruct and PreDestroy*
> *methods, if any; and to dispose of the attribute converter instances.*
>
> *The persistence provider is only required to support CDI injection into
> attribute converter in Java EE container*
> *environments. If the CDI is not enabled, the persistence provider must
> not invoke attribute converter*
> *that depend upon CDI injection.*
>
> *An attribute converter is a non-contextual object. In supporting
> injection into attribute converter, the persistence*
> *provider must behave as if it carries out the following steps involving
> the use of the CDI SPI.*
> *• Obtain a BeanManager instance.*
> *• Create an AnnotatedType instance for the attribute converter class.*
> *• Create an InjectionTarget instance for the annotated type.*
> *• Create a CreationalContext.*
> *• Instantiate the converter by calling the InjectionTarget produce
> method.*
> *• Inject the converter instance by calling the InjectionTarget inject
> method on the*
> *instance.*
> *• Invoke the PostConstruct callback, if any, by calling the
> InjectionTarget postConstruct*
> *method on the instance.*
>
> *When the converter instance is to be destroyed, the persistence provider
> must behave as if it carries out the*
> *following steps.*
> *• Call the InjectionTarget preDestroy method on the instance.*
> *• Call the InjectionTarget dispose method on the instance*
> *• Call the CreationalContext release method.*
>
> *Persistence providers may optimize the steps above, e.g. by avoiding
> calls to the actual CDI SPI and*
> *relying on container-specific interfaces instead, as long as the outcome
> is the same.*
>
> *Attribute converters that do not make use of CDI injection are stateless.
> The life-cycle of such attribute converters*
> *is unspecified.*
>
> Do you think this is fine, something should be corrected or is there a
> reason why this should not be added to JPA 2.2 MR?
>
> Thank you,
> --lukas
>
> [1]: https://java.net/jira/browse/JPA_SPEC-109
>