Big Data and Hadoop Training @$80 or Rs. 6,000 only - valid till Oct 17th Apply Now!

Spring Injection Example


Spring Injection Example

Spring Injection Example

Spring Injection Example

Spring Injection Example


XML Bean-Injection, The given example below gives the brief description of the extension name spaces that can be used in the spring framework With the use of extensible name space you can  make Spring configuration file more simpler to use for many tasks which can occurs repeatedly . Extension namespaces also allows us to supply property values by using attributes .

xmlns:p="":-Here we have introduced  namespace:"p" that cannot be validated.

<bean id="mybean"  class="Inject">:- Here "Inject" is the name of the bean class which would be referred in the xml file with the id "mybean".

p:name="Girish" :-Here we simply use the property name in the "p" namespace, as in "p:name".


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
  <bean id="mybean" 
  p:email="[email protected]"/>

This is the file through which we are retrieving the property of the bean defined above. Some of the methods which are used here are as follows:-

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("context.xml")):-This method creates an instance of the XmlBeanFactory which is used to read bean definition from an XML document

new ClassPathResource("context.xml"):-
Creates a new ClassPathResource for ClassLoader .Here the context.xml is the file which is to be loaded.

String.format("Name: %s\n" +"Age: %d\n" ):-This method formats an object to produce a string. Here

%s:-is for accessing String value.

%d:-is for accessing Integer value.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
import org.springframework.beans.factory.xml.XmlBeanFactory;

public class Main {

  public static void main(String[] args) {
 XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
  Inject demo = (Inject) beanFactory.getBean("mybean");

class Inject {
  private String name;
  private int age;
  private String company;
  private String email;
  private String address;

  public void setAddress(String address) {
  this.address = address;

  public void setCompany(String company) { = company;

  public void setEmail(String email) { = email;

  public void setAge(int age) {
  this.age = age;

  public void setName(String name) { = name;

  public String toString() {
  return String.format("Name: %s\n" +
  "Age: %d\n" +
  "Address: %s\n" +
  "Company: %s\n" +
  "E-mail: %s",, this.age, this.address,,;
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
	<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
	<property name="url" value="jdbc:mysql://"/>
	<property name="username" value="root"/>
	<property name="password" value="root"/>
<bean id="lobHandler" class="">
	<property name="nativeJdbcExtractor" ref="nativeJdbcExtractor"/>
<bean id="nativeJdbcExtractor" 
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
	<property name="dataSource" ref="dataSource"/>

Output of the Spring Injection Example program

Nov 25, 2008 1:03:13 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [context.xml]
Name: Girish
Age: 24
Address: Noida
E-mail: [email protected]
BUILD SUCCESSFUL (total time: 1 second)

Download source code