Announcement Announcement Module
Collapse
No announcement yet.
How to set a property without a setter Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to set a property without a setter

    Hi all,
    I've just started to develop an application with Spring and I would like to set up an embedded Jettyserver as a Spring-bean.

    Instantiating the server will go this way:
    org.mortbay.jetty.Server server = new Server();
    server.addListener(":1234"); // defines the port

    The problem now is, that the required method isn't called setListener("myport"), but addListener("myPort") so that creating the bean allways fails :o(

    I've got this problem also with other objects and so it would be nice if somebody has an idea how to solve this problem in a proper way.

    TIA
    items

  • #2
    ... solve this problem in a proper way.
    without wrapping the Serverobject ;o)

    Comment


    • #3
      Hi

      The Spring way is to create a small FactoryBean implementation that will do the instantiation for you.

      Code:
      public class JettyServerFactoryBean extends AbstractFactoryBean {
      
          private String listenerPort;
      
          // setter for the listenerPort instance variable
      
          protected Object createInstance() {
      
      org.mortbay.jetty.Server server = new org.mortbay.jetty.Server();
      server.addListener(this.listenerPort); // defines the port
      return server;
      
          }
      
      }
      I appreciate that this is not a whole lot to go on (just on my way out the office), so ping the forum back if this is not what you want/need/etc.

      Cheers
      Rick

      Comment


      • #4
        thats correct, creating a server object is the easiest way.
        In an app context I had:
        Code:
            <bean id="httpServer" class="some.package.server.JettyServer" init-method="init"
                destroy-method="stop">
        And, the server class would wire it up:
        Code:
            public void init() throws IOException, IllegalAccessException, Exception {
                log.info("init of JettyServer");
                log.info("Adding socket listener: " + socketListener);
                httpServer.addListener(socketListener);
                log.info("Adding HTTP context: " + httpContext);
        and so forth...
        }
        But, it would be nice if there were already a Jetty server bean. And, an easier way of setting non-javabean properties.

        Comment


        • #5
          I did not get to see Rick's response before I posted. Thats the best way to go ...

          Still, a method of setting non-javabean properties would be interesting.

          Comment


          • #6
            When i checked out Jetty's javadoc, the Server takes a port number as a constructor arg. You can then use Spring' constructor injection to pass in the port number.

            Also addXXX is usually (but not always) accompanied by setXXX(XXX[]) methods which can be used for setter injection through Spring.
            Last edited by sabarish; Aug 25th, 2006, 01:31 PM.

            Comment


            • #7
              Hi,
              thanks for your answers.
              It helps a lot for my work and also to understand Spring a little bit better.

              hen i checked out Jetty's javadoc, the Server takes a port number as a constructor arg. You can then use Spring' constructor injection to pass in the port number.
              Yes. But like jbetancourt said. It would be great to have an attribute for describing the method's name if it doesn't start with set.

              bye
              items

              Comment


              • #8
                Yea, something like a factory-method:

                Code:
                <property method="addListener" value=xxxx"/>
                
                or
                
                <property method="addListener" ref=beanx"/>
                However, its not very JavaBean spec-ish.

                Comment


                • #9
                  How to test the factory with JUnit

                  Hi again,
                  everything works great and it becomes more and more interesting to work with Spring, but some questions are still open:
                  How is it possible to test my factory without loading the whole application? My "createInstance()" method is protected because my class is derived from AbstractFactoryBean. And how creates Spring the Serverbean without calling a public factory method when getObject() returns null?

                  Thanks again
                  items

                  Comment

                  Working...
                  X