Thursday 17 May 2012

Spring Core Module


Spring core module is given to support dependency Injection. This module is base module for other modules of spring.

Resources of spring core module application.
1)Spring Interface(optional)
2)Spring Bean class(POJO)
3)Spring Configuration file(.xml file)
4)Client Application(Standalone java application)

1.Spring Interface:
Declaration of business methods or utility methods. Spring interface is the common understanding document. But it is optional in core module.

2.SpringBean Class(POJO class)
üImplements spring interface (if spring interface is defined otherwise implements is optional)
üContains bean properties (member variables) to hold values injected through dependency injection.
üContains Setter Methods, Constructor Supporting dependency injections.
üContains user-defined methods as life cycle methods (optional)

3.Spring Configuration file
Configuration file is nothing but passing details of resources to the underlying container software.Any filename.xml can act as spring configuration file and this file name must be informed to spring container.There is no default name for spring configuration file
This files contains following things.
üContains Spring Bean classes .
üContains Spring Bean Properties
üContains Dependency Injection


4.Client Application
ØMust be local client
ØActivates spring container by parsing configuration file.
ØMakes spring container performing dependency injection on spring bean class properties.
ØGets access to spring Bean class Object from spring container.
ØCalls business logic on spring bean class object


There are 3 types of dependency Injection supported by spring software
1)Setter Injection

2)Constructor Injection

3)Interface Injection

1)Setter Injection
Again each injection is divided into three types.
     a)Primitives b)Objects c) Collection variables.

    application:1(a.By usign Primitives type)
Sample Spring application on core module by using setter Injection:
Spring bean:
public class testbean
{
          int age;
          public void setAge(int age)
             {
               this.age=age;
             }
        public int getAge()
           {
             return age;
            }
}   

Spring Configuration file
<beans>
<bean name=“tb” class=“testbean”>
<property name=“age”>
<value>16</value>
</property>
</bean>
</beans>

When bean properties is configured with properties tag the spring container gets the instruction to perform setter injection on the bean property.When above code given to spring container it generate following code for instantiation of bean class and setter injection on bean property

testbean  tb=(testbean)Class.forName(“testbean”);
tb.setAge(16);

Client Application:
public  class client
{
    public static void main(String arg[]) throws Exception
       {
  FileSystemResource fsr=new FileSystemResource(“springconfig.xml”);
  XmlBeanFactory factory=new XmlBeanFactory(fsr);
                (or)
              XmlBeanFactory fact=new XmlBeanFactory(new   ClassPathResource("testbeanxml.xml"));
  testbean tb=(testbean)factory.getBean(“td”);
  System.out.println(“the age is “+tb.getAge());
       }
}
In above code we did not use any setter method. But we will get result when we use getter method. Because spring 

With respect above client Application:
XmlBeanFactory factory=new XmlBeanFactory(fsr);
üThis Statement activates Bean Factory  spring container and this container reads and verifies entries available in spring Configuration file.

üSpring Container use SAX parser (XML parser) to read,verify and manipulate XML entries given in springconfiguration file.

üWhen Factory.getBean(“db”) of client application executes the following operations will be taken place.
  1.Gets spring Bean class name from spring configuration file based on given beanId(“bd”);
  2.Spring Container loads testbean class from the memory by using Class.forName().
  3.Spring Container creates  testbean class object with the support of zero-argument constructor of  NewInstance()




To Download code for application-1:
Note: To execute above application we need two jar files
1.Spring.jar
2.commons-logging.jar


Injection through Object Type

If spring bean property type is anther beanclass name (predefined or userdefined) then use <ref> tag to configure dependent value to bean property in springconfiguration file.
<property name=“d”><ref bean=“dt”/>
</property>

application:-2
b.By Using Object reference
reference class:
dept.java

public class dept {
int deptno;
public int getDeptno() {
return deptno; }

public void setDeptno(int deptno) {
this.deptno = deptno; }
}

springbean pojo class
emp.java

public class emp {
int empno;
dept d;
public int getEmpno() {
return empno;}
public void setEmpno(int empno) {
this.empno = empno; }
public dept getD() {
return d;}
public void setD(dept d) {
this.d = d;}
}

client.java

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
public class client {
public static void main(String[] args) {
// TODO Auto-generated method stub
XmlBeanFactory fact=new XmlBeanFactory(new ClassPathResource("springconfig.xml"));
emp obj=(emp)fact.getBean("e");
System.out.println("empno is "+obj.getEmpno());
dept d=obj.getD();
System.out.println(" deptno is "+d.getDeptno());
}
}



Spring configuration File:
if we use anther class object in spring bean class then we need to use <ref> tag as shown below

springconfig.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean name="de" class="dept">
<property name="deptno">
<value>10</value>
</property>
</bean>
<bean name="e" class="emp">
<property name="empno">
<value>1</value>
</property>
<property name="d"><ref bean="de"></ref></property>
</bean>
</beans>

Note: To execute above application we need two jar files
1.Spring.jar
2.commons-logging.jar



c.By Using Collection Variables
We can use collection framework variable for springbean property they are
1.List
2.Set
3.Map
4.Property
for example take this springbean class
public  class demo
{
    List fruits;
    Set phones;
     Map capitals
     Property  faculty;
And setter and getter methods;
}

application:3
demo.java(Spring bean class)
public class demo {

List<String> fruits;
Set<String> names;
Map<Integer, String> books;
Properties authors;
public List<String> getFruits() {
return fruits;
}
public void setFruits(List<String> fruits) {
this.fruits = fruits;
}
public Set<String> getNames() {
return names;
}
public void setNames(Set<String> names) {
this.names = names;
}
public Map<Integer, String> getBooks() {
return books;
}
public void setBooks(Map<Integer, String> books) {
this.books = books;
}
public Properties getAuthors() {
return authors;
}
public void setAuthors(Properties authors) {
this.authors = authors;
}
}


springconfig.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean name="d" class="demo">
<property name="fruits">
<list>
<value>Apple</value>
<value>Orange</value>
<value>Mango</value>
</list>
</property>
<property name="names">
<set>
<value>Hema</value>
<value>Anitha</value>
</set>
</property>
<property name="books">
<map>
<entry>
<key><value>1</value></key><value>java</value>
</entry>
<entry>
<key><value>2</value></key><value>Oracle</value>
</entry>
</map>
</property>
<property name="authors">
<props>
<prop key="ap">Hyd</prop>
<prop key="tamil">Chennai</prop>
</props>
</property>
</bean>
</beans>

client.java
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
XmlBeanFactory fact=new XmlBeanFactory(new ClassPathResource("springconfig.xml"));
demo obj=(demo)fact.getBean("d");
List<String> f=obj.getFruits();
System.out.println("Fruts..............");
for(int i=0;i<f.size();i++)
{
String s=f.get(i);
System.out.println(s);
}
System.out.println("Names............");
Set<String> ss=obj.getNames();
Iterator<String> ie=ss.iterator();
while(ie.hasNext())
{
String s=(String)ie.next();
System.out.println(s);
}
}
}




2.Constructor Injections

If spring container uses parameterized constructor to create spring bean class object and to set values to bean properties as initial values then it is called Constructor Injection.
Constructor injection also supports to inject values to all types of bean properties.
For Constructor Injections.

demo.java(Springbean class)
public class demo {
int age;
String name;
public demo(int age,String name)
{
this.age=age;
this.name=name;
}


And Setter and getter methods as shown above
}

springconfig.xml

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean name="d" class="demo">
<constructor-arg index="0" value="10">
</constructor-arg>
<constructor-arg  index="1" value="abc">
</constructor-arg>
</bean>
</beans>

Client.java
public class client {

public static void main(String[] args) {

XmlBeanFactory fact=new XmlBeanFactory(new ClassPathResource("springconfig.xml"));

demo obj=(demo)fact.getBean("d");

int age=obj.getAge();

String name=obj.getName();
System.out.println("age is "+age+"name is"+name);
}
}










No comments:

Post a Comment