Why
spring is light weighted
- The main reason being Spring Framework light weight is
because of its API distribution.
- It has so many Jar files in the package. They are
designed in such a way that each module of Spring Framework is divided
into an Jar API.
- So, whenever you design any application with Spring
Framework, need to add only those APIs which you want to use from the
framework.
- So, if you are using Spring's IOC feature then you will
add Spring Core and Spring Bean Jar files, rest you don't need to include
in your project.
- So, that way Spring Framework is light weight, because
framework is very much decoupled.
Spring employs four key strategies:
- Lightweight and minimally invasive development with
plain old Java objects (POJOs)
- Loose coupling through dependency injection and
interface orientation
- Declarative programming through aspects and common
conventions
- Boilerplate reduction through aspects and templates.
What are the advantages of Spring Framework?
Ans. The advantages of spring are as follows:
- Spring has layered architecture. So it is up to the programmer what to use and what to leave.
- Spring Enables POJO (Plain old Java object) Programming. POJO programming enables continuous integration and testability.
- Dependency Injection and Inversion of Control Simplifies JDBC (Java Database Connectivity), open source and no vendor lock-in.
What are the types of Dependency Injection Spring supports?
Ans. Following are the types of Dependency Injection Spring supports:
- Setter Injection: Setter-based DI is realized by calling setter methods on the beans after invoking a no-argument constructor or no-argument static factory method to instantiate the bean.
- Constructor Injection: Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator.
Explain Bean-LifeCycle.
Ans: Spring framework is based on IOC so we call it as IOC container also So Spring beans reside inside the IOC container. Spring beans are nothing but Plain old java object (POJO).
Following steps explain their life cycle inside container.
- Container will look the bean definition inside configuration file (e.g. bean.xml).
- using reflection container will create the object and if any property is defined inside the bean definition then it will also be set.
- If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the bean’s ID.
- If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.
- If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization() methods will be called before the properties for the Bean are set.
- If an init() method is specified for the bean or @PostConstruct annotation is used , it will be called.
- If the Bean class implements the DisposableBean interface, then the method destroy() will be called when the Application no longer needs the bean reference.
- If the Bean definition in the Configuration file contains a 'destroy-method' attribute or @PreDestroy annotation is used , then the corresponding method definition in the Bean class will be called.
What is IOC or inversion of control?
Answer: Inversion of control means invert the control of creating
the object from our own using new operator to container or framework.
Now it’s the responsibility of container to create object as
required.
We maintain one xml file where we configure our components,
services, all the classes and their property.
We just need to mention which service is needed by which
component and container will create the object for us.
This concept is known as dependency
injection because all object
dependency (resources) is injected into it by framework.
Example:
<bean id="messanger" class="com.shambhu.bean.HelloWorld">
<property
name="msg" value="Hello
World" />
</bean>
In this example HelloWorld class
contain getter and setter for msg and container will
instantiate msg and set the value automatically when
it is used.
This whole process is
also called wiring in Spring and by using annotation
What is IOC or inversion of control?
Answer: Inversion of control means invert the control of creating
the object from our own using new operator to container or framework.
Now it’s the responsibility of container to create object as
required.
We maintain one xml file where we configure our components,
services, all the classes and their property.
We just need to mention which service is needed by which
component and container will create the object for us.
This concept is known as dependency
injection because all object
dependency (resources) is injected into it by framework.
Example:
<bean id="messanger" class="com.shambhu.bean.HelloWorld">
<property
name="msg" value="Hello
World" />
</bean>
In this example HelloWorld class
contain getter and setter for msg and container will
instantiate msg and set the value automatically when
it is used.
This whole process is
also called wiring in Spring and by using annotation
0 comments: