Câblage automatique du ressort 'byType'

Ce mode spécifie le câblage automatique par type de propriété. Le conteneur Spring examine les beans sur lesquels l' attribut autowire est défini sur byType dans le fichier de configuration XML. Il essaie ensuite de faire correspondre et de câbler une propriété si sontypecorrespond 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 haricot est définie sur autowire byType dans le fichier de configuration, et il contient un correcteur orthographique propriété de SpellChecker type Spring cherche une définition de haricot nommé SpellChecker , et l' utilise pour définir la propriété. Vous pouvez toujours câbler les propriétés restantes à l'aide des balises <property>. L'exemple suivant illustrera le concept où vous ne trouverez aucune différence avec l'exemple ci-dessus, sauf que le fichier de configuration XML a été modifié.

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 void setSpellChecker( SpellChecker spellChecker ) {
      this.spellChecker = spellChecker;
   }
   public SpellChecker getSpellChecker() {
      return spellChecker;
   }
   public void setName(String name) {
      this.name = name;
   }
   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">
      <property name = "spellChecker" ref = "spellChecker" />
      <property name = "name" value = "Generic Text Editor" />
   </bean>

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

</beans>

Mais si vous allez utiliser le câblage automatique 'byType', 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 = "byType">
      <property name = "name" 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.
Impression