[weld-dev] spec question on 3.5 Resource Beans

Gavin King gavin.king at gmail.com
Wed Feb 17 19:12:24 EST 2010


On Wed, Feb 17, 2010 at 5:55 PM, Mark Struberg <struberg at yahoo.de> wrote:
>> JSR-299 does not define the behavior
>> of @PersistenceContext,
>> @Resource, etc for non-static fields.
>
> it does. 7.3.6 strongly indicates that a CDI managed bean should react the same like an EE managed bean in that respect:
>
> 7.3.6. Lifecycle of resources
> The container must perform ordinary Java EE component environment injection upon any non-static field that functions as
> a resource declaration, as defined by the Java EE platform and Common Annotations for the Java platform specifications.
> The container is not required to perform Java EE component environment injection upon a static field.

This section talks about fields which are resource declarations. i.e.
fields annotated @Produces. It says that just because the field is a
resource declaration, doesn't mean that the container is relieved of
its responsibilities to do normal EE environment injection as defined
by the EE spec. It is absolutely *not* defining what the container's
responsibilities in that respect are. They is defined by the EE spec.
It also isn't saying anything about fields which are not annotated
@Produces.

> The problem is that this may contradict 3.5.1, which a few people did read like injecting e.g.
> @PersistenceContext into a CDI managed bean is _only_ applicable through the 'Resource
> bean' producer field way.

Except that 3.5.1 doesn't say anything like that. And therefore there
is no contradiction.

> So it really comes down to be one question. Should the following line work in a CDI managed bean, yes or no?
> private @PersistenceContext(unitName="myUnit") EntityManager myUnitEm;

I'm sure you can find the answer to this by reading the managed beans
and Java EE specs, which define this stuff.

Short answer: it depends upon whether CDI is executing in an EE
environment or not. If it is, then the EE spec requires that it does
work. If not, then all bets are off, since compliance with the EE spec
is no longer expected.

-- 
Gavin King
gavin.king at gmail.com
http://in.relation.to/Bloggers/Gavin
http://hibernate.org
http://seamframework.org


More information about the weld-dev mailing list