Issue
I'm trying to inject a list of Color
from java.awt.Color
into my bean. In my XML I have:
<bean id="twoPlayers" class="basic.TwoPlayer">
<property name="Colors">
<list value-type="java.awt.Color">
<value>BLUE</value>
<value>GREEN</value>
</list>
</property>
</bean>
The thing is it sees BLUE
and GREEN
as String
so when I run the app I get an error saying type mismatch, can't cast String to Color. But I pointed out the type, so what's the problem? How to do this correctly?
The class:
public class TwoPlayer {
public Color[] Colors;
public void setColors(Color[] colors) {
Colors = colors;
}
...
}
I know I have ar array here, not a list but I've read it's not a problem for Spring.
Solution
Inspired by Vinayak Mittal answer (sorry, I can't upvote your answer, I don't have enough reputation), I decided to go with something like this:
<bean id="blue" class="java.awt.Color">
<constructor-arg index="0" type="int"><value>0</value></constructor-arg>
<constructor-arg index="1" type="int"><value>6</value></constructor-arg>
<constructor-arg index="2" type="int"><value>234</value></constructor-arg>
</bean>
<bean id="green" class="java.awt.Color">
<constructor-arg index="0" type="int"><value>29</value></constructor-arg>
<constructor-arg index="1" type="int"><value>172</value></constructor-arg>
<constructor-arg index="2" type="int"><value>32</value></constructor-arg>
</bean>
<bean id="twoPlayers" class="basic.TwoPlayer">
<property name="Colors">
<list value-type="java.awt.Color">
<ref bean="blue"/>
<ref bean="green"/>
</list>
</property>
</bean>
It's not exactly what I wanted because I have to define the colors myself but it is good enough solution.
Answered By - Nerwena