Reputation: 8631
I'm injecting in a list which will populate a map used within the class.
However when injecting in the flowing attribute:
private List<?> srcSystemIDList;
via the flowing XML:
<bean id="transformerHelper" class="com.common.TransformerHelper">
<property name="srcSystemIDList" value="#{ T(java.util.Arrays).asList('6','57','92','93','7','108','106','105','98','52','122','9','26','51','101','102','118') }" />
</bean>
which is used by this method to set the map:
public void srcSystemIDListInit()
{
Object[] srcSystemArray = srcSystemIDList.toArray();
int j;
for(int i = 0; i< srcSystemArray.length; i = i+2)
{
j = i + 1;
if(j < srcSystemArray.length)
{
srcSystemIDMap.put(srcSystemArray[i].toString(), srcSystemArray[j].toString());
}
}
}
I'm calling this via the constructor.
public TransformerHelper()
{
srcSystemIDListInit();
}
However when the constructor calls the method the list is null throwing a null pointer exception.
How can I populate the map via the injected list
Upvotes: 1
Views: 179
Reputation: 42849
You can do as Vikdor says, or you have a couple other approaches you can take too.
setSrcSystemIDList(...)
. This will require the least work. (But really, all of these suggestions are quite easy...)InitializingBean
interface, and call your method from there.@PostConstruct
.init-method
attribute to your bean
xml, which specifies the method to call.Upvotes: 2
Reputation: 16060
You should have a look at something like this
<bean id="transformerHelper" class="com.common.TransformerHelper">
<constructor-arg><ref bean="yourListBean"></constructor-arg>
</bean>
Visit the Source for more info.
Cheers,
Upvotes: 0
Reputation: 2121
You can either call initialization from property setter:
public void setSrcSystemIDList(List<?> srcSystemIDList) {
this.srcSystemIDList = srcSystemIDList;
srcSystemIDListInit();
}
And use your spring configuration as is.
Or define a constructor with parameters:
public TransformerHelper(List<?> srcSystemIDList) {
this.srcSystemIDList = srcSystemIDList;
srcSystemIDListInit();
}
And transfer the list as constructor argument in spring configuration:
<bean id="transformerHelper" class="com.common.TransformerHelper">
<constructor-arg value="#{ T(java.util.Arrays).asList('6','57','92','93','7','108','106','105','98','52','122','9','26','51','101','102','118') }" />
</bean>
Upvotes: 1
Reputation: 24124
If you want to inject through constructor, then you should declare your constructor as
public TransformerHelper(List<?> srcSystemIDList)
{
this.srcSystemIDList = srcSystemIDList;
srcSystemIDListInit();
}
and your bean definition should be
<bean id="transformerHelper" class="com.common.TransformerHelper">
<constructor-arg value="#{ T(java.util.Arrays).asList('6','57','92','93','7','108','106','105','98','52','122','9','26','51','101','102','118') }" />
</bean>
Upvotes: 2