6 Replies Latest reply on Mar 18, 2008 12:45 PM by Tom Nichols

    Passing properties via components.xml

    Tom Nichols Newbie

      Hi -- I have Seam components defined via the @Name annotation, but I want to externalize some configuration and pass in the values via components.xml,


      i.e.


      @Name("weatherBean")
      public class WeatherBean { ...
      
      }
      



      And then in components.xml...


           <component name='weatherBean'>
                <property name="requestURL">@myPT.weather.requestURL@</property>
           </component>
      



      I was hoping Seam would auto-create based on the Name annotation and then merge the configuration from components.xml given the matching name.  However this doesn't seem to work.  Do I have to choose either annotations or XML configuration?


      Thanks.

        • 1. Re: Passing properties via components.xml
          Thomas Gerhards Newbie

          I want to externalize some configuration

          If you run inside an appserver then injections via ejb-jar.xml should be used.

          • 2. Re: Passing properties via components.xml
            Tom Nichols Newbie

            Thanks for the response.  Can you give an example?

            • 3. Re: Passing properties via components.xml
              Luke Maurer Newbie

              Tom Nichols wrote on Mar 12, 2008 08:23 PM:


              I was hoping Seam would auto-create based on the Name annotation and then merge the configuration from components.xml given the matching name.  However this doesn't seem to work.  Do I have to choose either annotations or XML configuration?


              Oh, heck no - what do you think this is, Hibernate? *ducks* >:-D


              Seriously though, I've successfully followed section 4.2 of the manual before; so long as your component has a setter method for the property (in your case, setRequestURL(String url)), it should work precisely as you describe. It has to be a method, though (not just a field) - have you got one?


              (OTOH, I'd like to know how Thomas's ejb-jar.xml idea would work too ...)

              • 4. Re: Passing properties via components.xml
                Tom Nichols Newbie

                Luke Maurer wrote on Mar 12, 2008 09:45 PM:

                Seriously though, I've successfully followed section 4.2 of the manual before; so long as your component has a setter method for the property (in your case, setRequestURL(String url)), it should work precisely as you describe. It has to be a method, though (not just a field) - have you got one?


                Yes, weatherBean has a setRequestURL(String) method. 


                Ok, it seems like it is working now, I'm not sure why I wasn't seeing the desired behavior before...  I went through section 4.2 several times and there are only a couple examples of a <component> definition without a class attribute.  So it was a bit ambiguous.  There's no real example of combining both annotations and XML definition. 


                Conversely, if I define a component in components.xml, can that class use annotations like @Logger? 


                Thanks.

                • 5. Re: Passing properties via components.xml
                  Luke Maurer Newbie

                  AFAICT, there isn't a separate processing engine in Seam for annotations vs. XML (as there is in Hibernate, where annotations and XML don't (yet) play as nicely together). components.xml just augments the annotations and externalizes properties. So a component without a @Name should still have any annotations like @In, @Out, @Logger, @DataModel, etc. work just fine.

                  • 6. Re: Passing properties via components.xml
                    Tom Nichols Newbie

                    Grrr-eat.  That's good to hear.  Thanks for the clarification Luke.