Spring

1. What is Spring ?

Spring is an open source framework created to address the complexity of enterprise application development. One of the chief advantages of the Spring framework is its layered architecture, which allows you to be selective about which of its components you use while also providing a cohesive framework for J2EE application development.

2. What are the advantages of Spring framework ?

  • Spring has layered architecture. Use what you need and leave you don’t need now.
  • Spring Enables POJO Programming. There is no behind the scene magic here. POJO Programming enables continous integration and testability.
  • Dependency Injection and inversion of Control Simplifies JDBC
  • Open source and no vendor lock-in.

3. What are features of Spring ?

  • Light Weight:- Spring is lightweight when it comes to size and transparency. The basic version of spring framework is around 1MB. And the processing overhead is also very negligible.
  • Inversion of control (IOC):- Loose coupling is achieved in spring using the technique Inversion of Control. The objects give their dependencies instead of creating or looking for dependent objects.
  • Aspect Oriented:- Spring support Aspect oriented programming and enables cohesive development by 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. But other frameworks can be easily used instead of Spring MVC Framework.
  • Transaction Management:- Spring framework provides a generic abstraction layer for transaction management. This 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.
  • JDBC Exception Handling:- The JDBC abstraction layer of the Spring offers a meaningful exception hierarchy, which simplifies the error handling.

4. How many modules are there in Spring? What are they?

Spring has seven modules. They are as follows:

  • 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.

5. What are the types of Dependency Injection Spring supports ?

  • Setter Injection:- Setter-based DI is realized by calling setter methods on your beans after invoking a no-argument constructor or no-argument static factory method to instantiate your bean.
  • Constructor Injection:- Constructor-based DI is realized by invoking a constructor with a number of arguments, each representing a collaborator.

6. 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.

7. 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.

8. Difference between Bean Factory and Application Context ?

  • Application contexts provide a means for resolving text messages, including support for i18n of those messages.
  • Application contexts provide a generic way to load file resources, such as images.
  • Application contexts can publish events to beans that are registered as listeners.
  • Certain operations on the container or beans in the container, which have to be handled in a programmatic fashion with a bean factory, can be handled declaratively in an application context.
  • ResourceLoader support: Spring’s Resource interface us a flexible generic abstraction for handling low-level resources. An application context itself is a ResourceLoader, Hence provides an application with access to deployment-specific Resource instances.
  • MessageSource support: The application context implements MessageSource, an interface used to obtain localized messages, with the actual implementation being pluggable.

9. How is a typical spring implementation look like ?

Following files are needed for Spring Application:

  • An interface that defines the functions.
  • An implementation that contains properties, its setter and getter methods, functions and so on.
  • Spring AOP (Aspect Oriented Programming)
  • A XML file called Spring configuration file
  • Client program that uses the function.

10. What do you mean by Bean wiring ?

The act of creating associations between application components (beans) within the Spring container is reffered to as Bean wiring.

11. What do you mean by Auto Wiring?

The Spring container is able to autowire relationships between collaborating beans. This means that it is possible to automatically let Spring resolve collaborators (other beans) for your bean by inspecting the contents of the BeanFactory. The autowiring functionality has five modes. 1. no 2. byName 3.byType 4.constructor 5.autodirect.

12. What is DelegatingVariableResolver ?

Spring provides a custom JavaServer Faces VariableResolver implementation that extends the standard Java Server Faces managed beans mechanism which lets you use JSF and Spring together. This variable resolver is called as DelegatingVariableResolver

13. What are the different modules in Spring framework ?

  • The Core container module
  • Application context module
  • AOP module (Aspect Oriented Programming)
  • JDBC Abstraction and DAO module.
  • O/R mapping integration module (Object/Relational)
  • Web module
  • MVC framework module

14. What is Core container module ?

This module provides the fundamental functionality of the spring framework. In this module BeanFactory is the heart of any spring-based application. The entire framework was built on the top of this module. This module makes the Spring container.

15. What is Application context module ?

The Application context module makes spring a framework. This module extends the concept of BeanFactory, providing support for internationalization (I18N) messages, application lifecycle events, and validation. This module also supplies many enterprise services such JNDI access, EJB integration, remoting, and scheduling. It also provides support to other framework.

16. What is AOP module ?

The AOP module is used for developing aspects of Spring-enabled application. Much of the support has been provided by the AOP Alliance in order to ensure the interoperability between Spring and other AOP frameworks. This module also introduces metadata programming to Spring. Using Spring’s metadata support, we will be able to add annotations to our source code that instruct Spring on where and how to apply aspects.

17. What is JDBC abstraction and DAO module?

Using this module we can keep up the database code clean and simple, and prevent problems that result from a failure to close database resources. A new layer of meaningful exceptions on top of the error messages given by several database servers is bought in this module. In addition, this module uses Spring’s AOP module to provide transaction management services for objects in a Spring application.

18. What are object/relational mapping integration module?

Spring supports an object/relational mapping (ORM) tool over straight JDBC by providing the ORM module. Spring provide support to tie into several popular ORM frameworks, including Hibernate, JDO, and iBATIS SQL Maps. Spring’s transaction management supports each of these ORM frameworks as well as JDBC.

19. What is web module ?

This module is built on the application context module, providing a context that is appropriate for web-based applications. This module also contains support for several web-oriented tasks such as transparently handling multipart requests for file uploads and programmatic binding of request parameters to your business objects.

20. 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.

21. 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.

22. What does a simple 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.

23. 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.

24. What is Significance of JSF – Spring integration ?

Spring – JSF integration is useful when an event handler wishes to explicitly invoke the bean factory to create beans on demand, such as a bean that encapsulates the business logic to be performed when a submit button is pressed.

25. How to integrate your Struts application with spring ?

 Configure Spring to manage your Actions as beans, using the ContextLoaderPlugin, and set their dependencies in a Spring context file.

Subclass Spring’s ActionSupport classes and grab your Spring-managed beans explicitly using a getWebApplicationContext() method.

26. 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.

27. 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.

28. What is 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.

29. What is an Advice ?

Advice is the implementation of an aspect. It is something like telling your application of a new behavior. Generally, and advice is inserted into an application at joinpoints.

30. What is a Pointcut?

A pointcut is something that defines at what joinpoints an advice should be applied. Advices can be applied at any joinpoint that is supported by the AOP framework. These Pointcuts allow you to specify where theadvice can be applied.

31. What is Target ?

A target is the class that is being advised. The class can be a third party class or your own class to which you want to add your own custom behavior. By using the concepts of AOP, the target class is free to center on its major concern, unaware to anyadvice that is being applied.

32. What is a Proxy ?

A proxy is an object that is created after applying advice to a target object. When you think of client objects the target object and the proxy object are the same.

33. What is meant by Weaving ?

The process of applying aspects to a target object to create a new proxy object is called as Weaving. The aspects are woven into the target object at the specified joinpoints.

34. What are the different types of AutoProxying ?

  • BeanNameAutoProxyCreator
  • DefaultAdvisorAutoProxyCreator
  • Metadata autoproxying.

35.  Describe Spring Security ?

Spring security framework focuses on providing both authentication and authorization in java applications. It also takes care of most of the common security vulnerabilities such as CSRF attack.

It’s very beneficial and easy to use Spring security in web applications, through the use of annotations such as @EnableWebSecurity. You should go through following posts to learn how to use Spring Security framework.

Spring Security in Servlet Web Application
Spring MVC and Spring Security Integration Example

36.  What is Controller in Spring MVC framework ?

Controllers provide access to the application behavior that you typically define through a service interface. Controllers interpret user input and transform it into a model that is represented to the user by the view. Spring implements a controller in a very abstract way, which enables you to create a wide variety of controllers.

37. What is Annotation-based container configuration?

An alternative to XML setups is provided by an annotation-based configuration which relies on the bytecode metadata for wiring up components instead of angle-bracket declarations. Instead of using XML to describe a bean wiring, the developer moves the configuration into the component class itself by using annotations on the relevant class, method, or field declaration.

38. What are the ways to insert the collection concept in Spring?

Basically, there are four collection elements to insert in Spring.

  • <set> element – It wires the set of values by eliminating the duplicates from them.
  • <list> element – It is useful to insert or inject values and also allows duplicate values.
  • <map> element – It is used to insert a key or name-value pair which can be of any type.
  • <props> element – It is used to insert key or name-value pair, but the type should only be the string.

39. Explain the concept of Auto wiring.

Auto-wiring is an essential concept in Spring framework. It is used to implicitly inject object dependency by use of a setter or constructor based injection. Auto-wiring works with reference types only, so it is not useful for injecting values for primitive and string types.

The best advantage of auto wire is that the developers need to write less code as dependency injection is taken care by auto wire. Also, the programmer has not controlled over the process then.

Syntax: <bean id="customer" class="com.sthelp.same.Customer" autowire="byName" />

40. Is there any limitation of autowiring? If yes, explain.

Yes, there are some limitations of Auto wiring which are as mentioned below.

  • There is always a possibility of overriding.
  • The developer will not be able to auto-wire primitive and Spring properties.
  • Auto-wiring becomes complex when used in big applications compared to explicit wiring.

41. What is annotation wiring and how do we turn it on?

The alternative use of XML is annotations in which the developers move the entire configuration in one class with the help of annotations for a particular class or method.

It is turned on in the Spring Configuration file by declaring <context:annotation-config/>.

42. What is the use of @Required annotation?

It is used to indicate that at configuration time, the bean property should be populated through auto wiring or explicit property value in the bean definition.

import org.Springframework.beans.factory.annotation.Required;
public class Employee {
private Integer age;
private String Lname;
@Required
public void setAge(Integer age) {
this.age = age;
}
public Integer getAge() {
return age;
}
@Required
public void setLName(String Lname) {
this.name = name;
}
public String getLName() {
return name;
}
}

43. What is the use of @Autowired annotation?

It helps to get better control and understanding of how and where auto wiring should be achieved. It can be used to the autowire bean on any setter method, property or constructor.

import org.Springframework.beans.factory.annotation.Autowired;
public class code-editor {
private SpellCheck spellCheck;
 @Autowired
public void setSpellCheck( SpellCheck spellCheck ){
this.spellCheck = spellCheck;
}
public SpellCheck getSpellCheck( ) {
return spellCheck;
}
public void spellCheck() {
spellCheck.checkSpelling();
}
}

44. What is the use of @Qualifier annotation?

It is mainly used when the developer is bound to create many beans of the same type and want to wire only one of them with the property, in this scenario @Qualifier with @Autowired is used for removing confusion and specifying the exact bean to be wired.

45. How is an event handled in Spring?

Event handling is achieved through ApplicationEvent class and ApplicationListner interface.

When the bean implements ApplicationListner then ApplicationEvent gets generated to ApplicationContext and notifies that the bean is generated.

46. What are the examples of standard Spring Events?

There are many standard Spring events serving respective purpose and few among them are ContextStartedEvent, ContextRefreshedEvent, ContextStoppedEvent, ContextClosedEvent, RequestHandledEvent and so on.

47. What is Transaction Management in Spring? Explain the different types of Transaction Management.

Transaction is basically some operation performed on some data in the database. Transaction Management comes under Relational Database management system and is used to ensure data ethics and consistency.

The core advantage of Transaction Management is that it supports declarative and programmatic Transaction Management and API like Hibernate, JTA, and JDBC by correct integration.

There are two types of Transaction Management, which are mentioned below:

  • Programmatic Transaction Management is used to help the transaction in terms of coding or scripting.
  • Declarative Transaction Management is used to isolate business code and transactions.

48. What are the different types of Object Relational Mapping that Spring supports?

Spring supports ORM like Hibernate, IBatis, TopLink, Java Data Object, OJB, and JPA etc.

49. What do you mean by Spring Batch?

Spring batch is also a lightweight framework that is created to support the development of robust batch applications for the enterprise system.

Spring batch improves the productivity, approach for development, and makes certain features that the users have learned in Spring to be much easier.

50. Explain Tasklet in Spring Batch.

Tasklet is an interface, which is often called to perform one task only like clean up, or deletion or setting up of resources during execution time.

Related Articles

Core Java

1. What are primitive types in Java ? byte, short, int, long, float, double, char, boolean… 2. What are tokens? Name the 5 types of tokens available in Java with an example…

C Questions

1.What is c? C is a high-level and general-purpose programming language that is ideal for developing firmware or portable applications. Originally intended for writing system…

Spring

1. Hello World Program HelloWorld.java MainApp.java 2. How to create Spring applcation. Create Java Class Create XML File Create test class Load jar files required…

Responses

Your email address will not be published. Required fields are marked *

×