Spring Bean Injection Using Xml File

                                                            Index

1. Usage of Bean Injection
2. Configuration xml  file
3. Container class
4. Injected class
5. Maven dependencies


Project structure is:

                SpringBeanExample
                      --src/main/java
                              --com.example.springexample.xmlautowired
                                       * AppAutoWiredFromXml.java
                                       * CurrencyService.java
                                       * FooService.java
                        --src/main/resources
                                 *spring-config.xml



1. Usage of Bean Injection ( AppAutoWiredFromXml.java )

package com.example.springexample.xmlautowired;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AppAutoWiredFromXml {

     public static void main(String[] args) {
          ApplicationContext context = 
                 new ClassPathXmlApplicationContext("spring-config.xml");
  
          FooService bean = (FooService) context.getBean("fooService");
  
          bean.method();
     }
}




2. Configuration xml  file ( spring-config.xml )

<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

     <bean id="fooService" class="com.example.springexample.xmlautowired.FooService">
          <property name="currencyServiceBean" ref="currencyServiceBean" />
     </bean>
     <bean id="currencyServiceBean"  
          class="com.example.springexample.xmlautowired.CurrencyService" />
</beans>




3. Container class ( FooService.java )
 

package com.example.springexample.xmlautowired;

import org.springframework.beans.factory.annotation.Autowired;

public class FooService {

    @Autowired
     CurrencyService currencyServiceBean;
 
     public void method(){
          currencyServiceBean.convert(10.0);
     }

     public CurrencyService getCurrencyServiceBean() {
          return currencyServiceBean;
     }

     public void setCurrencyServiceBean(CurrencyService currencyServiceBean) {
          this.currencyServiceBean = currencyServiceBean;
     }
}



4. Injected class ( CurrencyService.java )
 

package com.example.springexample.xmlautowired;


public class CurrencyService {

     public void convert(double value){
          System.out.println("value: " + 4.4 *value);
     }
}





5. Maven dependencies(pom.xml)



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

    ...

     <properties>
         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
         <maven.compiler.source>1.7</maven.compiler.source>
         <maven.compiler.target>1.7</maven.compiler.target>
         <spring.version>3.0.2.RELEASE</spring.version>
     </properties>

       <dependencies>
        ....
           <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>${spring.version}</version>
           </dependency>

           <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
           </dependency>
      .....
 
      </dependencies>
</project>