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