[
https://issues.jboss.org/browse/CDI-414?page=com.atlassian.jira.plugin.sy...
]
Antoine Sabot-Durand commented on CDI-414:
------------------------------------------
I relaunch discussion on this ticket since we are on the verge to introduce a new
annotation related to "proxy" or "business methods" exception with
CDI-527.
This annotation could be designed to support broader proxy related feature than just
CDI-527 and could provide a solution to extend proxy generation to self invocation.
Suppose the annotation to be like
{code}
@Target({TYPE})
@Retention(RUNTIME)
@Documented
public @interface AllowProxy {
boolean withFinalMethods() default true;
boolean onSelfInvocations() default false;
}
{code}
{{@AllowProxy}} alone could be used for CDI-527, but this annotation could be used for
this ticket.
The code provided by [~struberg] above would become
{code}
@ApplicationScoped
@AllowProxy(selfInvocation=true) //ask the container to create proxy on all invocation
public class CountryService {
private List<Country> cachedCountries;
public List<Counry> getCountries() {
if (cachedCountries == null) {
loadCountries(); // this call would be a business method call thanks to the
annotation
}
return cachedCountries;
}
// Transactional aspect will be activated for all calls including from method in the
same instance
@Transactional
protected synchronized loadCountries() {
if (countries != null) return;
countries = em…. load all countries form DB
}
}
{code}
As [~jharting] stated multiple times above, it is technically doable thanks to subclassing
proxy.
It's not done today because CDI was initially aligned on EJB regarding interception.
Today Interceptor spec is no more part EJB and refers to "business method"
concept (without defining it) for interception.
CDI has its own definition of "business method" in
http://docs.jboss.org/cdi/spec/1.2/cdi-spec.html#biz_method, so we could imagine adding
definition for business method with this new annotation and stay consistant with
interceptor spec.
Support for "self" injection or intercepted self
invocation
-----------------------------------------------------------
Key: CDI-414
URL:
https://issues.jboss.org/browse/CDI-414
Project: CDI Specification Issues
Issue Type: Bug
Components: Resolution
Reporter: arjan tijms
Fix For: 2.0 (discussion)
Many features of CDI and EJB work by means of a proxy that intercepts calls and adds
'aspects'. In Java it's however not possible to decorate the {{this}} pointer,
so methods called on the same bean instance from within a method in the bean do not get
their 'aspects' applied.
This is a well known limitation, but in EJB it's possible to work around this by
injecting a bean into itself. E.g.
{code}
@Stateless
public class Foo {
@EJB
private Foo self;
// ...
}
{code}
Also see
http://adam-bien.com/roller/abien/entry/how_to_self_invoke_ejb
Unfortunately using CDI and {{@Inject}} this doesn't work. Weld for instance fails
the deployment and logs:
{noformat}
WELD-001443 Pseudo scoped bean has circular dependencies.
{noformat}
See also:
http://adam-bien.com/roller/abien/entry/inject_vs_ejb
Although there are workarounds, it would be great if {{@Inject}} in combination with CDI
could support self injection as well.
With that projects migrating from {{@EJB}} to {{@Inject}} can do so more easily and the
capability can be convenient for new projects as well (e.g. calling two separate
{{@Transactional}} methods from a single method without being required to create a new
bean).
--
This message was sent by Atlassian JIRA
(v6.4.11#64026)