R4R
Right Place For Right Person TM
 
R4R Java Spring Spring FAQS Spring Interview Questions And Answers

 


Tolal:56 Click: 1 2 3
Previous Home Next

Spring Interview Questions And Answers

Page 1

Ques: 1 
What is spring?

Ans:
Spring is an open source framework, created by Rod Johnson. Spring is created to address the complexity of enterprise application development. Spring makes it possible to use plain-vanilla JavaBeans to achieve things that were previously only possible with EJBs. However Spring's usefulness isn't limited to server-side development. Any Java application can benefit from Spring in terms of simplicity, testability, and loose coupling. Spring is a lightweight container, with wrappers that make it easy to use many different services and frameworks. Lightweight containers accept any JavaBean, instead of specific types of components. Spring Web Services aims to facilitate contract-first SOAP service development, allowing for the creation of flexible web services using one of the many ways to manipulate XML payloads.

Ques: 2 What are the various modules of Spring?

Ans:
The Spring framework is made up of seven well defined modules. These modules give you everything you need to develop enterprise-ready applications. All of Spring's modules of are built on top of the core container. The container defines how beans are created, configured and managed-more of the nuts and bolts of Spring. * The core container * Application context module * Spring's AOP module * JDBC abstraction and DAO module * Object/relation mapping integration module * Spring's web module * The Spring MVC framework

Ques: 3 Explain the IoC (Inversion of Control) in Spring framework?

Ans:
Inversion of control is at the heart of the Spring framework. The org.springframework.beans and org.springframework.context packages provide the basis for the Spring Framework's IoC container. The basic concept of the Inversion of Control pattern (dependency injection) is that programmers don’t need to create your objects but describe how they should be created. In a IOC scenario, the container creates all the objects, connects them together by setting the necessary properties, and determines when methods will be invoked. The implementation pattern types for IOC used by SpringFramework are as follows: * Dependencies can be assigned through JavaBeans properties (setter methods). * Dependencies are provided as constructor parameters and are not exposed as JavaBeans Setter properties.

Ques: 4 
What is (AOP) aspect-oriented programming?

Ans:
Aspect oriented programming is often defined as a programming technique that promotes separation of concerns within a software system. Systems are composed of several components, each responsible for a specific piece of functionality. AOP gives you aspects. Aspects enable modularization of concerns such as transaction management that cut across multiple types and objects. AOP is used in the Spring Framework: * To provide declarative enterprise services, especially as a replacement for EJB declarative services. The most important such service is declarative transaction management. * To allow users to implement custom aspects, complementing their use of OOP with AOP.

Ques: 5 
What are the various approaches use by IoC pattern for decoupling of component in Spring framework?

Ans:
The IoC (Inversion of Control) pattern uses three different approaches in order to achieve decoupling of control of services from your components: * Interface Injection : Your components explicitly conformed to a set of interfaces, with associated configuration metadata, in order to allow the framework to manage them correctly. * Setter Injection : External metadata is used to configure how your components can be interacted with. * Constructor Injection : Your components are registered with the framework, including the parameters to be used when the components are constructed, and the framework provides instances of the component with all of the specified facilities applied.

Ques: 6 Compare Spring with Enterprise Java Beans?

Ans:
As J2EE containers, both Spring and EJB offer the deevloper powerful features for developing applications. Comparing the features: * Transaction management : EJB must use a JTA transaction manager and supports transactions that span remote method calls. Spring supports multiple transaction environment with JTA, Hibernate, JDO, JDBC, etc and does not support distributed transaction. * Declarative Transaction support : EJB can define transaction in deployment descriptor and can't declaratively define rollback behaviour. Spring can define transaction in Spring confuguration file and can declaratively define rollback behaviour per method and per exception type. * Persistence : EJB supports programmatic bean-managed persistence and declarative container managed persistence. Spring provides a framework for integrating with several persistence technologies, including JDBC, Hibernate, JDO, and iBATIS. * Declarative security : EJB supports declarative security through users and roles and configured in the deployment descriptor. In Spring, no security implementation out of the box. Acegi provides the declarative security framework built on the top of Spring. * Distributed computing : EJB provides container managed remote method calls. Spring provides proxying for remote calls via RMI, JAX-RPC, and web services.

Ques: 7 What is BeanFactory in Spring framework?

Ans:
A BeanFactory is an implementation fo the factory design patterns. The BeanFactory is a root interface for accessing a Spring bean container. This is the basic client view of a bean container; further interfaces such as ListableBeanFactory and ConfigurableBeanFactory are available for specific purposes. This interface is implemented by objects that hold a number of bean definitions, each uniquely identified by a String name. Depending on the bean definition, the factory will return either an independent instance of a contained object. The point of this approach is that the BeanFactory is a central registry of application components, and centralizes configuration of application components (no more do individual objects need to read properties files. There are several implementations of BeanFactory in Spring. But the most useful is org.springframework.beans.factory.xml.XmlBeanFactory, which loads its bean based on the definitions contained in an XML file. BeanFactory f = new XmlBeanFactory(new FileInputStream("beans.xml"));

Ques: 8 
What are the advantages of Spring framework?

Ans:
The advantages of Spring framework are: * Spring is a layered architecture * Spring Enables POJO (Plain Old Java Object) Programming. POJO programming enables continuous integration and testability. * Dependency Injection and Inversion of Control Simplifies JDBC * Spring is an open source framework available to all for commercial purpose.

Ques: 9 
What are features of Spring ?

Ans:
Features of Spring : * Lightweight :- Spring is lightweight in terms of size and overhead. The entire spring network can be distributed in a single JAR file that weights in at just over 1 MB. * Inverse of Control :- The basic concept of the Inversion of Control pattern 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 IOC container) is then responsible for looking it all. * Aspect-Oriented :- Spring supports the features of aspect oriented programmin approach that enables cohesive development. * Container :- Spring is a container because it manages the life cycle and configuration of application objects. * 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.

Ques: 10 What are the various steps involved in a Spring beans life cycle?

Ans:
The bean factory performs several steps before a bean is ready is use: * Instantiate. * Populate properties. * The factory calls BeanNameAware's setBeanName() method. * The factory calls BeanFactoryAware's setBeanFactory() method. * PostProcessBeforeInitialization() method is called if BeanPostProcessor associated with the bean. * An init-method is called, if specified. * Finally, PostProcessAfterInitialization() is called if BeanPostProcessor associated with it.

Ques: 11 
What do you mean by bean wiring in Spring container?

Ans:
Piecing together beans within the Spring container is known as wiring. Wiring the beans means you are telling the Spring container what beans are needed and how the container should use dependency injection to tie them together. You should have some basic idea of XML for basic wiring of beans.

Ques: 12 
How do add a bean in spring application?

Ans:
A bean can be added in spring application using : <beans> <bean id="foo" class="com.Foo"/> <bean id="bar" class="com.Bar"/> </beans> in the Spring configuration file. In the bean tag the id attribute specifies the bean name and the class attribute specifies the fully qualified class name.

Ques: 13 
What is XMLBeanFactory in Spring framework?

Ans:
The org.springframework.beans.factory.xml.XmlBeanFactory is a class that implements the BeanFactory interface. 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. MyBean myBean = (MyBean) factory.getBean("myBean");

Ques: 14 
What is Spring configuration file? 
Ques: 15 
What are singleton beans and how can you create prototype beans? 

Ans:
The singleton property of <bean> tells the context whether or not a bean is to be defined as a singleton. This 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> Prototyped beans are useful when you want the container to give a unique instance of a bean ezch time it is asked for, but you still want to configure one or more properties of that bean through Spring. <bean id="student"> class="com.StudentImpl" singleton="false"> A new instance of a prototype bean will be created each time getBean() is invoked with the bean's name.

Ques: 16 
How can you reference a bean from another bean in Spring?

Ans:
We use the <property> element to set the properties that reference other beans. The <ref> subelement of <property> lets us do this: <bean id="foo" class="com.Foo"> <property name="bar"> <ref bean="bar"/> </property> </bean>

Ques: 17 
What is inner bean in Spring fremawork?

Ans:
You can embed a <bean> element directly in the <property> element. For Example: <bean id="student" class="com.StudentImpl"> <property name="course"> <bean class="com.CourseImpl"/> </property> </bean> The drawback of wiring a bean reference in this manner is that you can't reuse the instance of StudentImpl anywhere else.

Ques: 18 
What is autowiring in the Spring framework? Explain its type?

Ans:
You wire all of your bean's properties explicitly using the <property> element, However you can have Spring wire them automatically by setting the 'autowire' property on each <bean> that you want autowired: <bean id="foo" class="com.Foo" autowire="autowireType"/> There are four types of autowiring: * byName :- Attempts to find a bean in the container whose name is the same as the name of the property being wired. * byType :- Attempts to find a single bean in the container whose type matches the type of the property being wired. If no matching bean is found, the property will not be wired. * constructor :- Tries to match up one or more beans in the container with the parameters of one of the constructors of the bean being wired. * autodetect :- Attempts to autowire by constructor first and then using byType. Ambiguity is handled the same way as with constructor and byType wiring.

Ques: 19 
How will you handle the ambiguities of autowiring in Spring?

Ans:
When sutowiring using byType or constructor, it's possible that the container may find two or more beans whose type matches the property's type or the types of the constructor arguments. What will happen if there are ambiguous beans suitable for autowiring? Spring can't sort out ambiguities and chooses to throw an exception raher than guess which bean you meant to wire in. If you encounter such ambiguities when autowiring, the best solution is not to autowire the bean.

Ques: 20 How will you wire a string value to a property whose type is a non-string?

Ans:
Yes. You can wire a string value to a property whose type is a non-string. The java.beans.PropertyEditor interface provides a means to customize how String values are mapped to non-String types. This interface is implemented by java.beans.PropertyEditorSupport that has two methods: * getAsText() : returns the String representation of a property's value. * setAsText(String value) : sets a bean property value from the String value passed in. If you want to map the non-string property to a String value, the setAsText() method is called to perform the conversion.


Goto Page:

1 2 3
Share |

Spring Objective

Spring Objective Questions And Answers

Spring Interview Questions And Answers

Spring Interview Questions And Answers

R4R,Spring Objective, Spring Subjective, Spring Interview Questions And Answers,Spring,Spring Interview,Spring Questions ,Spring Answers

New Updates

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R