<HTML><BODY>Hi Matej<br><br>Thank you for your comment. However, note, that I wrote foo = newFoo() but not foo = new Foo();<br>By other words foo is created in newFoo method, but not calling new operator.<br><br>Best regards, Alex<br><br><br><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;">
        Среда, 30 августа 2017, 12:45 +03:00 от Matej Novotny &lt;manovotn@redhat.com&gt;:<br>
        <br>
        <div id="">






















        












<div class="js-helper js-readmsg-msg">
        <style type="text/css"></style>
         <div>
                <base target="_self" href="https://e.mail.ru/">
                
            <div id="style_15040863160000000233_BODY">Hi Alex<br>
<br>
What you suggest will inevitably fail for the reasons we mentioned previously.<br>
Both approaches will try to resolve a bean of type SimpleFoo which is ambiguous.<br>
<br>
Furthermore, doing `foo = new Foo()` is like saying "goodbye CDI".<br>
If you do this, you control the lifecycle of such object, not CDI.<br>
Therefore, there will be no injection done into such object, neither will CDI be able to dispose of such object.<br>
<br>
Matej<br>
<br>
----- Original Message -----<br>
&gt; From: "Alex Sviridov" &lt;<a href="mailto:ooo_saturn7@mail.ru">ooo_saturn7@mail.ru</a>&gt;<br>
&gt; To: "weld-dev" &lt;<a href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a>&gt;<br>
&gt; Sent: Wednesday, August 30, 2017 11:10:27 AM<br>
&gt; Subject: Re: [weld-dev] How to make method injection when bean subclass is required in Weld?<br>
&gt; <br>
&gt; <br>
&gt; <br>
&gt; Hi Matej,<br>
&gt; <br>
&gt; I meant the following (in previous example I had a mistake - called Parent<br>
&gt; Child and Child Parent)<br>
&gt; , now without this mistake:<br>
&gt; 1)<br>
&gt; public class Parent {<br>
&gt; <br>
&gt; @Inject<br>
&gt; private Instance&lt;SimpleFoo&gt; fooInstance;<br>
&gt; <br>
&gt; private SimpleFoo foo;<br>
&gt; <br>
&gt; protected SimpleFoo newFoo() {<br>
&gt; return fooInstance.get();<br>
&gt; }<br>
&gt; <br>
&gt; @PostConstruct<br>
&gt; private void doPostConstruct() {<br>
&gt; foo = newFoo();<br>
&gt; }<br>
&gt; }<br>
&gt; <br>
&gt; public class Child extends Parent {<br>
&gt; <br>
&gt; @Inject<br>
&gt; private Instance&lt;AdvancedFoo&gt; fooInstance;<br>
&gt; <br>
&gt; @Override<br>
&gt; protected AdvancedFoo newFoo() {<br>
&gt; return fooInstance.get();<br>
&gt; }<br>
&gt; }<br>
&gt; <br>
&gt; <br>
&gt; 2)<br>
&gt; <br>
&gt; public class Parent {<br>
&gt; <br>
&gt; @Inject<br>
&gt; protected BeanManager beanManager;<br>
&gt; <br>
&gt; private SimpleFoo foo;<br>
&gt; <br>
&gt; protected SimpleFoo newFoo() {<br>
&gt; SimpleFoo foo = constructing bean with BM;<br>
&gt; return foo;<br>
&gt; }<br>
&gt; <br>
&gt; @PostConstruct<br>
&gt; private void doPostConstruct() {<br>
&gt; foo = newFoo();<br>
&gt; }<br>
&gt; }<br>
&gt; <br>
&gt; public class Child extends Parent {<br>
&gt; <br>
&gt; @Override<br>
&gt; protected AdvancedFoo newFoo() {<br>
&gt; AdvancedFoo foo = constructing bean with BM;<br>
&gt; return foo;<br>
&gt; }<br>
&gt; }<br>
&gt; <br>
&gt; Thank you for your explanation of Typed. I got it.<br>
&gt; <br>
&gt; Best regards, Alex<br>
&gt; <br>
&gt; <br>
&gt; Среда, 30 августа 2017, 10:51 +03:00 от Matej Novotny &lt;<a href="mailto:manovotn@redhat.com">manovotn@redhat.com</a>&gt;:<br>
&gt; <br>
&gt; 1) If you inject Instance&lt;T&gt;, you still have the ambiguous dependency issue<br>
&gt; for any class which does have a subclass.<br>
&gt; E.g. from your sample:<br>
&gt; <br>
&gt; @Inject<br>
&gt; Instance&lt;SimpleFoo&gt; instance;<br>
&gt; <br>
&gt; //within some method<br>
&gt; instance.get(); -&gt; this will blow up because you have two beans which have<br>
&gt; SimpleFoo type (SimpleFoo and AdvancedFoo)<br>
&gt; <br>
&gt; 2) I don't understand what you mean by this. How does BM help here?<br>
&gt; <br>
&gt; <br>
&gt; Sidenote:<br>
&gt; You might want to try and use what Martin said - limiting the types of a bean<br>
&gt; with @Typed(MyClass.Foo).<br>
&gt; That way you have control over the bean types and can further manupulate the<br>
&gt; injection.<br>
&gt; Limit all your children to only the actual subclass type they have:<br>
&gt; <br>
&gt; @Dependent<br>
&gt; @Typed(AdvancedFoo.class)<br>
&gt; public class AdvancedFoo extends SimpleFoo {<br>
&gt; // this ben now only has a bean of AdvancedFoo, e.g. it does not fit into<br>
&gt; injection point for SimpleFoo<br>
&gt; }<br>
&gt; <br>
&gt; And then override the initializer methods like this:<br>
&gt; <br>
&gt; @Dependent<br>
&gt; public class Parent extends Child {<br>
&gt; <br>
&gt; @Inject<br>
&gt; @Override<br>
&gt; protected void setFoo(AdvancedFoo foo) {<br>
&gt; this.foo = foo; // assuming foo is a protected field<br>
&gt; }<br>
&gt; }<br>
&gt; <br>
&gt; Matej<br>
&gt; <br>
&gt; <br>
&gt; ----- Original Message -----<br>
&gt; &gt; From: "Alex Sviridov" &lt; <a href="mailto:ooo_saturn7@mail.ru">ooo_saturn7@mail.ru</a> &gt;<br>
&gt; &gt; To: "weld-dev" &lt; <a href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a> &gt;<br>
&gt; &gt; Sent: Tuesday, August 29, 2017 8:54:47 PM<br>
&gt; &gt; Subject: Re: [weld-dev] How to make method injection when bean subclass is<br>
&gt; &gt; required in Weld?<br>
&gt; &gt; <br>
&gt; &gt; I thought here, and would like to share my ideas hoping to get comments<br>
&gt; &gt; from<br>
&gt; &gt; more experienced people.<br>
&gt; &gt; <br>
&gt; &gt; First of all I came to conclusion that CDI works badly with cases when we<br>
&gt; &gt; need<br>
&gt; &gt; to change field values in super classes. If there is a lot of inheritance<br>
&gt; &gt; as<br>
&gt; &gt; in my case:<br>
&gt; &gt; ParentA, ChildA0, ChildA1.., ParentB, ChildB0, ChildB1..,... then situation<br>
&gt; &gt; is<br>
&gt; &gt; becoming very bad. Maybe in future there will be other solutions in CDI<br>
&gt; &gt; specs.<br>
&gt; &gt; <br>
&gt; &gt; I found two additional ways that can be used. 1) Inject not beans but<br>
&gt; &gt; instances,<br>
&gt; &gt; + method SimpleFoo newFoo {return Instance&lt;SimpleFoo&gt;.get} + overriding.<br>
&gt; &gt; 2) Inject BeanManager + method SimpleFoo newFoo() {beanManager...} +<br>
&gt; &gt; overriding.<br>
&gt; &gt; <br>
&gt; &gt; Maybe such ways can be named lazy/postponed initialization with overriding<br>
&gt; &gt; support....<br>
&gt; &gt; <br>
&gt; &gt; Best regards, Alex<br>
&gt; &gt; <br>
&gt; &gt; <br>
&gt; &gt; <br>
&gt; &gt; <br>
&gt; &gt; Вторник, 29 августа 2017, 18:22 +03:00 от Martin Kouba &lt; <a href="mailto:mkouba@redhat.com">mkouba@redhat.com</a><br>
&gt; &gt; &gt;:<br>
&gt; &gt; <br>
&gt; &gt; Hi Alex,<br>
&gt; &gt; <br>
&gt; &gt; that's an interesting question. Indeed, qualifiers are the way to go if<br>
&gt; &gt; you need to keep the method signature.<br>
&gt; &gt; <br>
&gt; &gt; Another way could be to override the setFoo() method so that the Child<br>
&gt; &gt; initializer is ignored and add a new method to inject AdvancedFoo:<br>
&gt; &gt; <br>
&gt; &gt; @Override<br>
&gt; &gt; protected void setFoo(SimpleFoo foo) { // Do nothing }<br>
&gt; &gt; <br>
&gt; &gt; @Inject<br>
&gt; &gt; void setAdvancedFoo(AdvancedFoo foo) {<br>
&gt; &gt; super.setFoo(foo);<br>
&gt; &gt; }<br>
&gt; &gt; <br>
&gt; &gt; However, note that right now there are the following beans:<br>
&gt; &gt; <br>
&gt; &gt; SimpleFoo with bean types Object, SimpleFoo<br>
&gt; &gt; AdvancedFoo -&gt; Object, SimpleFoo, AdvancedFoo<br>
&gt; &gt; <br>
&gt; &gt; So if you do @Inject SimpleFoo you get ambiguous dependency exception<br>
&gt; &gt; because both SimpleFoo and AdvancedFoo are eligible for injection.<br>
&gt; &gt; <br>
&gt; &gt; To resolve this you need to use qualifiers or restrict the bean types of<br>
&gt; &gt; AdvancedFoo:<br>
&gt; &gt; <br>
&gt; &gt; @Typed(AdvancedFoo.class)<br>
&gt; &gt; class AdvancedFoo extends SimpleFoo {}<br>
&gt; &gt; <br>
&gt; &gt; HTH<br>
&gt; &gt; <br>
&gt; &gt; Martin<br>
&gt; &gt; <br>
&gt; &gt; <br>
&gt; &gt; Dne 29.8.2017 v 15:09 Matej Novotny napsal(a):<br>
&gt; &gt; &gt; Hi Alex,<br>
&gt; &gt; &gt; <br>
&gt; &gt; &gt; no need to be sorry, you have come to the right place :)<br>
&gt; &gt; &gt; As for your question, the simplest thing is probably to use qualifiers.<br>
&gt; &gt; &gt; <br>
&gt; &gt; &gt; Create your own like this:<br>
&gt; &gt; &gt; <br>
&gt; &gt; &gt; @Qualifier<br>
&gt; &gt; &gt; @Retention(RetentionPolicy.RUNTIME)<br>
&gt; &gt; &gt; @Target({ ElementType.TYPE, ElementType.PARAMETER, ElementType.FIELD,<br>
&gt; &gt; &gt; ElementType.METHOD })<br>
&gt; &gt; &gt; public @interface MyQualifier {}<br>
&gt; &gt; &gt; <br>
&gt; &gt; &gt; <br>
&gt; &gt; &gt; And then change your AdvancedFoo class to use the qualifier:<br>
&gt; &gt; &gt; <br>
&gt; &gt; &gt; @Dependent<br>
&gt; &gt; &gt; @MyQualifier<br>
&gt; &gt; &gt; public class AdvancedFoo extends SimpleFoo {<br>
&gt; &gt; &gt; }<br>
&gt; &gt; &gt; <br>
&gt; &gt; &gt; And accordingly, the init method which uses injection should then look<br>
&gt; &gt; &gt; like<br>
&gt; &gt; &gt; this:<br>
&gt; &gt; &gt; <br>
&gt; &gt; &gt; @Dependent<br>
&gt; &gt; &gt; public class Parent extends Child {<br>
&gt; &gt; &gt; <br>
&gt; &gt; &gt; @Inject<br>
&gt; &gt; &gt; @Override<br>
&gt; &gt; &gt; protected void setFoo(@MyQualifier SimpleFoo foo) {<br>
&gt; &gt; &gt; super.setFoo(foo);<br>
&gt; &gt; &gt; }<br>
&gt; &gt; &gt; }<br>
&gt; &gt; &gt; <br>
&gt; &gt; &gt; Does this answer your question?<br>
&gt; &gt; &gt; <br>
&gt; &gt; &gt; Matej<br>
&gt; &gt; &gt; <br>
&gt; &gt; &gt; ----- Original Message -----<br>
&gt; &gt; &gt;&gt; From: "Alex Sviridov" &lt; <a href="mailto:ooo_saturn7@mail.ru">ooo_saturn7@mail.ru</a> &gt;<br>
&gt; &gt; &gt;&gt; To: "weld-dev" &lt; <a href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a> &gt;<br>
&gt; &gt; &gt;&gt; Sent: Tuesday, August 29, 2017 1:46:23 PM<br>
&gt; &gt; &gt;&gt; Subject: [weld-dev] How to make method injection when bean subclass is<br>
&gt; &gt; &gt;&gt; required in Weld?<br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; Hi all,<br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; I am really sorry for writing to this mailing list, but I checked all<br>
&gt; &gt; &gt;&gt; user<br>
&gt; &gt; &gt;&gt; forums and chats and saw that they are very old.<br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; I would be very thankful if someone gives suggestion for solving the<br>
&gt; &gt; &gt;&gt; following problem.<br>
&gt; &gt; &gt;&gt; I have a child and parent class. Child has SimpleFoo, Parent needs<br>
&gt; &gt; &gt;&gt; Advaced<br>
&gt; &gt; &gt;&gt; foo. So,<br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; @Dependent<br>
&gt; &gt; &gt;&gt; public class SimpleFoo {<br>
&gt; &gt; &gt;&gt; }<br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; @Dependent<br>
&gt; &gt; &gt;&gt; public class AdvancedFoo extends SimpleFoo {<br>
&gt; &gt; &gt;&gt; }<br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; @Dependent<br>
&gt; &gt; &gt;&gt; public class Child {<br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; private SimpleFoo foo;<br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; @Inject<br>
&gt; &gt; &gt;&gt; protected void setFoo(SimpleFoo foo) {<br>
&gt; &gt; &gt;&gt; this.foo = foo;<br>
&gt; &gt; &gt;&gt; }<br>
&gt; &gt; &gt;&gt; }<br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; @Dependent<br>
&gt; &gt; &gt;&gt; public class Parent extends Child {<br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; @Inject<br>
&gt; &gt; &gt;&gt; @Override<br>
&gt; &gt; &gt;&gt; protected void setFoo(SimpleFoo foo) { //How to inject here AdvancedFoo?<br>
&gt; &gt; &gt;&gt; super.setFoo(foo);<br>
&gt; &gt; &gt;&gt; }<br>
&gt; &gt; &gt;&gt; }<br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; How to inject in Parent AdvancedFoo? I know that I can do it via<br>
&gt; &gt; &gt;&gt; constructor<br>
&gt; &gt; &gt;&gt; injection<br>
&gt; &gt; &gt;&gt; but I need method injection. How to do it? Can it be done without using<br>
&gt; &gt; &gt;&gt; names<br>
&gt; &gt; &gt;&gt; (like MyBean1)<br>
&gt; &gt; &gt;&gt; but only using classes (AdvancedFoo)?<br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; Best regards, Alex<br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; --<br>
&gt; &gt; &gt;&gt; Alex Sviridov<br>
&gt; &gt; &gt;&gt; <br>
&gt; &gt; &gt;&gt; _______________________________________________<br>
&gt; &gt; &gt;&gt; weld-dev mailing list<br>
&gt; &gt; &gt;&gt; <a href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a><br>
&gt; &gt; &gt;&gt; <a href="https://lists.jboss.org/mailman/listinfo/weld-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/weld-dev</a><br>
&gt; &gt; &gt; _______________________________________________<br>
&gt; &gt; &gt; weld-dev mailing list<br>
&gt; &gt; &gt; <a href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a><br>
&gt; &gt; &gt; <a href="https://lists.jboss.org/mailman/listinfo/weld-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/weld-dev</a><br>
&gt; &gt; &gt; <br>
&gt; &gt; <br>
&gt; &gt; --<br>
&gt; &gt; Martin Kouba<br>
&gt; &gt; Senior Software Engineer<br>
&gt; &gt; Red Hat, Czech Republic<br>
&gt; &gt; <br>
&gt; &gt; <br>
&gt; &gt; _______________________________________________<br>
&gt; &gt; weld-dev mailing list<br>
&gt; &gt; <a href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a><br>
&gt; &gt; <a href="https://lists.jboss.org/mailman/listinfo/weld-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/weld-dev</a><br>
&gt; <br>
&gt; <br>
&gt; _______________________________________________<br>
&gt; weld-dev mailing list<br>
&gt; <a href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a><br>
&gt; <a href="https://lists.jboss.org/mailman/listinfo/weld-dev" target="_blank">https://lists.jboss.org/mailman/listinfo/weld-dev</a><br>
</div>
            
        
                <base target="_self" href="https://e.mail.ru/">
        </div>

        
</div>


</div>
</blockquote>
<br></BODY></HTML>