Java & J2EE Page 3 - The Spring Framework: Understanding IoC |
The Setter Injection is the most commonly used form of Dependency Injection in the Spring Framework. To tell Spring that the application wants to use a Setter Injection, the declaration needs to be made in the configuration file. Just as with Constructor Injection, the declaration is done in the beans section. The steps involve declaring the properties of the POJO and passing the value to the property. The properties of a POJO are its instance variables. To declare the attributes, the <property> element is used. This element is the child element of the <bean> element. The value of the name attribute of <property> accepts the name of a property of the POJO declared using the <bean> element. For example, to declare the userName property of the GreetingBean, the code would be: <bean id=”GreetingBean” class=”org.me.GreetingBean”> <property name=”username”> : : </property> </bean>
To pass the desired value to the property, the <value> element needs to be used. It is the child element of <property>. For example, to pass the value “Raj” to the property userName, the code would be: <bean id=”GreetingBean” class=”org.me.GreetingBean”> <property name=”username”> <value>Raj</value> </property> </bean>
That’s how Setter Injection is used in Spring Framework. Finally, the Spring Framework does not support Interface Injection. However, using an auto-wiring service, one can make use of Interface Injection. How to use it will be discussed in the future, when I cover auto-wiring in detail. That completes this section. In the next section will look at an application that will make use of Constructor Injection.
blog comments powered by Disqus |
|
|
|
|
|
|
|