<HTML><BODY>Hi Martin<br><br>Thank you very much for your comment and suggestion about improving.<br><br>Best regards, Alex<br><br><br><blockquote style="border-left:1px solid #0857A6; margin:10px; padding:0 0 0 10px;">
        Четверг, 31 августа 2017, 12:50 +03:00 от Martin Kouba &lt;mkouba@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_15041730570000000403_BODY">Hi Alex,<br>
<br>
1) should work but looks a little bit cumbersome. WRT 2) you should <br>
avoid using BeanManager to create a bean instance whenever possible. And <br>
in this particular case, if SimpleFoo is @Dependent it wouldn't be <br>
destroyed correctly (@PreDestroy) when Parent or Child is destroyed.<br>
<br>
The following should also work (no need to have two Instance IPs):<br>
<br>
public class Parent {<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Inject<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private Instance&lt;SimpleFoo&gt; fooInstance;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private SimpleFoo foo;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;protected SimpleFoo newFoo() {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return fooInstance.get();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@PostConstruct<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;private void doPostConstruct() {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foo = newFoo();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;}<br>
<br>
&nbsp;&nbsp;public class Child extends Parent {<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Override<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;protected AdvancedFoo newFoo() {<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return fooInstance.select(AdvancedFoo.class).get();<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
&nbsp;&nbsp;}<br>
<br>
<br>
Martin<br>
<br>
Dne 31.8.2017 v 11:07 Alex Sviridov napsal(a):<br>
&gt; Hi Martin,<br>
&gt; <br>
&gt; <br>
&gt; Could you comment the following solutions?<br>
&gt; <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; <br>
&gt; Best regards, Alex<br>
&gt; <br>
&gt;     Среда, 30 августа 2017, 10:51 +03:00 от Matej Novotny<br>
&gt;     &lt;<a href="mailto:manovotn@redhat.com">manovotn@redhat.com</a> &lt;mailto:<a href="mailto:manovotn@redhat.com">manovotn@redhat.com</a>&gt;&gt;:<br>
&gt; <br>
&gt;     1) If you inject Instance&lt;T&gt;, you still have the ambiguous<br>
&gt;     dependency issue 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<br>
&gt;     which have 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<br>
&gt;     of a bean with @Typed(MyClass.Foo).<br>
&gt;     That way you have control over the bean types and can further<br>
&gt;     manupulate the 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<br>
&gt;     fit into 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><br>
&gt;     &lt;mailto:<a href="mailto:ooo_saturn7@mail.ru">ooo_saturn7@mail.ru</a>&gt;&gt;<br>
&gt;      &gt; To: "weld-dev" &lt;<a href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a><br>
&gt;     &lt;mailto:<a href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a>&gt;&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<br>
&gt;     subclass is required in Weld?<br>
&gt;      &gt;<br>
&gt;      &gt; I thought here, and would like to share my ideas hoping to get<br>
&gt;     comments 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<br>
&gt;     when we<br>
&gt;      &gt; need<br>
&gt;      &gt; to change field values in super classes. If there is a lot of<br>
&gt;     inheritance as<br>
&gt;      &gt; in my case:<br>
&gt;      &gt; ParentA, ChildA0, ChildA1.., ParentB, ChildB0, ChildB1..,... then<br>
&gt;     situation<br>
&gt;      &gt; is<br>
&gt;      &gt; becoming very bad. Maybe in future there will be other solutions<br>
&gt;     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} +<br>
&gt;     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<br>
&gt;     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<br>
&gt;     &lt;<a href="mailto:mkouba@redhat.com">mkouba@redhat.com</a> &lt;mailto:<a href="mailto:mkouba@redhat.com">mkouba@redhat.com</a>&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<br>
&gt;     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<br>
&gt;     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<br>
&gt;     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<br>
&gt;     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,<br>
&gt;     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<br>
&gt;     then look 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><br>
&gt;     &lt;mailto:<a href="mailto:ooo_saturn7@mail.ru">ooo_saturn7@mail.ru</a>&gt; &gt;<br>
&gt;      &gt; &gt;&gt; To: "weld-dev" &lt; <a href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a><br>
&gt;     &lt;mailto:<a href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a>&gt; &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<br>
&gt;     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<br>
&gt;     checked all 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<br>
&gt;     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<br>
&gt;     needs 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<br>
&gt;     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<br>
&gt;     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> &lt;mailto:<a href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a>&gt;<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> &lt;mailto:<a href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a>&gt;<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> &lt;mailto:<a href="mailto:weld-dev@lists.jboss.org">weld-dev@lists.jboss.org</a>&gt;<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; ------------------------------------------------------------------------<br>
&gt; <br>
&gt; -- <br>
&gt; Alex Sviridov<br>
<br>
-- <br>
Martin Kouba<br>
Senior Software Engineer<br>
Red Hat, Czech Republic<br>
</div>
            
        
                <base target="_self" href="https://e.mail.ru/">
        </div>

        
</div>


</div>
</blockquote>
<br>
<br>-- <br>Alex Sviridov<br></BODY></HTML>