"Welcome To Ashok IT" "Spring Boot and MicroServices" Topic : Dependency Injection Using Objects Date : 27/08/2024 (Session - 11) _____________________________________________________________________________________________________________________________ Today Session : Bean Autowiring =============================== * Bean wiring is nothing but configuring the dependencies of abean along with bean in xml file. * If you manually configure bean dependencies into an xml file then the size of xml file will be increased. * Inorder to cut down the xml configuration the framework has introduced the autowiring facility. * Incase of autowiring we no need to configure the dependencies explictly in xml file based on autowire strategy the container will inject the dependencies of abean automatically. * Bean autowiring facility is only applicable for "Dependency in the form of Objects". * To add autowiring facility we need to configure "autowire" attribute with tag. * The values of autowire attribute are 1) no 2) byName 3) byType 4) constructor * Bean Autowiring facility can be achieved using two approaches 1) XML Approach 2) Annotation Approach 1) no ====== * The default value of autowire attribute is "no" it means the dependencies of abean must configured explictly, the container doesn't apply autowiring facility. 2) byName ========== * In this type of autowiring the container checks for property name of bean is matched with id of abean in xml file or not. * If bean id is matched with the property name to be injected then container calls setter injection and injects the dependency object. * If bean id is not matched with the property name then the dependency will not injected. Example: ======== public class ExampleBean1{ private ExampleBean2 exampleBean2; public void setExampleBean2(ExampleBean2 exampleBean2){ this.exampleBean2 = exampleBean2; } } spring.xml ========== 3) byType: ========== * In this type of autowire strategy the container checks for the property type is matched with Bean class configured in xml file or not. * If a bean class of xml file is matched with property type to be injected then the container calls setter injection and injects the dependency object. * If a bean class of xml file is not matched with the property type then the property will not be injected. * If a bean class of xml file is matched with more than onces with the property type then the exception will be thrown. Example: public class ExampleBean1{ private "ExampleBean2" exampleBean2; public void setExampleBean2(ExampleBean2 exampleBean2){ this.exampleBean2 = exampleBean2; } } spring.xml ========== 4) constructor: =============== * In this type of autowiring the container checks for a property type of a bean class configured in xml file are matched or not. * If abean class is matched with the property Type to be injected then the container injects the dependency object through constructor injection. * If abean class is not matched with the property Type then container throws an exception. * If more than one bean class is matched with property Type then again container throws an exception. * constructor strategy and byType strategy are common in verfication but there is differences injections. byType means setter injection and constructor means constructor injection is applied. +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++