Java

Friday 28 September 2012

spring ioc by madhav-8790977721


TUTIORALS FROM MADHAV:


     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY  



Introduction to spring framework:
Spring is a multi-tier open-source lightweight application framework, addressing most infrastructure concerns of enterprise applications.
It is mainly a technology dedicated to enale us to build applications using POJOS.
That means spring framework provides support in simplifying the development of every single tier in an  enterprise application.
For example:
Starting from the presentation to the integration tier(or sometimes data access layer)implementation it has its role in simplifying our obj.
Spring framework overview:
·         Spring core container:
·         AOP
·         JDBC AND DAO Module:
·         ORM  Module
·         J2EE (JAVA Enterprise edition)
·         Web module
Inversion of control(IOC)
Inversion of control is an architectural pattern describing an external entity(that is container)used to wire objects at creation time by injecting  there dependencies.
In other words the ioc describes that a dependency injection needs to be done by an external entity instead of creating the dependencies by the component itself.
Initializing spring core container and accessing spring beans.
The spring core container can be instantiated by creating an object of any one of the BeanFactoy or
ApplicationContext  implementation classes supplying the spring beans configurations.
//using the BeanFactory to instantiate spring core container

BeanFactory beans=new  XmlBeanFactory(new FileSystemResources(“spring.xml”);
//using applicationContext to instantiate spring core container
ApplicationContext context=new ClassPathXmlApplicationContext(“spring.xml”);
Methods of BeanFactory:
Method
Description
Boolean containsBean(String name)
If bean is available  this return ‘true’ otherwise it retrns ‘false’

Object getBean(String name)
Retrn an instance  that is instantiated using the bean definition iidentified by the given name in this bean factoy.

Example programs 1:
//THIS  EXAMPLE WILL DESCRIBES GETTING SPRING BEANS FROM SPRING CONTAINER.

Shape.java

package madhav.container;

public interface Shape {

      public void draw();
}


Triangle.java
package madhav.container;

public class Triangle implements Shape {

      public void draw()
      {
            System.out.println("triangle");
      }
}
Spring.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-3.0.xsd">
 <bean id="triangle" class="madhav.container.Triangle"/>

</beans>


TestCase.java

package madhav.container;

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

public class TestCase {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub
           
            //testing the springcontainer

            BeanFactory bf=new XmlBeanFactory(new FileSystemResource("spring.xml"));
         Shape shape=(Shape)bf.getBean("triangle");
         shape.draw();
        

      }

}


Output:
triangle

 Dependency Injection
Java components / classes should be as independent as possible of other Java classes. This increases the possibility to reuse these classes and to test them independently of other classes(Unit Testing). To decouple Java components from other Java components the dependency to a certain other class should get injected into them rather that the class itself creates / finds this object.

TYPES OF DEPENDENCY INJECTION:
·         Setter method injection
·         Constructor injection

Setter  method Injection:

This is the most popular and simple DI method, it will injects the dependency via a setter method.
Example programs 2:
//THIS  EXAMPLE WILL DESCRIBES  SETTING THE DEPENDENCIES BY USING “SETTER METHOD INJECTION” .
point
Triangle
                    
    
                                 

Point.java

package madhav.iocdep;

public class Point {
private int x;
private int y;
public int getX() {
      return x;
}
public void setX(int x) {
      this.x = x;
}
public int getY() {
      return y;
}
public void setY(int y) {
      this.y = y;
}

}

Triangle.java

package madhav.iocdep;

public class Triangle {
private Point pointa;
private Point pointb;
private Point pointc;
public Point getPointa() {
      return pointa;
}
public void setPointa(Point pointa) {
      this.pointa = pointa;
}
public Point getPointb() {
      return pointb;
}
public void setPointb(Point pointb) {
      this.pointb = pointb;
}
public Point getPointc() {
      return pointc;
}
public void setPointc(Point pointc) {
      this.pointc = pointc;
}
public void show()
{
      System.out.printf("pointa=" + "(" + pointa.getX()+"," + pointa.getY()+ ")" +"\n" );
      System.out.printf("pointb=" + "(" + pointb.getX()+"," + pointb.getY()+ ")" +"\n" );
      System.out.printf("pointc=" + "(" + pointc.getX() +"," + pointc.getY()+ ")" +"\n" );
}

}
Spring.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-3.0.xsd">
This is id of triangle instance for refereeing the out side world .
 

 <bean id="triangle" class="madhav.iocdep.Triangle">
    <property name="pointa" ref="point1"></property>
    <property name="pointb" ref="point2"></property>
    <property name="pointc" ref="point3"></property>
</bean>
<bean id="point1" class="madhav.iocdep.Point">
    <property name="x" value="0"></property>
    <property name="y" value="0"></property>
</bean>
<bean id="point2" class="madhav.iocdep.Point">
    <property name="x" value="2"></property>
    <property name="y" value="5"></property>
</bean>
<bean id="point3" class="madhav.iocdep.Point">
    <property name="x" value="6"></property>
    <property name="y" value="9"></property>
</bean>
</beans>

TestCase.java


package madhav.iocdep;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestCase {
            /**
             * @param args
             */
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
Id of “Triangle “instance maintained by spring container.
                           //test casse for iocdep(ioc dependency setter based)
                           BeanFactory bf=new XmlBeanFactory(new FileSystemResource("spring.xml"));
                           Triangle t=(Triangle)bf.getBean("triangle");
                           t.show();
            }
}

Output:

pointa=(0,0)
pointb=(2,5)
pointc=(6,9)



Constructor injection:
The Constructor injection method of dependency is the method of injecting the dependencies of an object through its constructor arguments.
Example programs 2:
//THIS  EXAMPLE WILL DESCRIBES  SETTING THE DEPENDENCIES BY USING “CONSTRUCTOR INJECTION” .
Point.java

package madhav.iocdep;

public class Point {
private int x;
private int y;
public int getX() {
      return x;
}
public void setX(int x) {
      this.x = x;
}
public int getY() {
      return y;
}
public void setY(int y) {
      this.y = y;
}

}
THIS METHOD USED BY THE SPRING CONTAINER
FOR SETTING THE DEPEDENCE-“pointa”
 
Triangle.java

package madhav.iocdep;

public class Triangle {
private Point pointa;

public Triangle(Point pointa)
{
      this.pointa=pointa;
}
public Point getPointa() {
      return pointa;
}
public void setPointa(Point pointa) {
      this.pointa = pointa;
}


public void show()
{
      System.out.printf("pointa=" + "(" + pointa.getX()+"," + pointa.getY()+ ")" +"\n" );
}

}

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


 <bean id="triangle" class="madhav.iocdep.Triangle">
THESE PROPERTIES SET BY THE SPRING CONTAINER BASED ON SETTERS OF A “Point” CLASS.
    <constructor-arg ref="point1"></constructor-arg>
</bean>
<bean id="point1" class="madhav.iocdep.Point">
    <property name="x" value="0"></property>
    <property name="y" value="0"></property>
</bean>

</beans>

TestCase.java
package madhav.iocdep;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestCase {
            /**
             * @param args
             */
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
                           //test casse for iocdep(ioc dependency constructor based
                           BeanFactory bf=new XmlBeanFactory(new FileSystemResource("spring.xml"));
                           Triangle t=(Triangle)bf.getBean("triangle");
                           t.show();
            }
}
Output:

pointa=(0,0)
 example 4
// THIS PROGRAM WILL DESCRIBES –COLLECTIONS IN SPRING BEANS.
Point.java

package madhav.ioccollections;

public class Point {
private int x;
private int y;
public int getX() {
      return x;
}
public void setX(int x) {
      this.x = x;
}
public int getY() {
      return y;
}
public void setY(int y) {
      this.y = y;
}

}

Triangle.java

package madhav.ioccollections;

import java.util.List;

public class Triangle {
private List<Point> points;


public List<Point> getPoints() {
      return points;
}


public void setPoints(List<Point> points) {
      this.points = points;
}


public void show()
{
      for(Point p:points)
      {
     
      System.out.printf("points=" + "(" + p.getX()+"," + p.getY()+ ")" +"\n" );
      }
}

}

Spring.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-3.0.xsd">
  <!--  hear i am defining collection of points for Triangle class. -->

  <bean id="trianglelist" class="madhav.ioccollections.Triangle">
    <property name="points">
    <list>
    <ref bean="point1"/>
    <ref bean="point2"/>
    <ref bean="point3"/>
    </list>
    </property>
</bean>
<bean id="point1" class="madhav.ioccollections.Point">
    <property name="x" value="0"></property>
    <property name="y" value="0"></property>
</bean>
<bean id="point2" class="madhav.ioccollections.Point">
    <property name="x" value="2"></property>
    <property name="y" value="5"></property>
</bean>
<bean id="point3" class="madhav.ioccollections.Point">
    <property name="x" value="6"></property>
    <property name="y" value="9"></property>
</bean>

   

</beans>

TestCase.java


package madhav.ioccollections;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestCase {
            /**
             * @param args
             */
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
                        //test case for ioccollection
                          
                           BeanFactory bf=new XmlBeanFactory(new FileSystemResource("spring.xml"));
                           Triangle t=(Triangle)bf.getBean("trianglelist");
                           t.show();
            }
}

Output:
points=(0,0)
points=(2,5)
points=(6,9)
BEAN INHERITANCE:
In spring , the inheritance is supported in bean configuration for a bean to share common values properties or configurations.
A child bean or inherited bean can inherit its parent bena configurations, properties and some attributes.

EXAMPLE 4(BEAN-INHERITANCE)
// THIS PROGRAM WILL DESCRIBES –BEAN INHERITANCE.

Point.java

package madhav.iocdep;

public class Point {
private int x;
private int y;
public int getX() {
      return x;
}
public void setX(int x) {
      this.x = x;
}
public int getY() {
      return y;
}
public void setY(int y) {
      this.y = y;
}

}

Triangle.java

package madhav.iocdep;

public class Triangle {
private Point pointa;
private Point pointb;
private Point pointc;
public Point getPointa() {
      return pointa;
}
public void setPointa(Point pointa) {
      this.pointa = pointa;
}
public Point getPointb() {
      return pointb;
}
public void setPointb(Point pointb) {
      this.pointb = pointb;
}
public Point getPointc() {
      return pointc;
}
public void setPointc(Point pointc) {
      this.pointc = pointc;
}
public void show()
{
      System.out.printf("pointa=" + "(" + pointa.getX()+"," + pointa.getY()+ ")" +"\n" );
      System.out.printf("pointb=" + "(" + pointb.getX()+"," + pointb.getY()+ ")" +"\n" );
      System.out.printf("pointc=" + "(" + pointc.getX() +"," + pointc.getY()+ ")" +"\n" );
}

}
Spring.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-3.0.xsd">
PARENT BEAN
 
<bean id="triangleparent" class="madhav.iocdep.Triangle">
<property name="pointa" ref="point1"></property>
</bean>

 <bean id="triangle" class="madhav.iocdep.Triangle" parent=”triangleparent>
   
    <property name="pointb" ref="point2"></property>
    <property name="pointc" ref="point3"></property>
</bean>
<bean id="point1" class="madhav.iocdep.Point">
    <property name="x" value="0"></property>
HEAR THE CHILD BEAN IS INHERIT THE PARENT
PROPERTIES

    <property name="y" value="0"></property>
</bean>
<bean id="point2" class="madhav.iocdep.Point">
    <property name="x" value="2"></property>
    <property name="y" value="5"></property>
</bean>
<bean id="point3" class="madhav.iocdep.Point">
    <property name="x" value="6"></property>
    <property name="y" value="9"></property>
</bean>
</beans>

TestCase.java


package madhav.iocdep;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestCase {
            /**
             * @param args
             */
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
                       
                           BeanFactory bf=new XmlBeanFactory(new FileSystemResource("spring.xml"));
                           Triangle t=(Triangle)bf.getBean("triangle");
                           t.show();
            }
}
Output:

pointa=(0,0)
pointb=(2,5)
pointc=(6,9)

INNER BEANS:
In spring framework, whenever a bean is used for only one particular property, it’s advice to declare it an inner bean. And the inner bean is support both in setter injection ‘property’ and constructor injection ‘constructor-arg’.
EXAMPLE 6(INNER BEANS)
// THIS PROGRAM WILL DESCRIBES –INNER BEANS.
Point.java

package madhav.iocdep;

public class Point {
private int x;
private int y;
public int getX() {
      return x;
}
public void setX(int x) {
      this.x = x;
}
public int getY() {
      return y;
}
public void setY(int y) {
      this.y = y;
}

}

Triangle.java

package madhav.iocdep;

public class Triangle {
private Point pointa;
private Point pointb;
private Point pointc;
public Point getPointa() {
      return pointa;
}
public void setPointa(Point pointa) {
      this.pointa = pointa;
}
public Point getPointb() {
      return pointb;
}
public void setPointb(Point pointb) {
      this.pointb = pointb;
}
public Point getPointc() {
      return pointc;
}
public void setPointc(Point pointc) {
      this.pointc = pointc;
}
public void show()
{
      System.out.printf("pointa=" + "(" + pointa.getX()+"," + pointa.getY()+ ")" +"\n" );
      System.out.printf("pointb=" + "(" + pointb.getX()+"," + pointb.getY()+ ")" +"\n" );
      System.out.printf("pointc=" + "(" + pointc.getX() +"," + pointc.getY()+ ")" +"\n" );
}

}
Spring.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-3.0.xsd">

INNER BEAN
 <bean id="triangle" class="madhav.iocdep.Triangle">
    <property name="pointa">

<bean class="madhav.iocdep.Point">
    <property name="x" value="0"></property>
    <property name="y" value="0"></property>
</bean>

</property>
    <property name="pointb" ref="point2"></property>
    <property name="pointc" ref="point3"></property>
</bean>
<bean id="point2" class="madhav.iocdep.Point">
    <property name="x" value="2"></property>
    <property name="y" value="5"></property>
</bean>
<bean id="point3" class="madhav.iocdep.Point">
    <property name="x" value="6"></property>
    <property name="y" value="9"></property>
</bean>
</beans>

TestCase.java


package madhav.iocdep;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestCase {
            /**
             * @param args
             */
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
                           //test casse for iocdep(ioc dependency setter based)
                           BeanFactory bf=new XmlBeanFactory(new FileSystemResource("spring.xml"));
                           Triangle t=(Triangle)bf.getBean("triangle");
                           t.show();
            }
}

Output:

pointa=(0,0)
pointb=(2,5)
pointc=(6,9)


bean autowiring:


Autowiring    provides more fine-grained control over where and how autowiring should be accomplished. 
Example program 8:
//this program will describes “autowiring”:
Point.java

package madhav.iocdep;

public class Point {
private int x;
private int y;
public int getX() {
      return x;
}
public void setX(int x) {
      this.x = x;
}
public int getY() {
      return y;
}
public void setY(int y) {
      this.y = y;
}

}

Triangle.java

package madhav.iocdep;

public class Triangle {
private Point pointa;

THIS METHOD USED BY THE SPRING CONTAINER
FOR SETTING THE DEPEDENCE-“pointa”
public Triangle(Point pointa)
{
      this.pointa=pointa;
}
public Point getPointa() {
      return pointa;
}
public void setPointa(Point pointa) {
      this.pointa = pointa;
}


public void show()
{
      System.out.printf("pointa=" + "(" + pointa.getX()+"," + pointa.getY()+ ")" +"\n" );
}

}

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


 <bean id="triangle" class="madhav.iocdep.Triangle" autowire=”type”>
   
</bean>
<bean id="point1" class="madhav.iocdep.Point">
    <property name="x" value="0"></property>
    <property name="y" value="0"></property>
</bean>
By using the , we can avoid  “<propery> element tag from the
“spring.xml 
 
</beans>


TestCase.java


package madhav.iocdep;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestCase {
            /**
             * @param args
             */
            public static void main(String[] args) {
                        // TODO Auto-generated method stub
                           //test casse for iocdep(ioc dependency constructor based
                           BeanFactory bf=new XmlBeanFactory(new FileSystemResource("spring.xml"));
                           Triangle t=(Triangle)bf.getBean("triangle");
                           t.show();
            }
}
Output:
pointa=(0,0)
BEAN SCOPES AND LIFECYCLE:
We know that the beans configured in the spring beans XML configuration file are instantiated and managed by the spring core container.
And one of the other great things of sprig frameworks customization support is the configuration that is allows to specifying the scope for each particular bean definition as per the requirements
Scope
Description
Singleton
A bean definition configured with this scope is instantiated only once per container instance.


Prototype
A bean definition configured with this scope  is instantiated every time it is requested for or referenced.
Request

Session

Global session



Annotations:
1.       @Autowire
2.       @Required
3.       @Qualifies

Working with stereotype

4.       @component
5.       @repository
6.       @service and @Controller

@Autowire :
Used to set the dependences automatically. Config methods may have an arbitrary name and any number of arguments; each of those arguments will be autowired with a matching bean in the Spring container.
@Required
Used to set the dependences automatically.
@Qulifier
This annotation may be used on a field or parameter as a qualifier for candidate beans when autowiring.


Example program1:
//this program will describes the annotations-@Autowire,@Requied
Driver.java

package softech.madhav;

public class Driver {
private String driverName;
private String driverLoc;
public String getDriverName() {
      return driverName;
}
public void setDriverName(String driverName) {
      this.driverName = driverName;
}
public String getDriverLoc() {
      return driverLoc;
}
public void setDriverLoc(String driverLoc) {
      this.driverLoc = driverLoc;
}


}

Connection.java

package softech.madhav;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

public class Connection {
     
private Driver driver;

public Driver getDriver() {
      return driver;
}
@Autowired
public void setDriver(Driver driver) {
      this.driver = driver;
}
public void getConnection()
{
      System.out.println("connection --obtained the below info");
      System.out.println("drivername="+ driver.getDriverName());
      System.out.println("drivername="+ driver.getDriverLoc());
     
}
}
Spring.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
              
   
      <context:annotation-config></context:annotation-config>
     

<bean id="driver" class="softech.madhav.Driver" >
<property name="driverName" value="mysql"></property>
<property name="driverLoc" value="mysql.driver"></property>
 </bean>

<bean id="conn" class="softech.madhav.Connection">

</bean> 

</beans>
TestCase.java
package softech.madhav;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.beans.factory.xml.*;
public class TestCase {
            public static void main(String[] args) {
  ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
  // BeanFactory ctx=new XmlBeanFactory(new FileSystemResource("spring.xml"));
  Connection e=(Connection)ctx.getBean("conn");
   e.getConnection();
            }
}

Output:
connection --obtained the below info
drivername=mysql
drivername=mysql.driver
Example program 2:
//this program will describes the annotations -@Component,@Service,@Qualifier
Driver.java
package softech.madhav;

public class Driver {
private String driverName;
private String driverLoc;
public String getDriverName() {
      return driverName;
}
public void setDriverName(String driverName) {
      this.driverName = driverName;
}
public String getDriverLoc() {
      return driverLoc;
}
public void setDriverLoc(String driverLoc) {
      this.driverLoc = driverLoc;
}


}
Connection.java

package softech.madhav;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service("conn")
public class Connection {
            @Autowired
            @Qualifier("driver2")
private Driver driver;
public Driver getDriver() {
            return driver;
}
public void setDriver(Driver driver) {
            this.driver = driver;
}
public void getConnection()
{
            System.out.println("connection --obtained the below info");
            System.out.println("drivername="+ driver.getDriverName());
            System.out.println("drivername="+ driver.getDriverLoc());
}
}

Spring.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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd">
              
     <context:component-scan base-package="softech.madhav"/>
This will give information  to the container  for searching the components automatically.
      <context:annotation-config></context:annotation-config>
     
<bean id="driver" class="softech.madhav.Driver" >
<property name="driverName" value="oracle"></property>
<property name="driverLoc" value="orcle.driver"></property>
</bean>
<bean id="driver2" class="softech.madhav.Driver" >
<property name="driverName" value="mysql"></property>
<property name="driverLoc" value="mysql.driver"></property>
 </bean>

</beans>

TestCase.java
package softech.madhav;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.FileSystemResource;
import org.springframework.beans.factory.xml.*;
public class TestCase {
            public static void main(String[] args)
  ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
  // BeanFactory ctx=new XmlBeanFactory(new FileSystemResource("spring.xml"));
  Connection e=(Connection)ctx.getBean("conn");
   e.getConnection();
            }
}

Output:
connection --obtained the below info
drivername=mysql
drivername=mysql.driver





click hear to download the source code
TUTIORALS FROM MADHAV:


     JAVA-SERVLETS     JAVA-JDBC     JAVA-JSP       HIBERNATE-SHCEMABASED 

    SPRING-AOP-ANNOTATIONS      SPRING -DAO     SPRIN-MVC     SPRING-SECUTITY  








No comments:

Post a Comment