JaiswalTraining

Get the online training

Corejava Servlet Jsp  Php  Hibernate  Ajax Web Service   Ejb2.1 Ejb3.0 Struts Struts2  JPA Spring Ibatis   JSF    JSF2.0  CoreJavaDesignPattern    Jquery  Flex J2EE-Design-Patterns  Jboss7  Maven  Contact Me                                                                                                                                                                        
            IGNOU SMU PTU Project                                           Training                                                                                                                              
              

Contact Us  0091- 9210721663         jaiswaltraining@gmail.com




       Corejava
Servlet
Jsp
Php
Hibernate
Ajax
Web Service
Ejb2.1
Ejb3.0
Struts  
Struts2
JPA
Spring
Ibatis
JSF
JSF2.0
CoreJavaDesignPattern
Jquery
Flex
J2EE-Design-Patterns
Jboss7
Maven








 

Spring Core Module

Bean definition file
  • Objects whose implementations are likely to change are defined in XML file. Java code does not need to refer to
    any specific implementation
    • You use the <bean> tag to define object’s name and class
    • You use nested <property> or <constructor-arg> elements to give startup values to the object
  • Container based on bean definition file
    • BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource(“applicationcontext.xml");
  • You get object instances from the container
    • (InterfaceType)beanFactory.getBean("bean-name")

Dependency Injection OR IOC (Inversion of Control )
  • A technique that allows object configuration to be moved out of code and into a configuration file.
  • A way of sorting out dependencies between objects and automatically “injecting”
     references to collaborating objects on demand.
IOC Basic
  • The org.springframework.beans and org.springframework.context packages provide the basis for the Spring Framework's IoC container.
  • The BeanFactory interface provides an advanced configuration mechanism capable of managing objects of any nature. 
  • The most commonly used BeanFactory implementation is the XmlBeanFactory class.
  • This implementation allows you to express the objects that compose your application, and interdependencies between such objects,
  • in terms of XML. The XmlBeanFactory takes this XML configuration metadata and uses it to create a fully configured system or application
Example:-

class Fruits {
    Apple a = new Apple();
}

Config.xml

<bean id=“apple” class=“com.mypack.Apple”/>
<bean id =“fruits” class=“com.mypack.Fruits”>
    <property name=“appleobj” ref=“apple”/>
</bean>

It is Implemented in three Ways...
  • Setter injection
  •  Constructor injection  
  •  Auto-wiring 


Basic Steps To Do.....

  • Write Java Bean classes, with get and set methods for each property to be configured by Spring. These can optionally implement interfaces.
  • Create an XML file(applicationcontext.xml) the describes the Java Beans to be configured. Beans can also be described using a Java property file, but using XML is more common.
  • Use an XMLBeanFactory to create configured beans. There are other ways to read bean descriptions, but using an XmlBeanFactory is more common.

applicationcontext.xml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC
 
"-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"
  
      <beans> 
    
  <bean id="exampleBean" class="examples.ExampleBean"
    <!-- setter injection using the nested <ref/> element --> 
    <property name="beanOne"><ref bean="anotherExampleBean"/></property> 
    <!-- setter injection using the neater 'ref' attribute --> 
    <property name="beanTwo" ref="yetAnotherBean"/> 
   </bean> 
  <bean id="anotherExampleBean" class="examples.AnotherBean"/> 
  <bean id="yetAnotherBean" class="examples.YetAnotherBean"/>       

<bean id="exampleBean" class="examples.ExampleBean">
     <constructor-arg ref="anotherExampleBean"/> 
    <constructor-arg ref="yetAnotherBean"/>
   </bean> 
<bean id="anotherExampleBean" class="examples.AnotherBean"/> 
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/> 

</beans> 


BeanFactory

import org.springframework.beans.factory.BeanFactory; 
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource; 
  
   BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource
(
“applicationcontext.xml"));
   MyBeanInterface bean = (MyBeanInterface)beanFactory.getBean("myBeanId"); 

First way:-
 
Setter Injection


                It can be done in two ways 
    • Through Values
    • Through Reference
Through values
  • In this case we provide the values either in
    • beans.xml or in
    • bean file.
Through Reference
  • In this case reference is provided in place of value.
  • This is done with help of attribute ref in beans.xml.
  • Reference of component  is  given.

Java Class

public class ExampleBean {

private 
AnotherBean beanOne; 
private YetAnotherBean beanTwo; 

public void setBeanOne(AnotherBean beanOne
{
     this.beanOne = beanOne; 
        }
     public void setBeanTwo(YetAnotherBean beanTwo)
        {        
     this.beanTwo = beanTwo;     
   
}
  }
        


beans.xml

<bean id="exampleBean" class="examples.ExampleBean"
    <!-- setter injection using the nested <ref/> element --> 
    <property name="beanOne"><ref bean="anotherExampleBean"/></property> 
    <!-- setter injection using the neater 'ref' attribute --> 
    <property name="beanTwo" ref="yetAnotherBean"/> 
   </bean> 
  <bean id="anotherExampleBean" class="examples.AnotherBean"/> 
  <bean id="yetAnotherBean" class="examples.YetAnotherBean"/> 


Second Way:-

Constructor injection
                   
                It can be done in two ways 
    • Through Values
    • Through Reference
Through values
  • In this case we provide the values either in
    • beans.xml or in
    • bean file.
Through Reference
  • In this case reference is provided in place of value.
  • This is done with help of attribute ref in beans.xml.
  • Reference of component  is  given.


Java Class

public class ExampleBean 
    private AnotherBean beanOne; 
    private YetAnotherBean beanTwo;
  public ExampleBeanAnotherBean anotherBean, YetAnotherBean yetAnotherBean)
   
    this.beanOne = anotherBean;
     this.beanTwo = yetAnotherBean; 
  } } 


beans.xml file

<bean id="exampleBean" class="examples.ExampleBean">
   <constructor-arg ref="anotherExampleBean"/>
   <constructor-arg ref="yetAnotherBean"/> 
</bean> 
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
 <bean id="yetAnotherBean" class="examples.YetAnotherBean"/> 


Example:-Using Setter Injection using values.

A Simple display of text  "Hello World".. with Eclipse.
  • click on New----->Project

 

  • Select Java Project


  • Give it  any name 
  • Here we are  giving it SpringJavaApplication



  • Then Right click on Project------->build  path--->configure build path
  • thern click on Add External jar
  • Add all necessory Spring jar files
  • click on ok.





  • Create a new class fie.
  • Name it as Hello.java.




  • Add the package name as mypack.
  • Click on the finish button  as shown below.

HelloWorld.java



package mypack;

public class HelloWorld {

  private String message;

    public HelloWorld() {
    System.out.println("Object of HelloWorld created.");
    }

  public void setMessage(String message) {
    this.message = message;
  }

  public void display() {
    System.out.println(message);
  }
}
  • Right click on src----------> New------>others


  • Select xml--->xml file
  • give it any name  Springconfiguration.xml



Springconfiguration.xml

Setting values here for setter method


<?xml version="1.0" encoding="UTF-8"?>

<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.xsd"
>


<bean id="helloWorld" class="mypack.HelloWorld">
  <property name="message" value="Hello World!"></property>
</bean>

</beans>

Craete another class file named HelloWorldApp.java in mypack folder

HelloWorldApp.java

package mypack;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.core.io.FileSystemResource;

public class HelloWorldApp {

  public static void main(String[] args) {

   private static XmlBeanFactory beanfactory;
    public static void main(String[] args) {
        Resource file = new FileSystemResource("src/Springconfiguration.xml");
        System.out.println("Spring Configuration File Found");
        beanfactory = new XmlBeanFactory(file);
        System.out.println("Bean factory created");


        HelloWorld helloWorld = (HelloWorld) beanfactory.getBean("helloWorld");
        helloWorld.display();

}

Directory Structor will be




Run  the project
  • right click on project
  • click on run as------>Java Application


Output




Download Zip Code

Example:-Construtor Injection using Values






  • Create a new class fie named customer as shown below.

customer.java
package mypack;

public class customer {
  private int id;
  private String name;
 
  public customer(int id, String name) {
  this.name=name;
  this.id=id;
  }
  
  
  public String display(){
    String infor="id is "+id+"\nname is"+name;
    return infor;
  }
}

Create a Program class

Program.java

package mypack;

import myutility.Utility;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;

public class Program {

  public static void main(String[] args) {
    BeanFactory beanFactory =(BeanFactory)Utility.getBeanFactory()

    customer cus = (customerbeanFactory.getBean("cs");
    System.out.println(cus.display());
  }

}

Create Springconfiguration.xml file

<?xml version="1.0" encoding="UTF-8"?>
<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.xsd"
>
  
  <bean id="cs" class="mypack.customer">
    <constructor-arg  value="102" type="int" />
    <constructor-arg  value="Shankar"
      type="java.lang.String" />
  </bean>

</beans>

Output:- Run the Program class.



Download Eclipse  zip code

Bean Scope
  • Bean scopes control bean creation and storage
  • The Spring IoC container defines five bean
    • singleton (default)--one and only one instance of a particular class will evr be created per classloader.
    • prototype
    •  request
    •  session
    •  globalSession
  • prototype,request, session, globalSession Only available in web application environments.
  • Bean scope configuration is exposed through the XML bean attribute scope.
For Example

<?xml version="1.0" encoding="UTF-8"?>

<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.xsd"
>


<bean id="helloWorld" class="mypack.HelloWorld" scope="prototype">
  <property name="message" value="Hello World!"></property>
</bean>

</beans>