Community   SpringSource   Projects    Downloads    Documentation    Forums    Training   Exchange   Blogs

Go Back   Spring Community Forums > Core Spring Projects > Core Container

Reply
 
Thread Tools Display Modes
  #1  
Old May 31st, 2005, 09:58 AM
arlevitt arlevitt is offline
Junior Member
 
Join Date: May 2005
Location: New York, NY
Posts: 6
Default Is it possible to inject Static Enumerated Properties?

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;
  ...
}
My "Direction" class is a static enumeration. Thus, its enumerated types would be accessed in the following manner:

Code:
Car car = new Car(Direction.EAST, 55);
The code for the direction class looks like this:

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;
    }
}
Refer here for a complete pattern description from Effective Java.

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>
When I refer to the "east" bean, it creates a Direction class, but it is not created properly as one of the enumerated types. I believe this may be a legal operation because Spring is using reflection. I have tried this with and without using [singleton="false"] on the "east" bean. However, I am wondering if anyone knows how to access and inject one of the static types that i have defined. (ie: Direction.EAST instead of just "East")
Any help would be appreciated.
Reply With Quote
  #2  
Old May 31st, 2005, 01:48 PM
robh robh is offline
Senior Member
Spring Modules TeamSpring Team
 
Join Date: Aug 2004
Location: Southampton, UK
Posts: 827
Default

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
Reply With Quote
  #3  
Old May 31st, 2005, 01:58 PM
arlevitt arlevitt is offline
Junior Member
 
Join Date: May 2005
Location: New York, NY
Posts: 6
Default Static Enumerated Types - FieldRetrievingFactoryBean

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>
Reply With Quote
  #4  
Old Jun 22nd, 2005, 12:01 PM
eguy66 eguy66 is offline
Junior Member
 
Join Date: Jun 2005
Location: Dover, NJ
Posts: 14
Default

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
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

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


All times are GMT -5. The time now is 10:59 AM.


Contegix provides first-class managed hosting and partial sponsorship of these forums.

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.