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

Ads

Tutorials   
Spring 4 Introduction and example Features of Spring Framework 5 How to make Spring web Login form? How to make Simple form in Spring web? Spring, Hibernate login and registration application Spring Framework Tutorial for beginners with examples Spring Framework for Apache Hadoop 2.3.0 GA released Spring Framework 4.1 GA is released with major features Why to use Spring Framework? Spring Framework 4.1 - First Release candidate available Spring IO Platform 1.0.0 Released Spring 4: Login Form using Spring MVC and Hibernate Example Spring 4 MVC Login form Example with source code Spring 4 MVC Hello World Example: Spring 4 MVC Tutorial will full source code Spring Web MVC Application Error:ClassNotFoundException: DispatcherServlet on deploying Features of Spring 4 Spring Framework 4.0 released Spring Framework 4: Spring Framework 4 Tutorials and Example Spring Integration 3.0 Release is released and available for download Spring Tutorial for Beginners Java Springs Framework Tutorial Spring Architecture Spring Framework Tutorials database spring registration form Spring Login Example Roseindia Spring Tutorial Spring Tutorial Spring 3.2 MVC insert and retrieve blob from the database The hidden tag The errors tag net.roseindia.dao net.roseindia.service net.roseindia.model net.roseindia.controller Spring 3.2 MVC Hibernate Example Spring 3.2 MVC, Upload File in a specific folder Spring 3.2 MVC Form Handling The textarea tag The options tag The option tag The select tag The radiobuttons tag The radiobutton tag The checkboxes tag mvc:default-servlet-handler Static Resources Configuration View Controllers Configuration View resolvers Spring 3.2 MVC Hello World Example Content Negotiation Configuration
Ads

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="http://www.springframework.org/schema/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".

context.xml

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:p="http://www.springframework.org/schema/p"
 xsi:schemaLocation="
 http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="mybean" 
  class="Inject"
  p:name="Girish" 
  p:age="24" 
  p:address="Noida" 
  p:company="Roseindia.net" 
  p:email="[email protected]"/>
</beans>

Main.java

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.

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class Main {
  public static void main(String[] args) {
  XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
  "context.xml"));
  Inject demo = (Inject) beanFactory.getBean("mybean");
  System.out.println(demo);
  }
}

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) {
  this.company = company;
  }

 public void setEmail(String email) {
  this.email = email;
  }
  public void setAge(int age) {
  this.age = age;
  }
  public void setName(String name) {
  this.name = name;
  }

  @Override
  public String toString() {
  return String.format("Name: %s\n" +
  "Age: %d\n" +
  "Address: %s\n" +
  "Company: %s\n" +
  "E-mail: %s",
  this.name, this.age, this.address, this.company, this.email);
  }
}

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
Company: Roseindia.net
E-mail: [email protected]
BUILD SUCCESSFUL (total time: 1 second)

Download source code

Advertisements

Share on Google+Share on Google+

Spring Injection Example

Posted on: May 22, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Spring Injection Example   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
Steve White
April 17, 2011
libraries needed to build and run

build: /usr/share/java/spring-core.jar:/usr/share/java/spring-beans.jar run: :/usr/share/java/spring-core.jar:/usr/share/java/spring-beans.jar:/usr/share/java/commons-logging.jar Also, a log4j.properties file is required somewhere in the run path. I used log4j.rootLogger=ERROR, A1 log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout #Prevent internal log4j DEBUG messages from polluting the output. log4j.logger.org.apache.log4j.PropertyConfigurator=ERROR log4j.logger.org.apache.log4j.config.PropertySetter=ERROR log4j.logger.org.apache.log4j.FileAppender=ERROR
master
October 31, 2011
attuneinfocom.com

good post
Xuan Toan
December 6, 2011
How will we run Spring Injection project

How will we run Spring Injection project?
Rajpal Singh
January 4, 2013
SpringLooselyCoupled

Hello Sir, Can you explain Spring example comparing with normal code and how it is loosely coupled. Suppose,when the code changes in any file how it will not effect other file in spring....please explain with example. Rajpal Singh.

Ads

 

Ads