Results 1 to 7 of 7

Thread: Why is a default constructor needed for injection

  1. #1
    Join Date
    Feb 2009
    Posts
    3

    Question 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. #2

    Default

    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.
    Use the [ code ] tags, please! See how: http://www.screencast.com/users/brix...5-ba932d9e5b51

    - brixtonasias -

  3. #3
    Join Date
    Feb 2009
    Posts
    3

    Default

    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.

  4. #4

    Default

    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
    Use the [ code ] tags, please! See how: http://www.screencast.com/users/brix...5-ba932d9e5b51

    - brixtonasias -

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

    Default

    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???

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

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

  6. #6
    Join Date
    Jun 2009
    Location
    WWW.classiccoder.com\Tutorials\Spring
    Posts
    2

    Smile 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

  7. #7
    Join Date
    Feb 2011
    Posts
    1

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

Posting Permissions

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