Announcement Announcement Module
No announcement yet.
setter injection Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • setter injection

    Hi guys,

    I'm new to spring, looks great though. I am using setter injection to get a DAO instantiated in my service. When the web server starts up the setter is called with a fresh new instance of the DAO and it is assigned to the local variable. But when I later call my service the DAO object is null. I am 100% percent sure the DAO is injected as I placed a watch on the local variable.

    The only thing I can think, and I'm not even sure if the Garbage Collector will work like this, is that the setter injection creates the DAO and it's passed by reference to the service - but then the DAO is made null or Garbage Collected and since it is passed by reference all the references now point to null.

    Any ideas?

  • #2
    You are using another instance of that bean, which is either configured loaded by spring or you are creating a new instance yourself.


    • #3
      I figured out what was wrong

      I have a servlet mapper for actions called actions-servlet.xml. In there I have the bean definition for my form controller. But my definition for the service, that passes the DAO by setter injection is defined in applicationContext.xml.

      Is there a way to reference the service in applicationContext.xml from the actions-servlet.xml?


      • #4
        By simply referencing it.. I suggest a read of the reference guide (the web chapter) which explains all this.


        • #5
          This is true, but what if the bean I need to reference exists in a different xml file?


          • #6
            As I already stated, simply do it... It doesn't matter in which file it is defined... Again read the reference guide which explains all this (also in the case with parent/child contexts).


            • #7
              More specifically:

              As detailed in Section 3.13, “Additional Capabilities of the ApplicationContext”, ApplicationContext instances in Spring can be scoped. In the Web MVC framework, each DispatcherServlet has its own WebApplicationContext, which inherits all the beans already defined in the root WebApplicationContext. These inherited beans can be overridden in the servlet-specific scope, and you can define new scope-specific beans local to a given servlet instance.
              This will work and if you have any problems, send over any errors you are getting if you can't figure them out yourself.

              Check out the source of the GreenHouse application as a good reference as well: