0

I have the following field called someMap in which I need to inject a map at runtime depending upon the inputs I have received.

I have tried using the @Resource annotaion but it is tightly coupled to the beanId specified in the xml file.

@Resource(name="someMap")
private final Map<String,String> someMap;
<bean id="someMap" class="java.util.HashMap">
        <constructor-arg>
        <map>
            <entry key="Engraving1" value="@Engraving1" />
            <entry key="Engraving2" value="@Engraving2" />
            <entry key="Engraving3" value="@Engraving3" />
            <entry key="LeaveBlank" value="@LeaveBlank" />
        </map>
    </constructor-arg>
</bean>

    <bean id="someOtherMap" class="java.util.HashMap">
        <constructor-arg>
        <map>
            <entry key="Descirption" value="@Desc" />
            <entry key="Engraving2" value="@Engraving2" />
            <entry key="Engraving3" value="@Engraving3" />
            <entry key="UniqueId" value="$60034" />
        </map>
    </constructor-arg>
</bean>

What I want is to get rid of this tight coupling & initialize the someMap field to a differnt flavour of the Map defined in spring xml let's say someOtherMap

seenukarthi
  • 8,241
  • 10
  • 47
  • 68

1 Answers1

0

You can't change the annotation dynamic without using Reflection , so you can't change the name of reousece.

 @Resource(name="NAME")

But it is very easy to define two bean with different property or constrctor-arg.

    <bean id="boss" class="***">
       <property name="mydata">
           <bean class="HashMap">
           </bean>
       </property>
    </bean>

if you just want one bean definition with different data, XML and Annotation should not work. factory-method or setMethod will work for it, you need some java code.

Gui FU
  • 31
  • 4