Results 1 to 6 of 6

Thread: Converting Enum to int and String for injection in Spring 2.5

  1. #1
    Join Date
    Feb 2006
    Posts
    115

    Default Converting Enum to int and String for injection in Spring 2.5

    I have a Enum that I need to convert to various values, a String and an int, for injection into some other classes. I have been googling like crazy to figure this out, and it seems like I should be able to do it with a TypeConverter or a PropertyEditor.

    I did manage it implement a generic PropertyEditor with the getAsText() and setAsText() methods, but Spring is still coughing on startup.

    I still have not found out how to implement and register a custom TypeConverter, or found any information saying this is not what I really want.

    Can anyone help here?

  2. #2
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    First are the Strings and ints already int the Enum. Meaning does the int mean the position in the enum, and String being the String representation in the Enum. If so, those methods are already part of an Enum. If not, you can add methods to the Enum to do those conversions for you.

    Also, remember a PropertyEditor converts from String to an Object type. So if you want int, you can have it return an Integer instead.

    Shame you aren't using Spring 3.x or you could use Spring Expression Language.

    I think you need to add more detail as to what you have tried and your configuration.

    Mark

  3. #3
    Join Date
    Feb 2006
    Posts
    115

    Default

    Quote Originally Posted by bytor99999 View Post
    First are the Strings and ints already int the Enum. Meaning does the int mean the position in the enum, and String being the String representation in the Enum. If so, those methods are already part of an Enum. If not, you can add methods to the Enum to do those conversions for you.

    Also, remember a PropertyEditor converts from String to an Object type. So if you want int, you can have it return an Integer instead.

    Shame you aren't using Spring 3.x or you could use Spring Expression Language.

    I think you need to add more detail as to what you have tried and your configuration.

    Mark
    The ints are not the positions. They are custom digits set through constructors on the items in the Enum. And I have methods to do the conversion, but I am not sure how to call them. In the context file I am doing something like
    Code:
    <value type="com.mycompany.CustomEnum">ENUM_ITEM</value>
    This is the only way I know how to use the enum from an XML context file.

  4. #4
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    Well, if you upgrade to Spring 3.x then you can use Spring Expression Language.

    so you could do something like

    <value type="com.mycompany.CustomEnum">#{ENUM_ITEM.intMet hod}</value>

    Or in your object you could inject the CustomEnum into the object, then in the setter method code you call intMethod, and stringMethod to set the other two properties in the object.

    Mark

  5. #5
    Join Date
    Feb 2006
    Posts
    115

    Default

    So, TypeConverter is not the way to go here?

  6. #6
    Join Date
    Jan 2009
    Location
    Huntington Beach, CA
    Posts
    718

    Default

    Not that you couldn't find a way to use TypeConverter, and since you aren't using Spring 3.x you can't use the new Converter api.

    But one of the main things to try to do when building an app that uses Spring, is to try to keep yourself from coupling to Spring. So if you create a class that implements a Spring interface, that class is now coupled to Spring. And the framework really tries to help you avoid coupling to Spring.

    That is why I like my approach of

    Code:
    public class Fake {
    
        private int theIntValue;
        private String theStringValue;
    
        public void setValues(MyEnum enum) {
            this.theIntValue = enum.getIntValue();
            this.theStringValue = enum.getStringValue();
        } 
    }
    There the setValues is my converter method and I can call the setValues method with

    <property name="values" value="MyEnum.SOME_ENUM_VALUE"/>

    I personally like that in that outside of Spring I can write code that will work too.

    Here, is another possible solution.

    http://deepthoughts.orsomethingliket...2006/10/19/13/

    Another poster lower on the first page here.
    http://forum.springsource.org/showthread.php?t=11759

    Says a good point, that the purpose of Enums really are to just use the Enum FIELD, and not have to convert between ints and Strings, but I have done it myself too.

    Mark

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •