ResourceContext#getResource and ResourceContext#initResource work differently


Following two codes work differently, I don’t know why.

return locateOperator(
        operatorId_,
        (operatorId, operator) -> resourceContext.initResource(
                new DevicesResource()
                .operatorId(operatorId)
                .operator(operator))
);
return locateOperator(
        operatorId_,
        (operatorId, operator) -> resourceContext.getResource(
                DevicesResource.class)
        .operatorId(operatorId)
        .operator(operator)
);

The DevicesResource class has a field.

@PersistenceContext
private EntityManger entityManager

getResource properly injects while initResource don’t.

getResource

The resolved resource instance is properly initialized in the context of the current request processing scope. The scope of the resolved resource instance depends on the managing container. For resources managed by JAX-RS container the default scope is per-request.

initResource

Initialize the resource or sub-resource instance. All JAX-RS injectable fields in the resource instance will be properly initialized in the context of the current request processing scope.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s