|
#1
|
|||
|
|||
|
I am currently injecting properties into a "Car" object. My question below specifically refers to a static enumerated "Direction" type that I also describe below. Here is a simplified version of the Car:
Code:
public class Car
{
public Direction direction;
public int speed;
...
}
Code:
Car car = new Car(Direction.EAST, 55); Code:
public class Direction
{
private final String direction;
public static Direction NORTH = new Direction("North");
public static Direction SOUTH = new Direction("South");
public static Direction EAST = new Direction("East");
public static Direction WEST = new Direction("West");
// notice the PRIVATE constructor
private Direction(String direction)
{
this.direction = direction;
}
public String toString()
{
return this.direction;
}
}
My Spring Xml file is the typical Spring.xml: Code:
<beans>
...
<bean id="car1" class="edu.psu.sweng.model.Car">
<constructor-arg index="0">
<ref bean="east"/>
</constructor-arg>
<constructor-arg index="1>
<value>70</value>
</constructor-arg>
</bean>
<!-- Direction Enumerations -->
<bean id="east" class="edu.psu.sweng.model.Direction" singleton="false">
<constructor-arg>
<value>East</value>
</constructor-arg>
</bean>
...
</beans>
Any help would be appreciated. |
|
#2
|
|||
|
|||
|
It certainly is possible. Take a look at the JavaDoc for FieldRetrievingFactoryBean.
Rob
__________________
Rob Harrop Lead Engineer, dm Server SpringSource http://www.springsource.com Co-Author - Pro Spring |
|
#3
|
|||
|
|||
|
Yes Rob, thank you... here is the answer I came up with, which currently works!
The key here is with the "staticField" property name. The property name MUST be called "staticField". Code:
<bean id="east" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean">
<property name="staticField">
<value>edu.psu.sweng.model.Direction.EAST</value>
</property>
</bean>
|
|
#4
|
|||
|
|||
|
Sweet!
I just joined the forum, because I had exactly the same question. I entered "enumeration" as my search criterion, found this thread, and have my answer. Life is good. Thanks! Ed
__________________
Often the challenge lies not in crafting a solution, but in understanding the problem |
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| FlowExecutionStorage in a DB | cacho | Spring Web Flow | 7 | Oct 19th, 2009 04:36 PM |
| Writing a Util class with all static methods using Bean ... | mingshun | Core Container | 5 | Jan 23rd, 2008 05:30 PM |
| static initialization block | sleyzerzon | Core Container | 1 | Sep 25th, 2005 02:28 PM |
| Questioning the core component | Martin Kersten | Spring Rich Client Project | 6 | Feb 21st, 2005 04:45 AM |
| View with JTree on Left, TreeNode Properties on Right | cyboc | Spring Rich Client Project | 19 | Oct 21st, 2004 12:24 AM |