Announcement Announcement Module
Collapse
No announcement yet.
How to Control Map Entries when Autowire? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to Control Map Entries when Autowire?

    Hello,

    given this configuration:

    Code:
    [...]
       <context:component-scan base-package="com.company.name.testpackage"/>
    
    
    [...]
     <bean id="myString" class="java.lang.String">
            <constructor-arg type="java.lang.String" value="bla"/>
        </bean>
    
        <bean id="yourString" class="java.lang.String">
            <constructor-arg type="java.lang.String" value="aha"/>
        </bean>
    
        <bean id="regionsMap" class="java.util.TreeMap" autowire="constructor" autowire-candidate="false">
            <constructor-arg type="java.util.Map">
                <map>
                    <entry key="northSea" value="polygon((3 52, 3 56.5, 9.5 56.5, 9.5 52))"/>
                    <entry key="balticSea" value="polygon((9 53, 9 56.5, 14.5 56.5, 14.5 53))"/>
                </map>
            </constructor-arg>
        </bean>
    and that Java Code:
    Code:
    @Service
    public class PrepareInputProductsTasklet implements Tasklet {
        
        @Autowired
        private Map<String, String> regionsMap;
      
    [...]
    why do I get a regionsMap with "myString" -> "bla" and "yourString" -> "aha"?

    What I want is the content I gave to the constructor, how can that be accomplished?
    (wanted: "balticSea" -> "..." and "northSea" -> "...")

    Kind regards,
    Kieran

  • #2
    @Autowired in fields is used to inject beans by type. Drop the @autowired annotation from regionsMap and will work (assuming that you are assigning the map in the ctor).

    Cheers

    Comment

    Working...
    X