Câblage automatique à ressort par AutoDetect
Au printemps, «Autowiring by AutoDetect», signifie choisit «http://www.example.com/spring/spring-autowiring-by-constructor/[autowire by constructor]» si le constructeur par défaut (argument avec n'importe quel type de données) , sinon utilise «http://www.example.com/spring/spring-autowiring-by-type/[autowire by type]».
Voir un exemple de Spring «câblage automatique par détection automatique». Câblage automatique du haricot «kungfu» en «panda», via le constructeur ou le type (basé sur la mise en œuvre du haricot panda).
1. AutoDetect - par le constructeur
Si un constructeur par défaut est fourni, la détection automatique choisit le fil par constructeur.
package com.example.common; public class Panda { private KungFu kungfu; public Panda(KungFu kungfu) { System.out.println("autowiring by constructor"); this.kungfu = kungfu; } public KungFu getKungfu() { return kungfu; } public void setKungfu(KungFu kungfu) { System.out.println("autowiring by type"); this.kungfu = kungfu; } //... }
Sortie
autowiring by constructor Person [kungfu=Language [name=Shao lin]]
2. AutoDetect - par type
Si aucun constructeur par défaut n'est trouvé, la détection automatique choisit le fil par type.
package com.example.common; public class Panda { private KungFu kungfu; public KungFu getKungfu() { return kungfu; } public void setKungfu(KungFu kungfu) { System.out.println("autowiring by type"); this.kungfu = kungfu; } //... }
Sortie
autowiring by type Person [kungfu=Language [name=Shao lin]]
Télécharger le code source
Téléchargez-le -Spring-AutoWiring-by-Auto-Detect-Example.zip (6 Ko)