Results 1 to 7 of 7

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

  1. #1

    Default 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. #2
    Join Date
    Mar 2007
    Posts
    515

    Default

    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 ?

  3. #3

    Default

    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.

  4. #4
    Join Date
    Mar 2007
    Posts
    515

    Default

    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.

  5. #5

    Default

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

  6. #6
    Join Date
    Aug 2006
    Location
    Now Germany, previously Ukraine
    Posts
    1,546

    Default

    Quote 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.

  7. #7
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    Check chapter 3 of the reference guide and also take a look at the samples shipped with Spring.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

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