Announcement Announcement Module
Collapse
No announcement yet.
Any way to bind beans automatically instead of having to get context? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Any way to bind beans automatically instead of having to get context?

    I have a basic Spring applicationContext.xml defined bean attached to a class. However, those bean properties are not bound to any class objects that are created unless I create them like so:

    Code:
    MyObject mO = (MyObject) appContext.getBean("MyObjectBean");
    My question is, since this object will always need to be binded to the Spring bean, is there any way to configure Spring to do this automatically?

  • #2
    I'm not sure I understand what is the issue...
    You want your beans to be automatically instantiated in a certain class, without the need to access application context and retrieve the bean manually ?

    Comment


    • #3
      Exactly. The application I am working on will be binding many objects to beans, and I foresee the potential for many errors caused by improper/lack of retrieving the beans.

      Comment


      • #4
        Wouldn't simple setter injection do the trick ?
        Define whatever beans you want to use in your class, for each define a set method() and in your context file make necessary links between your class and needed beans.

        Comment


        • #5
          Pretend I'm new to Spring's application context altogether. ;-) Can you show me an example of what you mean?

          Comment


          • #6
            Originally posted by xaeryan View Post
            Pretend I'm new to Spring's application context altogether. ;-) Can you show me an example of what you mean?
            Red the documentation, especially, chapter 3 in the Spring reference.

            Comment


            • #7
              Check chapter 3 of the reference guide and also take a look at the samples shipped with Spring.

              Comment

              Working...
              X