Monday, September 7, 2009

SPRING interview Questions

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.

How to make beans aliases ?

Aliases are used in many situations for example if a component defines a resource by some name and another component want to use that resource by different name suiting to naming of that component aliasing is useful . See following piece of code to make alias of a bean. Here CAdataSource aliased to CBdataSource and then again CBdataSource aliased to AppdataSource.

<alias name="CAdataSource" alias="CBdataSource"/>

<alias name="CBdataSource" alias="AppdataSource" />

Explain Bean lifecycle in Spring framework?

1.       The spring container finds the beans definition from the XML file and instantiates the bean.

2.       Using the dependency injection, spring populates all of the properties as specified in the bean definition.

3.       If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the beans ID.

4.       If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.

5.       If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization() methods will be called.

6.       If an init-method is specified for the bean, it will be called.

7.       Finally, if there are any >BeanPostProcessors associated with the bean, theirpostProcessAfterInitialization() methods will be called.

How to define a bean which is created using a static factory method ?

Following piece of code creates a bean with static method makeInstance of sampleBean2 class.

<bean id="sampleBean" class="sample.SampleBean2" factory-method="makeInstance"/>

How to choose between BeanFactory and ApplicationContext Interface in IOC container?

The org.springframework.beans and org.springframework.context packages provide the basis for the Spring Framework's IoC container. The ApplicationContext interface is superset of BeanFactory implementations like and adds other functionality such as easier integration with Spring's AOP features, event propagation, and application-layer specific contexts such as the WebApplicationContext for use in web applications. So ApplicationContext is more preferred in general case.

How Spring Flexibility distinguishes it from other J2ee frameworks ?

Sometimes it is not possible to completely switch to a different framework. Spring does not force to use everything. Existing front-ends built using Web Work, Struts or other UI frameworks can be integrated perfectly well with a Spring-based middle-tier, allowing you to use the transaction features that Spring offers. The only things you need to do are wire up your business logic using an ApplicationContext and integrate your web layer using a WebApplicationContext.

Similiarly If you need to access existing code via web services, you can use Spring's Hessian, Burlap, Rmi or JaxRpcProxyFactory classes. That enables remote access to existing applications very easy. Even old java objects can be wrapped in Stateless EJBs.

How to choose correct type of view resolvers ?

Spring have number of view resolvers. Following criteria can be used to decide which to use.

AbstractCachingViewResolver:
If there is a need to catch views in a web application, an abstract view resolver can be used. XmlViewResolver:
It can accepts a configuration file written in XML. The default configuration file is /WEB-INF/views.xml.UrlBasedViewResolver:
A kind of ViewResolver used where direct resolution of symbolic view names to URLs is required.ResourceBundleViewResolver:
If there is need to apply different view resources according to the locale.

What are the different types of bean injections?

There are two types of bean injections.

1.       By setter

2.       By constructor

How to configure Definitions used by Particular Dispatcher Servlet of Spring ?

Make a file named springexp-servlet.xml in WEB-INF directory. It is named based on the servlet-name from web.xml with '-servlet' appended to it. Put following line of code there. This file describes that dispacther servlet will use springexpController as one of its controller. A URL mapping also has added so the DispatcherServlet can know which controller should be invoked for different URL's.

<beans>

<bean id="springexpController" class="SpringexpController"/>

<bean id="urlMapping"

class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping ">

<property name="mappings">

<props>

<prop key="/hello.htm">springexpController</prop>

</props>

</property>

</bean>

</beans>

1

2

3

4

5

6

7

8

9

10

 

No comments:

Post a Comment