Spring Framework concepts
Spring framework is a light weight component for creating a java ee applications.Spring framework is an abstraction layer on top of the existing technologies. A spring framework will provide common functionalities for the projects as ready made,and the developer will build the remaining code for the projects.
In this Spring framework have mainly 3 injunctions are there.There are
1.setter injection
2.construction injection
3.interface injection
Setter Injection: In a spring framework if the dependencies are injected by calling the setter method of an object at run time then is called a setter injection.
Constructor Injection: if the dependencies are injected by calling the constructor of an object at run time then it is called constructor injection.
Interface Injection: if the dependencies are injected by the calling a method provided by a interface then it is calling interface injection.
Spring framework supports all the three types of dependency injections.The interface injection is supported only at particular time,that is in a bean life cycle process.
setter injection example:
public class TravelService
{
private Vehicle vehicle
{
public void setVehicle(Vehicle vehicle)
{
this.vehicle=vehicle;
}
}
constructor injection example:
public class TravelService
{
private Vehicle vehicle
{
public TravelService(Vehicle vehicle)
{
this.vehicle=vehicle;
}
}
interface injection example:
public class Demo implements BeanFactoryAware
{
private BeanFactory fact;
{
public void set BeanFactory(Beanfactory fact)
{
this.fact=fact;
}
}
-->here interface provided method is used for injecting the dependency.So it is a interface injection.
Spring Bean: Spring bean is a pojo class. And a spring bean may or may not contain the default constructor.
The difference between the spring bean and the java bean is java bean may contain a default constructor,in spring bean mayor may not contain the default constructor.
To create a spring application required files are
-->Bean class
-->web.xml
-->client class
Steps for creating a spring bean object in a client application:
step-1: create a resource object
Resource r=new ClassPathResource("beans.xml");
step-2: create spring container object
BeanFactory factory=new XmlBeanFactory(r);
step-3: get the bean object from the container by using id with calling getBean()method
Object o=factory.getBean("id");
step-4:Type cast the Object class to our spring bean object.the it calls our business method.
DemoBean db=new DemoBean();
db.showMessage();
0 comments:
Enjoyed? Share Your View With Us