Autowiring de ressort par le constructeur

Ce mode est très similaire à byType , mais il s'applique aux arguments du constructeur. Le conteneur Spring examine les beans sur lesquels l' attribut autowire est défini comme constructeur dans le fichier de configuration XML. Il essaie ensuite de faire correspondre et de câbler l'argument de son constructeur avec exactement l'un des noms de beans dans le fichier de configuration. Si des correspondances sont trouvées, il injectera ces beans. Sinon, les haricots ne seront pas câblés.

Par exemple, si une définition de bean est définie sur autowire par le constructeur dans le fichier de configuration et qu'elle a un constructeur avec l'un des arguments de type SpellChecker , Spring recherche une définition de bean nommée SpellChecker et l'utilise pour définir l'argument du constructeur. Vous pouvez toujours câbler les arguments restants en utilisant les balises <constructor-arg>. L'exemple suivant illustrera le concept.

Laissez-nous mettre en place un IDE Eclipse fonctionnel et suivez les étapes suivantes pour créer une application Spring -

Pas La description
1 Créez un projet avec un nom SpringExample et créez un package com.tutorialspoint sous lesrc dossier dans le projet créé.
2 Ajoutez les bibliothèques Spring requises à l'aide de l' option Ajouter des JAR externes comme expliqué dans le chapitre Exemple de Spring Hello World .
3 Créez les classes Java TextEditor , SpellChecker et MainApp sous le package com.tutorialspoint .
4 Créez le fichier de configuration Beans Beans.xml sous lesrc dossier.
5 La dernière étape consiste à créer le contenu de tous les fichiers Java et le fichier de configuration Bean et à exécuter l'application comme expliqué ci-dessous.

Voici le contenu de TextEditor.java fichier -

package com.tutorialspoint;

public class TextEditor {
   private SpellChecker spellChecker;
   private String name;

   public TextEditor( SpellChecker spellChecker, String name ) {
      this.spellChecker = spellChecker;
      this.name = name;
   }
   public SpellChecker getSpellChecker() {
      return spellChecker;
   }
   public String getName() {
      return name;
   }
   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}

Voici le contenu d'un autre fichier de classe dépendant SpellChecker.java -

package com.tutorialspoint;

public class SpellChecker {
   public SpellChecker(){
      System.out.println("Inside SpellChecker constructor." );
   }
   public void checkSpelling(){
      System.out.println("Inside checkSpelling." );
   }
}

Voici le contenu de la MainApp.java fichier -

package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
      TextEditor te = (TextEditor) context.getBean("textEditor");
      te.spellCheck();
   }
}

Voici le fichier de configuration Beans.xml en état normal -

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- Definition for textEditor bean -->
   <bean id = "textEditor" class = "com.tutorialspoint.TextEditor">
      <constructor-arg  ref = "spellChecker" />
      <constructor-arg  value = "Generic Text Editor"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id = "spellChecker" class = "com.tutorialspoint.SpellChecker"></bean>
</beans>

Mais si vous allez utiliser l'autowiring 'par constructeur', alors votre fichier de configuration XML deviendra comme suit -

<?xml version = "1.0" encoding = "UTF-8"?>

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <!-- Definition for textEditor bean -->
   <bean id = "textEditor" class = "com.tutorialspoint.TextEditor" 
      autowire = "constructor">
      <constructor-arg value = "Generic Text Editor"/>
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id = "SpellChecker" class = "com.tutorialspoint.SpellChecker"></bean>

</beans>

Une fois que vous avez terminé de créer les fichiers de configuration source et bean, laissez-nous exécuter l'application. Si tout va bien avec votre application, elle imprimera le message suivant -

Inside SpellChecker constructor.
Inside checkSpelling.