Spring interview questions and answers
61 What is web module?
Spring comes with a full-featured MVC framework for building web
applications. Although Spring can easily be integrated with other MVC
frameworks, such as Struts, Spring’s MVC framework uses IoC to provide for a
clean separation of controller logic from business
objects. It also allows you to declaratively bind request parameters to your
business objects. It also can take advantage of any of Spring’s other services,
such as I18N messaging and validation.
62 What is a BeanFactory?
A BeanFactory is an implementation of the factory pattern that
applies Inversion of Control to separate the application’s configuration and
dependencies from the actual application code.
63 What is AOP Alliance?
AOP Alliance is an open-source project whose goal is to promote
adoption of AOP and interoperability among different AOP implementations by
defining a common set of interfaces and components.
64 What is Spring
configuration file?
Spring configuration file is an XML file. This file contains the
classes information and describes how these classes are configured and
introduced to each other.
65 .What does a simple
spring application contain?
These applications are
like any Java application. They are made up of several classes, each performing
a specific purpose within the application. But these classes are configured and
introduced to each other through an XML file. This XML file describes how to
configure the classes, known as theSpring configuration file.
66 What is
XMLBeanFactory?
BeanFactory has many implementations in Spring. But one of the most useful
one is org.springframework.beans.factory.xml.XmlBeanFactory,
which loads its beans based on the definitions contained in an XML file. To
create an XmlBeanFactory, pass a java.io.InputStream to the
constructor. The InputStream will provide the XML to the factory.
For example, the following code snippet uses a java.io.FileInputStream
to provide a bean definition XML file to XmlBeanFactory.
BeanFactory factory = new XmlBeanFactory(new FileInputStream("beans.xml"));
To retrieve the bean from a BeanFactory, call the getBean() method by passing the name of the bean you want to retrieve.
To retrieve the bean from a BeanFactory, call the getBean() method by passing the name of the bean you want to retrieve.
MyBean myBean = (MyBean) factory.getBean("myBean")
67 . What are important ApplicationContext implementations in spring framework?
- ClassPathXmlApplicationContext
– This
context loads a context definition from an XML file located in the class
path, treating context definition files as class path resources.
- FileSystemXmlApplicationContext
– This
context loads a context definition from an XML file in the filesystem.
- XmlWebApplicationContext
– This
context loads the context definitions from an XML file contained within a
web application.
68 . Explain Bean
lifecycle in Spring framework?
- 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.
69 What is bean wiring?
Combining together beans within the Spring container is known as
bean wiring or wiring. When wiring beans, you should tell the container what
beans are needed and how the container should use dependency injection to tie
them together.
70 How do add a
bean in spring application?
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="foo" class="com.act.Foo"/>
<bean id="bar" class="com.act.Bar"/>
</beans>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="foo" class="com.act.Foo"/>
<bean id="bar" class="com.act.Bar"/>
</beans>
71. What are
singleton beans and how can you create prototype beans?
Beans defined in spring framework are singleton beans. There is
an attribute in bean tag named ‘singleton’ if specified true then bean becomes
singleton and if set to false then the bean becomes a prototype bean. By
default it is set to true. So, all the beans in spring framework are by default
singleton beans.
<beans>
<bean id="bar" class="com.act.Foo" singleton=”false”/>
</beans>
<bean id="bar" class="com.act.Foo" singleton=”false”/>
</beans>
72 . What are the
important beans lifecycle methods?
There are two important bean lifecycle methods. The first one is
setup which is called when the bean is loaded in to the container. The second
method is the teardown method which is called when the bean is unloaded from
the container.
73 . How can you override
beans default lifecycle methods?
The bean tag has two more important attributes with which you
can define your own custom initialization and destroy methods. Here I have
shown a small demonstration. Two new methods fooSetup and fooTeardown are to be
added to your Foo class.
<beans>
<bean id="bar" class="com.act.Foo" init-method=”fooSetup” destroy=”fooTeardown”/>
</beans>
<bean id="bar" class="com.act.Foo" init-method=”fooSetup” destroy=”fooTeardown”/>
</beans>
74 . What are Inner
Beans?
When wiring beans, if a bean element is embedded to a property
tag directly, then that bean is said to the Inner Bean. The drawback of this
bean is that it cannot be reused anywhere else.
75. What are the
different types of bean injections?
There are two types of bean injections.
- By setter
- By constructor
76. What is Auto wiring?
You can wire the beans as you wish. But spring framework also
does this work for you. It can auto wire the related beans together. All you
have to do is just set the autowire attribute of bean tag to an autowire type.
<beans>
<bean id="bar" class="com.act.Foo" Autowire=”autowire type”/>
</beans>
<bean id="bar" class="com.act.Foo" Autowire=”autowire type”/>
</beans>
77 . What are different
types of Autowire types?
There are four different types by which autowiring can be done.
- byName
- byType
- constructor
- autodetect
78. 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.
79. What is an Aspect?
An aspect is the cross-cutting functionality that you are
implementing. It is the aspect of your application you are modularizing. An
example of an aspect is logging. Logging is something that is required
throughout an application. However, because applications tend to be broken down
into layers based on functionality, reusing a logging module through
inheritance does not make sense. However, you can create a logging aspect and
apply it throughout your application using AOP.
80 . What is a
Jointpoint?
A joinpoint is a point in the execution of the application where an aspect can be plugged in. This point could be a method being called,
an exception being thrown, or even a field being modified. These are the points
where your aspect’s code can be inserted into the normal flow of your
application to add new
behavior.
0 comments:
Post a Comment