Announcement Announcement Module
Collapse
No announcement yet.
Is it possible to inject Static Enumerated Properties? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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.

  • #2
    It certainly is possible. Take a look at the JavaDoc for FieldRetrievingFactoryBean.

    Rob

    Comment


    • #3
      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>

      Comment


      • #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

        Comment

        Working...
        X