Announcement Announcement Module
Collapse
No announcement yet.
HELP IoC problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • HELP IoC problem

    Hello All,

    Please advise I cannot access the exchangeCodes properties while I am able to access this one...<property name="areaCode" value="205"/>

    when I try to get the exchangeCodes I get null values.

    Thanks
    Richard

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
        "http://www.springframework.org/dtd/spring-beans.dtd">
    
    <beans>
        <bean id="SignupController" class="xxx.xxx.xxx.controller.SignupWizardController">
            <property name="pages">
                <list>
                    <value>signup-start-phoneplans</value>
                    <value>signup-step1-phonenumber</value>
                    <value>signup-step2-phoneadapter</value>
                </list>
            </property>
            <property name="phonePlansDao">
                <ref bean="PhonePlansDao"/>
            </property>
            <property name="sessionForm"><value>true</value></property>
            <property name="commandName"><value>sc</value></property>
            <property name="commandClass"><value>xxx.xxx.xxx.command.SignupCommand</value></property>
            <property name="countries" >
                <set>
                    <bean id="us" class="com.voxsant.commons.constants.Country">
                        <property name="country" value="United States"/>
                        <property name="stateProvinceRegion">
                            <set>
                                <bean id="alabama" class="xxx.xxx.xxx.constants.StateProvinceRegion">
                                    <property name="stateProvinceRegion" value="Alabama"/>
                                    <property name="areaCodes">
                                        <set>
                                            <ref bean="ac205"/>
                                        </set>
                                    </property>
                                </bean>
                            </set>
                        </property>
                    </bean>
                    <bean id="canada" class="com.xxx.xxx.constants.Country">
                        <property name="country" value="Canada"/>
                    </bean>
                </set>
            </property>
        </bean>
        <bean id="ac205" class="com.xxx.xxx.constants.AreaCode">
            <property name="areaCode" value="205"/>
            <property name="exchangeCodes" >
                <set>
                    <value>267 (Bessemer)</value>
                    <value>282 (Jasper)</value>
                    <value>304 (Dora)</value>
                    <value>316 (Centrevl)</value>
                    <value>352 (Leeds)</value>
                    <value>386 (Gardendale)</value>
                    <value>390 (Graysville)</value>
                    <value>409 (Tuscaloosa)</value>
                    <value>419 (Alabaster)</value>
                    <value>453 (Birmingham)</value>
                    <value>683 (Pinson)</value>
                    <value>742 (Livingston)</value>
                </set>
            </property>
        </bean>
    
    </beans>

  • #2
    Strange - can you please make sure that your configuration is correct (maybe with a very small example) and if possible, turn on logging to make sure the conversion works.
    I see no reason why the exchangeCodes set should work.
    Btw, it's recommended to externalize the codes (maybe to a properties file) - you can convert it to a set if you still want to (declare only keys but no values).

    Comment


    • #3
      Hi Costin,

      I am hoping that you can show a sample on how to do this..

      Btw, it's recommended to externalize the codes (maybe to a properties file) - you can convert it to a set if you still want to (declare only keys but no values).

      Thanks
      Richard

      Comment


      • #4
        It's discussed (with an example) in the spring documentation here:

        http://static.springframework.org/sp...lderconfigurer

        Comment


        • #5
          johnny beat me to it. Besides the reference documentation, you can also see the samples included in Spring distribution.

          Comment


          • #6
            Hi Costin/Guys,

            I hope somebody can give me a good example on how I can externalized these values, I am currently using a property file for my datasource values but I cannot visualize how different will it be if I put these values...

            Code:
            <value>267 (Bessemer)</value>
                            <value>282 (Jasper)</value>
            in a property file. I think the problem is in the <set>

            Code:
            <property name="exchangeCodes" >
                        <set>
                            <value>267 (Bessemer)</value>
                            <value>282 (Jasper)</value>
            part of the injection. If I use a property file i will still be using the <set> element right?

            Thanks
            Richard

            Comment


            • #7
              <property name="exchangeCodes">
              <bean class="org.springframework.beans.factory.config.Pr opertiesFactoryBean>
              <property name="location" value="classpath:myValues.properties"/>
              </bean>
              </property>

              while your myValues property can contain something like:
              267 = Bessemer
              282 = Jasper
              If you want to have only keys and your are not interested into a map you can use this format:

              267 Bessemer =
              282 Jasper =
              which will result in map just with keys (and empty values).

              Comment


              • #8
                Hi Costin,

                In here you have assumed that exchangeCodes is of java.util.Set datatype?

                Code:
                <property name="exchangeCodes">
                <bean class="org.springframework.beans.factory.config.Pr opertiesFactoryBean>
                <property name="location" value="classpath:myValues.properties"/>
                </bean>
                </property>
                thanks
                Richard

                Comment


                • #9
                  No, I have assumed that exchangeCodes is of type map (which is a parent of java.util.Properties). You can do a simple conversion from Map to Set if you want inside the setter or inside your init method.
                  Moreover, if you can also do your own factorybean or you can even call the keySet method on the returned map (you can do this from Spring XML).

                  Comment


                  • #10
                    Hi Costin,

                    I think I want to this but please please elaborate further i dont have a clue what you mean.

                    Code:
                    you can even call the keySet method on the returned map (you can do this from Spring XML).
                    I tried this but spring throws an exception...

                    Code:
                        private  Map exchangeCodes ;
                    
                        public Set getExchangeCodes() {
                            return exchangeCodes.keySet();
                        }
                    that the getter method is invalid since the type declared for exchangeCodes is interface Map.

                    Thanks
                    richard

                    Comment


                    • #11
                      Your setter/getter has to be of the same type with the internal field.

                      private Map exchangeCodes ;

                      public Map getExchangeCodes() {
                      return exchangeCodes;
                      }
                      You can however have a set
                      private Set exchangeCodes;

                      public Set getExchangeCodes() {
                      return exchangeCodes.keySet();
                      }
                      but then you have to transform the map into a set. You can do this by using a factory-method inside Spring xml:

                      Code:
                         <bean id="properties.factory" class="org.springframework.beans.factory.config.PropertiesFactoryBean>
                            <property name="location" value="classpath:myValues.properties"/>
                      </bean>
                      ...
                      <property name="exchangeCodes">
                      <bean factory-bean="propertiesFactory"
                            factory-method="keySet"/></property>

                      Comment


                      • #12
                        Hi Costin,

                        Thanks for all your help. I tried but cannot make it work. deadline is coming. Will come back to this some other time.

                        Regards
                        Richard

                        Comment

                        Working...
                        X