Results 1 to 5 of 5

Thread: Defining Enum value in bean definition (Spring 3.0.5.RELEASE)

Hybrid View

  1. #1

    Question Defining Enum value in bean definition (Spring 3.0.5.RELEASE)

    I have an enum defined as

    Code:
    public enum Monsoon {
    	SPRING(1),
    	SUMMER(2),
    	AUTUMN(3),
    	FALL(4),
    	WINTER(5);
    	
       private final Integer seasonId;
       	
       public Integer seasonId() {
    	return seasonId;
       }
       
       private Monsoon(Integer seasonId) {
          this.seasonId = seasonId;		
       }
    }
    i had defined the enum as property to a bean as follows;

    Code:
    <bean id="weather" class="org.weather.WeatherIntereceptor">
        <property name="seasonId">
            <value type="org.weather.Monsoon">#{SPRING.seasonId}</value>
        </property>
    </bean>
    But the above throws an exception saying SPRING no property or field definition

    Where am i going wrong ?

  2. #2
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

    I don't know if this helps you, but if you instead defined a "season" instance variable on "WeatherInterceptor" of type "Monsoon" (which really should be "Season"), then you could do this:
    Code:
    <bean id="weather" class="org.weather.WeatherIntereceptor">
        <property name="season" value="SPRING"/>
    </bean>

  3. #3

    Default

    That would'nt return 1 ( seasonId integer value ) that I was looking for, it would only give me SPRING.

  4. #4
    Join Date
    Jan 2006
    Location
    Seattle, Washington
    Posts
    467

    Default

    Of course it wouldn't return "1", it would return the actual enum value. I got this information right out of the Spring documentation. Search for "Setting a bean property or constructor arg from a field value" in the documentation.

  5. #5

    Default

    Agreed dkarr. I refactored the interceptor to contain enum instance inplace of featureId.
    That gives me access to featureId from the enum. Thanks!

Tags for this Thread

Posting Permissions

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