Properties Injection
Spring Inversion of Control (IoC ) also known as
Dependency Injection (DI ) is a process by which
objects define their dependencies with collaborating objects.
The following example shows properties injection.
01
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
07
<
bean
id
=
"complexBean"
class
=
"com.bethecoder.tutorials.spring3.basic.ComplexBean"
>
08
<
property
name
=
"simpleProps"
>
10
<
prop
key
=
"in"
>INDIA</
prop
>
11
<
prop
key
=
"us"
>AMERICA</
prop
>
12
<
prop
key
=
"uk"
>UNITED KINGDOM</
prop
>
17
<
bean
id
=
"complexBean2"
class
=
"com.bethecoder.tutorials.spring3.basic.ComplexBean"
>
18
<
property
name
=
"simpleProps"
>
package com.bethecoder.tutorials.spring3.tests.collections;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import com.bethecoder.tutorials.spring3.basic.ComplexBean;
public class PropertiesInjection {
/**
* @param args
*/
public static void main ( String [] args ) {
XmlBeanFactory factory = new XmlBeanFactory (
new ClassPathResource ( "properties_inj.xml" )) ;
ComplexBean complexBean = ( ComplexBean ) factory.getBean ( "complexBean" ) ;
System.out.println ( complexBean.getSimpleProps ()) ;
ComplexBean complexBean2 = ( ComplexBean ) factory.getBean ( "complexBean2" ) ;
System.out.println ( complexBean2.getSimpleProps ()) ;
}
}
It gives the following output,
{in=INDIA, uk=UNITED KINGDOM, us=AMERICA}
{in=INDIA, fr=FRANCE, uk=UNITED KINGDOM, us=AMERICA, cn=CHINA}