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 ?
-
Spring contains and manages the life cycle and configuration of application objects.
-
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.
-
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.
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.
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.
@Aspect reuqires Java 5.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.
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.
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.
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.
No comments:
Post a Comment