1. What is IOC (or Dependency Injection)?
The basic concept of the
Inversion of Control pattern (also known as dependency injection) is that you do
not create your objects but describe how they should be created. You don't
directly connect your components and services together in code but describe
which services are needed by which components in a configuration file. A
container (in the case of the Spring framework, the IOC container) is then
responsible for hooking it all up.
2. What are the different
types of IOC (dependency injection) ?
There are three types of
dependency injection:
- Constructor Injection (e.g. Pico container, Spring etc): Dependencies
are provided as constructor parameters.
- Setter Injection (e.g. Spring): Dependencies are assigned through
JavaBeans properties (ex: setter methods).
- Interface Injection (e.g. Avalon): Injection is done through an
interface.
Note: Spring supports only Constructor and Setter
Injection
3. What are features of
Spring ?
-
Lightweight:
-
Inversion of control (IOC):
Loose coupling.
-
Aspect oriented (AOP):
Separating application business logic from
system services.
-
Container:
Spring contains and manages the life cycle and
configuration of application objects.
-
MVC Framework:
Spring comes with MVC web application framework,
built on core Spring functionality. This framework is highly configurable via
strategy interfaces, and accommodates multiple view technologies like JSP,
Velocity, Tiles, iText, and POI.
-
Transaction Management:
Allowing the developer to add the
pluggable transaction managers, and making it easy to demarcate transactions
without dealing with low-level issues. Spring's transaction support is not tied
to J2EE environments and it can be also used in container less environments.
-
Spring provides best Integration
services with Hibernate, JDO and iBATIS.
4. What is Bean Factory
?
A BeanFactory is like a factory class that
contains a collection of beans. The BeanFactory holds Bean Definitions of
multiple beans within itself and then instantiates the bean whenever asked for
by clients.
- BeanFactory is able to create associations between collaborating objects as
they are instantiated. This removes the burden of configuration from bean itself
and the beans client.
- BeanFactory also takes part in the life cycle of a bean, making calls to
custom initialization and destruction methods.
5. What is Application
Context?
A bean factory is fine to simple applications,
but to take advantage of the full power of the Spring framework, you may want to
move up to Springs more advanced container, the application context. On the
surface, an application context is same as a bean factory.Both load bean
definitions, wire beans together, and dispense beans upon request. But it also
provides:
- A means for resolving text messages, including support for
internationalization.
- A generic way to load file resources.
- Events to beans that are registered as listeners.
6. What are the common
implementations of the Application Context ?
The three commonly used
implementation of 'Application Context' are
- ClassPathXmlApplicationContext : It Loads context definition from an
XML file located in the classpath, treating context definitions as classpath
resources. The application context is loaded from the application's classpath by
using the code .
ApplicationContext context = new
ClassPathXmlApplicationContext("bean.xml");
- FileSystemXmlApplicationContext : It loads context definition from an
XML file in the filesystem. The application context is loaded from the file
system by using the code .
ApplicationContext context = new
FileSystemXmlApplicationContext("bean.xml");
- XmlWebApplicationContext : It loads context definition from an XML
file contained within a web application.
ApplicationContext.
|
BeanFactory
|
Here we can have more than one config files
possible
|
In this only one config file or .xml
file
|
Application contexts can publish events to
beans that are registered as listeners
|
Doesn’t
support.
|
Support internationalization (I18N)
messages
|
It’s not
|
Support application life-cycle events, and
validation.
|
Doesn’t
support.
|
Support many enterprise services such JNDI
access, EJB integration, remoting
|
Doesn’t
support.
|
7. What is the typical
Bean life cycle in Spring Bean Factory Container ?
Bean life cycle in Spring Bean Factory
Container is as follows:
-
The spring container finds the bean’s definition
from the XML file and instantiates the bean.
-
Using the dependency injection, spring populates
all of the properties as specified in the bean definition
-
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.
-
If an init-method is specified for the bean, it
will be called.
-
Finally, if there are any BeanPostProcessors
associated with the bean, their postProcessAfterInitialization()
methods will be called.
8. How to integrate
Spring and Hibernate using HibernateDaoSupport?
Spring and Hibernate can integrate using
Spring’s SessionFactory called LocalSessionFactory. The integration process is
of 3 steps.
- Configure the Hibernate SessionFactory
- Extend your DAO Implementation from HibernateDaoSupport
- Wire in Transaction Support with AOP
9. What do you mean by Aspect ?
Aspects are implemented using
regular classes (the schema-based approach) or regular classes annotated with
the @Aspect annotation (@AspectJ style).
1) schema-based approach
XML style.
<aop:config>
<aop:pointcut id="cccdoperation"
expression="(execution(* com.elleinfo.admin.service.*Service.save*(..))) or (execution(* com.elleinfo.admin.service.*Service.process*(..)))" />
<aop:advisor advice-ref="joeTxAdvice" pointcut-ref="cccdoperation" />
<aop:advisor advice-ref="cccdTxAdvice" pointcut-ref="cccdoperation" />
<aop:advisor advice-ref="appsTxAdvice" pointcut-ref="cccdoperation" />
</aop:config>
<aop:config>
<aop:aspect id="myAspect" ref="aBean">
<aop:pointcut id="businessService"
expression="execution(* com.xyz.myapp.service.*.*(..)) && this(service)"/>
<aop:before pointcut-ref="businessService" method="monitor"/>
...
</aop:aspect>
</aop:config>
2) @Aspect {
@Pointcut
@Before/ @Around /@AfterReturning / @AfterThrowing/ @After
}
The XML style has two disadvantages.
- It does not fully encapsulate the
implementation of the requirement it addresses in a single place.
- The XML style is slightly more limited in what it can express than the
@AspectJ style: only the "singleton" aspect instantiation model is supported,
and it is not possible to combine named pointcuts declared in XML.
@Aspect reuqires Java 5.
It is ok to mix both.
10. Explain the
similarities and differences between EJB CMT and the Spring Framework's
declarative transaction management ?
The basic approach is similar: it is possible
to specify transaction behavior (or lack of it) down to individual method level.
It is possible to make a setRollbackOnly() call within a transaction
context if necessary. The differences are:
- Unlike EJB CMT, which is tied to JTA, the Spring Framework's declarative
transaction management works in any environment. It can work with JDBC, JDO,
Hibernate or other transactions under the covers, with configuration changes
only.
- The Spring Framework enables declarative transaction management to be
applied to any class, not merely special classes such as EJBs.
- The Spring Framework offers declarative rollback rules: this is a feature
with no EJB equivalent. Both programmatic and declarative support for rollback
rules is provided.
- The Spring Framework gives you an opportunity to customize transactional
behavior, using AOP. With EJB CMT, you have no way to influence the container's
transaction management other than setRollbackOnly().
- The Spring Framework does not support propagation of transaction contexts
across remote calls, as do high-end application servers.
11: What is difference between singleton and
prototype bean?
Singleton: means single bean definition to a single
object instance per Spring IOC container.
Prototype: means a single
bean definition to any number of object
instances.
all the beans in spring framework are by
default singleton beans
.
12. How many modules are there in Spring? What are they?
Spring comprises of seven modules. They
are. spring.core, spring.beans, spring.aop, sping.context, spring.jdbc, spring.orm, spring.web, spring.transaction, spring.aspect.
-
The core container:
The core container provides the
essential functionality of the Spring framework. A primary component of the core
container is the BeanFactory
, an implementation of the Factory
pattern. The BeanFactory
applies the Inversion of Control
(IOC) pattern to separate an application's configuration and dependency
specification from the actual application code.
-
Spring context:
The Spring context is a configuration
file that provides context information to the Spring framework. The Spring
context includes enterprise services such as JNDI, EJB, e-mail, internalization,
validation, and scheduling functionality.
-
Spring AOP:
The Spring AOP module integrates
aspect-oriented programming functionality directly into the Spring framework,
through its configuration management feature. As a result you can easily
AOP-enable any object managed by the Spring framework. The Spring AOP module
provides transaction management services for objects in any Spring-based
application. With Spring AOP you can incorporate declarative transaction
management into your applications without relying on EJB components.
-
Spring DAO:
The Spring JDBC DAO abstraction layer offers
a meaningful exception hierarchy for managing the exception handling and error
messages thrown by different database vendors. The exception hierarchy
simplifies error handling and greatly reduces the amount of exception code you
need to write, such as opening and closing connections. Spring DAO's
JDBC-oriented exceptions comply to its generic DAO exception hierarchy.
-
Spring ORM:
The Spring framework plugs into several ORM
frameworks to provide its Object Relational tool, including JDO, Hibernate, and
iBatis SQL Maps. All of these comply to Spring's generic transaction and DAO
exception hierarchies.
-
Spring Web module:
The Web context module builds on top
of the application context module, providing contexts for Web-based
applications. As a result, the Spring framework supports integration with
Jakarta Struts. The Web module also eases the tasks of handling multi-part
requests and binding request parameters to domain objects.
-
Spring MVC framework:
The Model-View-Controller (MVC)
framework is a full-featured MVC implementation for building Web applications.
The MVC framework is highly configurable via strategy interfaces and
accommodates numerous view technologies including JSP, Velocity, Tiles, iText,
and POI.
13. What are the different types of events related to Listeners?
There are a lot of events related to ApplicationContext of spring
framework. All the events are subclasses of
org.springframework.context.Application-Event. They are
- ContextClosedEvent – This is fired when the context is closed.
- ContextRefreshedEvent – This is fired when the context is initialized or
refreshed.
- RequestHandledEvent – This is fired when the web context handles any
request.
14. What is the Exception class related to all the exceptions that are thrown in
spring applications?
DataAccessException - org.springframework.dao.DataAccessException
15. list of new features for Spring 3.0
Spring Expression Language.can be used when defining XML and Annotation based bean definitions。 @Value("#{systemProperties.databaseName}")
IoC enhancements/Java based bean metadata: @Configuration, @Bean, @Value
General-purpose type conversion system and field formatting system
Object to XML mapping functionality (OXM) moved from Spring Web Services project
Comprehensive REST support
@MVC additions
Declarative model validation
Early support for Java EE 6
Embedded database support
To be Continued.