[JBoss JIRA] (CDI-414) Support for "self" injection
by Mark Struberg (JIRA)
[ https://issues.jboss.org/browse/CDI-414?page=com.atlassian.jira.plugin.sy... ]
Mark Struberg commented on CDI-414:
-----------------------------------
[~pmuir] I also believe that 'self' injecting a @Dependent scoped bean is weird. This currently will most likely lead to an infinite loop I guess. This is also not really limited by the interceptor behaviour but by injection semantics.
But self injecting _normalscoped_ beans is perfectly fine and there should be no difference between injecting the bean into itself or any other bean.
> Support for "self" injection
> ----------------------------
>
> Key: CDI-414
> URL: https://issues.jboss.org/browse/CDI-414
> Project: CDI Specification Issues
> Issue Type: Bug
> Components: Resolution
> Reporter: arjan tijms
>
> 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.2.6#6264)
10 years, 3 months
[JBoss JIRA] (CDI-414) Support for "self" injection
by Mark Struberg (JIRA)
[ https://issues.jboss.org/browse/CDI-414?page=com.atlassian.jira.plugin.sy... ]
Mark Struberg commented on CDI-414:
-----------------------------------
~jharting I was refering to the sample to the original example which (for what I understood) does not work in Weld, but imo this must work according to the spec:
{code}
@ApplicationScoped
public class Foo {
@Inject
private Foo self;
public MyValue getValue() {
if (!cached) {
return self.loadValueFromDb();
}
return cachedValue;
}
@Transactional
public MyValue loadValueFromDb() {
return em.....;
}
}
{code}
Of course just calling this.xxx instead of self.xxx will NOT trigger the interceptor. This is what I already tried to clarify in the *edit* section of my last post.
> Support for "self" injection
> ----------------------------
>
> Key: CDI-414
> URL: https://issues.jboss.org/browse/CDI-414
> Project: CDI Specification Issues
> Issue Type: Bug
> Components: Resolution
> Reporter: arjan tijms
>
> 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.2.6#6264)
10 years, 3 months
[JBoss JIRA] (CDI-414) Support for "self" injection
by Pete Muir (JIRA)
[ https://issues.jboss.org/browse/CDI-414?page=com.atlassian.jira.plugin.sy... ]
Pete Muir commented on CDI-414:
-------------------------------
I'm not sure that injection of "self" dependent scoped beans is required to be supported by the spec. It's probably not a bad idea though.
I agree though that this must work on normal scoped beans as Mark describes.
> Support for "self" injection
> ----------------------------
>
> Key: CDI-414
> URL: https://issues.jboss.org/browse/CDI-414
> Project: CDI Specification Issues
> Issue Type: Bug
> Components: Resolution
> Reporter: arjan tijms
>
> 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.2.6#6264)
10 years, 3 months
[JBoss JIRA] (CDI-414) Support for "self" injection
by Jozef Hartinger (JIRA)
[ https://issues.jboss.org/browse/CDI-414?page=com.atlassian.jira.plugin.sy... ]
Jozef Hartinger edited comment on CDI-414 at 8/18/14 6:32 AM:
--------------------------------------------------------------
We are talking about invocations on {code}this{code} which *do not* support interception. These were reactions to Antoine's suggestion.
was (Author: jharting):
We are talking about *this* invocations which *do not* support interception. This was a reaction to Antoine's suggestion.
> Support for "self" injection
> ----------------------------
>
> Key: CDI-414
> URL: https://issues.jboss.org/browse/CDI-414
> Project: CDI Specification Issues
> Issue Type: Bug
> Components: Resolution
> Reporter: arjan tijms
>
> 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.2.6#6264)
10 years, 3 months
[JBoss JIRA] (CDI-414) Support for "self" injection
by Jozef Hartinger (JIRA)
[ https://issues.jboss.org/browse/CDI-414?page=com.atlassian.jira.plugin.sy... ]
Jozef Hartinger commented on CDI-414:
-------------------------------------
We are talking about *this* invocations which *do not* support interception. This was a reaction to Antoine's suggestion.
> Support for "self" injection
> ----------------------------
>
> Key: CDI-414
> URL: https://issues.jboss.org/browse/CDI-414
> Project: CDI Specification Issues
> Issue Type: Bug
> Components: Resolution
> Reporter: arjan tijms
>
> 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.2.6#6264)
10 years, 3 months
[JBoss JIRA] (CDI-414) Support for "self" injection
by Mark Struberg (JIRA)
[ https://issues.jboss.org/browse/CDI-414?page=com.atlassian.jira.plugin.sy... ]
Mark Struberg edited comment on CDI-414 at 8/17/14 1:17 PM:
------------------------------------------------------------
folks, this sounds absolutely like a bug in current Weld.
The spec clearly defines that invocation of 'business methods' must get intercepted. We had this discussion a long time ago...
See 7.2
{quote}
When the application invokes:
• a method of a bean via a contextual reference to the bean, ...
the invocation is treated as a business method invocation.
{quote}
And this also used to work in older Weld versions. Self injection is a classic pattern which is heavily used in the industry. E.g. if you have a CDI bean which usually returns values from a cache and only on a cache miss it invokes a @Transactional method to fill the cache. If you now tell me that Weld just ignores this @Transactional interceptor then this is *really* bad news. Again, this used to work in older Weld versions...
There is absolutely nothing which we need to change in the spec as this IS already specified.
*Edit:*
To make this clear what I' refering to:
* calling a method on a self-injected bean (like in the example from Arjan) _must_ trigger the interceptor. If this doesn't work in Weld, then this is clearly a bug
* calling just another method from the same class (without invoking it via the self-injected Contextual Reference must _not_ trigger the interceptor. This was intended that way.
was (Author: struberg):
folks, this sounds absolutely like a bug in current Weld.
The spec clearly defines that invocation of 'business methods' must get intercepted. We had this discussion a long time ago...
See 7.2
{quote}
When the application invokes:
• a method of a bean via a contextual reference to the bean, ...
the invocation is treated as a business method invocation.
{quote}
And this also used to work in older Weld versions. Self injection is a classic pattern which is heavily used in the industry. E.g. if you have a CDI bean which usually returns values from a cache and only on a cache miss it invokes a @Transactional method to fill the cache. If you now tell me that Weld just ignores this @Transactional interceptor then this is *really* bad news. Again, this used to work in older Weld versions...
There is absolutely nothing which we need to change in the spec as this IS already specified.
> Support for "self" injection
> ----------------------------
>
> Key: CDI-414
> URL: https://issues.jboss.org/browse/CDI-414
> Project: CDI Specification Issues
> Issue Type: Bug
> Components: Resolution
> Reporter: arjan tijms
>
> 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.2.6#6264)
10 years, 4 months
[JBoss JIRA] (CDI-414) Support for "self" injection
by Mark Struberg (JIRA)
[ https://issues.jboss.org/browse/CDI-414?page=com.atlassian.jira.plugin.sy... ]
Mark Struberg commented on CDI-414:
-----------------------------------
folks, this sounds absolutely like a bug in current Weld.
The spec clearly defines that invocation of 'business methods' must get intercepted. We had this discussion a long time ago...
See 7.2
{quote}
When the application invokes:
• a method of a bean via a contextual reference to the bean, ...
the invocation is treated as a business method invocation.
{quote}
And this also used to work in older Weld versions. Self injection is a classic pattern which is heavily used in the industry. E.g. if you have a CDI bean which usually returns values from a cache and only on a cache miss it invokes a @Transactional method to fill the cache. If you now tell me that Weld just ignores this @Transactional interceptor then this is *really* bad news. Again, this used to work in older Weld versions...
There is absolutely nothing which we need to change in the spec as this IS already specified.
> Support for "self" injection
> ----------------------------
>
> Key: CDI-414
> URL: https://issues.jboss.org/browse/CDI-414
> Project: CDI Specification Issues
> Issue Type: Bug
> Components: Resolution
> Reporter: arjan tijms
>
> 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.2.6#6264)
10 years, 4 months
[JBoss JIRA] (CDI-452) Specify that web scoped (request, session, application) beans are injectable in async servlets
by Ed Burns (JIRA)
Ed Burns created CDI-452:
----------------------------
Summary: Specify that web scoped (request, session, application) beans are injectable in async servlets
Key: CDI-452
URL: https://issues.jboss.org/browse/CDI-452
Project: CDI Specification Issues
Issue Type: Feature Request
Components: Java EE integration
Affects Versions: 1.0
Reporter: Ed Burns
Priority: Trivial
Consider this code based on this blog post: < https://weblogs.java.net/blog/swchan2/archive/2013/06/06/asynchronous-ser... >.
{code}
@WebServlet(urlPatterns="/test2", asyncSupported=true)
public class TestAsyncMESServlet extends HttpServlet {
@Resource
private ManagedExecutorService managedExecutorService;
@Inject
MyRunnableImpl myRunnableImpl;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
final AsyncContext asyncContext = req.startAsync();
final PrintWriter writer = res.getWriter();
managedExecutorService.submit(myRunnableImpl);
}
public static class MyRunnableImpl implements Runnable {
@Inject
Bean bean; // Bean is @RequestScoped
@Override
public void run() {
writer.println("Done");
asyncContext.complete();
}
}
}
{code}
According to Jozef Hartzinger, this currently does not work, because only @Dependent and @ApplicationScoped beans are propagated to the new thread. To keep CDI relevant in light of the reactive programming movement and the popularity of node.js, we need to make this work.
--
This message was sent by Atlassian JIRA
(v6.2.6#6264)
10 years, 4 months
[JBoss JIRA] (CDI-414) Support for "self" injection
by Antonin Stefanutti (JIRA)
[ https://issues.jboss.org/browse/CDI-414?page=com.atlassian.jira.plugin.sy... ]
Antonin Stefanutti commented on CDI-414:
----------------------------------------
@[~mkouba], @[~jharting], thanks. Indeed, I agree that, from the CDI specification, self-invocation should not be supported. That being said, from the end-user perspective, the need could be considered legitimate IMO. Though, I understand that the question of self-invocation interception may be more general, that consistency concern with EJB spec for example should be taken into account, and that restrictions on the proxying mechanism chosen by the implementations may not be acceptable.
> Support for "self" injection
> ----------------------------
>
> Key: CDI-414
> URL: https://issues.jboss.org/browse/CDI-414
> Project: CDI Specification Issues
> Issue Type: Bug
> Components: Resolution
> Reporter: arjan tijms
>
> 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.2.6#6264)
10 years, 4 months