Enabling dependencies with autwire = "constructor" when multiple constructors are present?

I have a text editor class with below constructors

 public class TextEditor {
       private SpellChecker spellChecker;

       private SpellChecker1 spellChecker1;

       private SpellChecker2 spellChecker2;

     public TextEditor(SpellChecker spellChecker) {
          this.spellChecker = spellChecker;
        }

       public TextEditor(SpellChecker2 spellChecker2) {
              this.spellChecker2 = spellChecker2;
           }

       public TextEditor(SpellChecker spellChecker, SpellChecker1 spellChecker1,SpellChecker2 spellChecker2) {
              this.spellChecker = spellChecker;
              this.spellChecker1 = spellChecker1;
              this.spellChecker2 = spellChecker2;
           }

       public TextEditor(SpellChecker spellChecker, SpellChecker1 spellChecker1) {
              this.spellChecker = spellChecker;
              this.spellChecker1 = spellChecker1;
           }
        }

In spring - beans I have

<bean id="textEditor" class="com.TextEditor" autowire="constructor">
</bean>

what I observe, a constructor with two arguments is called sequentially. Is this a coincidence? Spring throw exception becoz should not follow, it does not know which constructor should be called?

+4
source share
2 answers

This is a consequence of the way autwires Spring constructors.

, - bean , , . -.

, BeanFactory. , bean - , . , - , ( , ..). , .

-, Spring .

, Spring 2 arg 3 arg, , bean arg 3.

+5

, Employee, , bean xml.                           

<bean id="qualification" class="com.Autowiring.constructor.Qualification">
    <property name="highestQualification" value="BTech"/>
    <property name="certifcations" value="SCJP"/>
</bean>

<bean name="address" class="com.Autowiring.constructor.Address">
    <property name="city" value="Bangalore" />
    <property name="zip" value="560054" />
    <property name="building" value="Building One" />
</bean>

4 . , , , , , id, name, dob, address qualification.

// - 1

public Employee(Address address, Qualification qualification) {
    super();
    System.out.println(1);
    this.address = address;
    this.qualification = qualification;
}

// - 2

public Employee(Qualification qualification) {
    super();
    System.out.println(2);
    this.qualification = qualification;
}

// - 3

public Employee(Address address) {
    super();
    System.out.println(3);
    this.address = address;
}

// - 4

public Employee(int id, String name, Date dob, Address address,
        Qualification qualification) {

    super();
    System.out.println(4);
    this.id = id;
    this.name = name;
    this.dob = dob;
    this.address = address;
    this.qualification = qualification;
}

Case-1: , 1 4 , -arg bean.

: 3 , , 2 3.

Case-2: , 4 , -arg bean.

. 1, , bean, 1.

Case-3. , -arg bean.

. 1, , bean, 1, 4- , id, name dob bean, - , , bean.

Case-4. , -arg employee bean, .

. 4- , , , dob, bean, 1- arg bean, 4- , , , 4- . : , spring , , .

0

Source: https://habr.com/ru/post/1527876/


All Articles