PDA

View Full Version : accessing a beans get function - ?injection to another bean?



akfoote
Jul 3rd, 2012, 02:08 PM
Hi all - new to spring and the way spring handles the underlying data (that I need to get to :)

I've got a project where I'm instantiating beans to handle incoming request header values. The filter bean that is defined is correctly picking these up and creating its internal map structure.

Now I need to get this map into another bean ..

What I have so far has not worked....

In a nutshell, I'm trying to set the Map of my second bean by accessing a getHeaderMap of the first filter bean.


<bean id="requestHeaderFilter" class="my.web.RequestHeaderFilter">
<property name="headerMapping">
<map>
<entry key="eid" value="eid" />
<entry key="displayName" value="displayName" />
</map>
</property>
</bean>


<bean id="headerRepo"
class="my.web.HeaderCollection">
<property name="backingMap" ref="requestHeaderFilter.getHeaderMapping()" />
</bean>


Any bean guidance would be helpful.. I'm currently also reading 'springbyexample' but just started...

Thanks.

amiladomingo
Jul 3rd, 2012, 03:09 PM
Hi,

You can declare the map as another bean and inject it to both beans.



<bean class="java.util.HashMap" id="map">
<constructor-arg index="0">
<map>
<entry key="eid" value="eid" />
<entry key="displayName" value="displayName" />
</map>
</constructor-arg>
</bean>


Note that you can also use the util namespace to create the map.

akfoote
Jul 3rd, 2012, 08:08 PM
Ahh OK so my map bean gets passed into each other bean using something like

<property name="headerMapping" ref="mapbean">

Is that what you mean?

amiladomingo
Jul 3rd, 2012, 11:44 PM
Ahh OK so my map bean gets passed into each other bean using something like

<property name="headerMapping" ref="mapbean">

Is that what you mean?

Exactly :)