-1

Ive found this configuration on a applicationContext xml and I still dont understand it (even if I found info about it, is not clear) could someone give me an idea of how java chooses the right bean for its property? I did not find this bean in other file of the project context, seems like is "magic"

<bean id="mailCreator"  class="com.project.ConfigurableXferEmailCreator">
        <property name="mailCreatorMap">
            <util:map id="mailCreatorMap" key-type="java.lang.String"
                    value-type="com.project.BaseMailCreator">
                <entry>
                    <key>
                        <util:constant static-field="com.project.creatorType.TYPE1"/>
                    </key>
                    <bean class="com.project.creator1" parent="baseCreator">
                        <property name="service" ref="someService1" />
                    </bean>
                </entry>
                <entry>
                    <key>
                        <util:constant static-field="com.project.creatorType.TYPE2"/>
                    </key>
                    <bean class="com.project.creator1" parent="baseCreator">
                        <property name="service" ref="someService2" />
                    </bean>
                </entry>

  .... and so on

I really have no idea how java recognizes which one will use, I just know it uses the right service but I dont see where is being specifically set, could someone give me a hand?

I checked couple of sites like this but still no idea , does it call all services?? http://www.java2s.com/Tutorials/Java/Spring/0140__Spring_Map_Properties.htm

jpganz18
  • 5,508
  • 17
  • 66
  • 115
  • It's a bit unclear to me what you're asking. What bean can you not find elsewhere? What is "java choosing"? This looks straightforward to me, it's just constructing a Map. – Taylor Jan 11 '18 at 18:35
  • Not an exact answer to your question, but relevant https://stackoverflow.com/questions/3153546/how-does-autowiring-work-in-spring – secondbreakfast Jan 11 '18 at 18:39
  • @Taylor, if I just call mail creator, will I just be able to access the beans as properties? is there any de fault if I dont know which one to get? – jpganz18 Jan 11 '18 at 19:34
  • ...pretty straight forward question and not unclear. – Jeryl Cook Jan 16 '18 at 22:49

1 Answers1

0

Your question: "how java chooses the right bean for its property"

I assume you mean for example this line:

<property name="service" ref="someService1" />

if you do not see another XML bean element that defines "someService1" fr example:

<bean id="someService1"  class="com.project.SomeService1Impl">

Then it exists in the Java Code try to find an annotation like below

@Service("someService1")
public class SomeService1Impl {
...

How?: Spring loads "someService1" in the Spring Context on initialization , so the XML can reference it.

Jeryl Cook
  • 989
  • 17
  • 40