4.Spring IoC : Dependency Injection

The process of injecting the data from xml to Bean Class Object.
For Example
·         we have a Student Bean, it has “Name” as String data member
·         In xml we will place Name value as “Satya Kaveti”
·         While loading the xml by the Spring Container it will wire the xml data with Bean Object

We have following data types
Primitive Data type         :  int, String, float
Secondary Data type      :  Objects, Class objects
Collections                          :  List, set, Map


In Spring Dependency Injection can do in 2 ways
·        Setter Injection

§  If we not pass any data through <property  name=” ” value=” ”> the container will create def. constructor

§  Setter method values can pass by using value attribute or <value > child tag
<beans>
       <bean id="ob" class="bo.HelloBean">
              <property name="message" value="Hello Spring!!" />
       </bean>
</beans>

§  we should not pass duplicate attribute values
<property name="message" value="Hello Spring!!" />   X
<property name="message" value="Hello Spring!!" /> x

§  we must pass name attribute
<property name="message"

·        Constructor Injection
  • we don’t have name attribute, we have to pass type – value – index if needed
  • you must pass Constructor arg, if our Bean contains parameterized  constructor
  • If not pass arg const. it won’t create even def. Constructor
  • we won’t pass same type of Constructor with same data
  • If u have overloading Constructor (same no args –diiff type) u can solve by using Type attribute
  • we can pass more than one value using this, but it's not possible in setter
  •  It any ambiguity in data type it will solve by order of Constructor
  • Constructor with more no.of params we will use INDEX attribute.
  • Overriding of index value can possible by const injection
<beans> 
<bean id="t" class="Bean">
 <constrctor-arg  value="" type="" index="">
<constrctor-arg  value="" type="" index="">
<constrctor-arg  value="" type="" index="">
  <value> </value>
</consttor>
</baen>

</beans>



Post a Comment

Thank You

Previous Post Next Post