Ans: With the two core concepts of Aspect-Oriented and Dependency Injection, Spring is one of the most popular and widely used Java EE frameworks. Through dependency injection it can provide tight coupling between various components. The cross-cutting tasks such as authentication and logging can be provided through Spring framework and the aspect-oriented programming can be implemented through this as well.
Spring framework is a featured framework which can provide several modules and lots of features for the specific tasks such as Spring JDBC and Spring MVC. Working with Spring is quite easy and a fun activity due to the presence of a number of online communities and resources.
Q2. Why Spring Framework is needed?
Ans: Spring framework is needed because it is –
Ans: Spring framework offers a number of advantages which includes the following:
Ans: Some important and mostly used Spring modules are:
Ans: Any class of Spring framework, which is initialized by Spring IoC container is known as Spring Bean. We can get the Spring Bean instance through Spring Application Context. The life-cycle of Spring Bean can be managed by Spring IoC container.
Ans: Some cross-cutting concerns for any enterprise level application like transaction management, data validation, authentication and logging in for various application modules and objects are most valuable. In case of object-oriented programming such application modularity is implemented by classes while in AOP it is achieved by Aspects.
Ans: There are two important methods of Bean life cycle:
Ans: Spring framework is based on two main concepts known as aspect-oriented programming and dependency injection. Few features of Spring framework are:
Ans: Dependency injection is a concept which is implemented through the design patterns. It can remove the hard-coded dependency and can make the application loosely coupled, maintainable and extendable. The dependency resolution can also be moved from compile time to run time through these design patterns. It provides following benefits:
Google Guice can also be used for dependency injection and the processes can be automated through this. If we want to implement any additional concept along with dependency injection then Spring is the best choice for that.
Ans: A front controller is defined as “a controller which handles all requests for a Web Application.” DispatcherServlet (actually a servlet) is the front controller in Spring MVC that intercepts every request and then dispatches/forwards requests to an appropriate controller.
When a web request is sent to a Spring MVC application, dispatcher servlet first receives the request. Then it organizes the different components configured in Spring’s web application context (e.g. actual request handler controller and view resolvers) or annotations present in the controller itself, all needed to handle the request.
Ans: As you know about servlet filters that they can pre-handle and post-handle every web request they serve — before and after it’s handled by that servlet. In the similar way, you can use HandlerInterceptor interface in your spring mvc application to pre-handle and post-handle web requests that are handled by Spring MVC controllers. These handlers are mostly used to manipulate the model attributes returned/submitted they are passed to the views/controllers.
A handler interceptor can be registered for particular URL mappings, so it only intercepts requests mapped to certain URLs. Each handler interceptor must implement the HandlerInterceptor interface, which contains three callback methods for you to implement: preHandle(), postHandle() and afterCompletion().
Problem with HandlerInterceptor interface is that your new class will have to implement all three methods irrespective of whether it is needed or not. To avoid overriding, you can use HandlerInterceptorAdapter class. This class implementsHandlerInterceptor and provide default blank implementations.
Ans: Spring supports validations primarily into two ways.
Using JSR-303 Annotations and any reference implementation e.g. Hibernate Validator
Q14. What is the difference between concern and cross-cutting concern in Spring AOP?
Ans:
Concern − Concern is behavior which we want to have in a module of an application. Concern may be defined as a functionality we want to implement. Issues in which we are interested define our concerns.
Cross-cutting concern − It's a concern which is applicable throughout the application and it affects the entire application. e.g. logging , security and data transfer are the concerns which are needed in almost every module of an application, hence are cross-cutting concerns.
Ans: For using servlet container configured JNDI DataSource, we need to configure it in the spring bean configuration file and then inject it to spring beans as dependencies. Then we can use it with JdbcTemplate to perform database operations.
< bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
< property name="jndiName" value="java:comp/env/jdbc/MySQLDB"/>
< /bean>
Ans: Bean Factory is core of the spring framework and, it is a Lightweight container which loads bean definitions and manages your beans. Beans are configured using XML file and manage singleton defined bean. It is also responsible for life cycle methods and injects dependencies. It also removes adhoc singletons and factories.
Ans: Following are the different types of events of listeners:
Ans: 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.
Ans: There are two ways to access hibernate using spring −