Announcement Announcement Module
Collapse
No announcement yet.
Why is a default constructor needed for injection Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why is a default constructor needed for injection

    Hello,

    I'm new to the spring framework and I've got a question:
    When I want to use Dependency Injection, why is it so important that the classes whose properties get injected have a default constructor? I can understand that setters are needed, but I don't know why there must be a default constructor, because aspects should be able to wrap around any constructor, not just the default ones (in AspectJ, you can use wildcards for parameter lists and so on).

    Is there any special reason? Or does some way exist to use Dependency Injection with a class that has only parameterized constructors? In my case, there are some classes where it wouldn't be a good thing to offer default constructors.

    Thanks in advance
    ZeHa

  • #2
    Is constructor injection an option?

    I think a constructor is needed to instantiate the class before injecting any values. All Spring does is creating an instance, therefore it needs a constructor - please correct me if I am wrong.

    Comment


    • #3
      Well, constructor injection would be as unpretty as having a default constructor in my case :/

      But I wonder why Spring is instantiating my classes. I thought it just wraps around the constructor using AOP, and everytime the constructor is called, the injections will be made.

      Comment


      • #4
        The way I thought Spring is working is - it creates an instance of the class according to your bean definition. Inside the framework I suspect that Spring does the same as you would do

        Code:
        new MyClass();
        And therefore it would need a constructor in some way. Another thing is that I'm not sure if Spring is actually using AOP for the dependency injection.

        I started working with Spring a couple of weeks ago so please forive any misunderstandings.

        Cheers

        Comment


        • #5
          If you don't want to use constructor injection spring uses the default constructor because that is the only way to create an instance because you haven't defined explicitly which constructor to use. If you have a class with a 2 argument constructor how should spring instantiate the class if you haven't told it to use 2 arguments (instead of setters) do some guess work, add null, something else???

          Originally posted by brixtonasias
          And therefore it would need a constructor in some way. Another thing is that I'm not sure if Spring is actually using AOP for the dependency injection.
          It doesn't use AOP it just used the BeanDefinition (which is the recipe of creating a bean instance).

          Originally posted by Zeha
          But I wonder why Spring is instantiating my classes. I thought it just wraps around the constructor using AOP, and everytime the constructor is called, the injections will be made.
          I suggest you get the basic of the Spring workings straight first. Read chapter 3 of the reference guide, which explains how spring works.

          Comment


          • #6
            Classiccoder.com

            Hi I think it is not compulsary to deifine public constructor in case of setter injection because java by default provide the no argument constructor. Check following link for this type of example:www.classiccoder.com

            Comment


            • #7
              No public constructor required

              Just a quick follow on.

              It is true you need a default contructor so Spring can instantiate it.

              It does not however have to be a public contructor.

              This is useful if you want to prevent any programmer from instantiation the default contructor.

              Comment

              Working...
              X