A.Spring Overview: Beanfactory
A.Spring Overview: Beanfactory
time, through an explicit property value in a bean definition or applied for scanning
through autowiring. The container throws scanBasePackageClasses : Provides the list of classes in the other
BeanInitializationException if the affected bean property has package which must be applied for scanning
not been populated.
49. Explain the @InitBinder?
This annotation is decorated on a method in which a date NamedParameterJdbcTemplate
format is declared, and throughout the class, the defined date SimpleJdbcInsert
format is used. Whenever the binding happens with a date SimpleJdbcCall
field @InitBinder; annotation says to use the 58. How can JDBC be used more efficiently in the
CustomDateEditor, which in return uses the date format
mentioned in @InitBinder. Spring framework?
50. Define @ControllerAdvice? When using the Spring JDBC framework the burden of
resource management and error handling is reduced. So
Classes with @ControllerAdvice can be declared explicitly as
developers only need to write the statements and queries to
Spring beans or auto-detected via classpath scanning. All such
get the data to and from the database. JDBC can be used more
beans are sorted via AnnotationAwareOrderComparator, i.e.
efficiently with the help of a template class provided by Spring
based on @Order and Ordered, and applied in that order at
framework, which is the JdbcTemplate (example here).
runtime. For handling exceptions, an @ExceptionHandler will
be picked on the first advice with a matching exception 59. JdbcTemplate
handler method. For model attributes and InitBinder JdbcTemplate class provides many convenience methods for
initialization, @ModelAttribute and @InitBinder methods will
doing things such as converting database data into primitives
also follow @ControllerAdvice order.
or objects, executing prepared and callable statements, and
51. Can we send an Object as the response of providing custom database error handling.
Controller handler method? 60. How can you fetch records by spring
Yes we can send JSON or XML based response in restful web JdbcTemplate?
services, using the @ResponseBody annotation.
There are two interfaces that can be used to fetch records
52. Explain @ModelAttribute? from the database:
The @ModelAttribute annotation refers to the property of the ResultSetExtractor
Model object and is used to prepare the model data. This RowMapper
annotation binds a method variable or the model object to a 61. What is the advantage of
named model attribute. The annotation accepts an optional
value which indicates the name of the model attribute. The NamedParameterJdbcTemplate?
@ModelAttribute annotation can be used at the parameter NamedParameterJdbcTemplate is built upon JDBCTemplate
level or the method level. The use of this annotation at the which is provided by spring and used for lower level
parameter level is to accept the request form values while at communication with databases. It makes possible to pass SQL
the method level is to assign the default values to a model. Let query arguments as key value pairs. As a result the program
me explain you further with the help of some examples. code is much more readable and therefore serves as better
53. @RequestMapping annotation documentation compared to the indexed or the “?”
placeholder approach. The latter is harder to follow specially if
The @RequestMapping annotation is used to map the web
the number of parameters is huge.
request onto a handler class (i.e. Controller) or a handler
method and it can be used at the Method Level or the Class 62. What is Spring JDBCTemplate class and how to
Level. If developers use the @RequestMapping annotation at use it?
a class level, it will be as a relative path for the method level
The JdbcTemplate class executes SQL queries, update
path.
statements and stored procedure calls, performs iteration
54. What is Spring Java-Based Configuration? Give over ResultSets and extraction of returned parameter values.
some annotation example. It handles the creation and release of resources, thus avoiding
errors such as forgetting to close the connection. It also
Java based configuration option enables you to write most of
catches JDBC exceptions and translates them to the generic,
your Spring configuration without XML but with the help of
more informative, exception hierarchy defined in the
few Java-based annotations.
org.springframework.dao package.
An example is the @Configuration annotation, that indicates that
the class can be used by the Spring IoC container as a source 63. What is the difference between JDBC and
of bean definitions. Another example is the @Bean annotated Spring JDBC?
method that will return an object that should be registered as Spring JDBC value-add provided by the Spring Framework’s
a bean in the Spring application context. on top JDBC layer
55. What is Annotation-based container Define connection parameters
Open the connection
configuration? Specify the statement
An alternative to XML setups is provided by annotation-based Prepare and execute the statement
configuration which relies on the bytecode metadata for Set up the loop to iterate through the results (if any)
wiring up components instead of angle-bracket declarations. Do the work for each iteration
Instead of using XML to describe a bean wiring, the developer Process any exception
moves the configuration into the component class itself by Handle transactions
using annotations on the relevant class, method, or field Close the connection
declaration. 64. Spring DAO support
56. How do you turn on annotation wiring? The Data Access Object (DAO) support in Spring is aimed at
Annotation wiring is not turned on in the Spring container by making it easy to work with data access technologies like
default. In order to use annotation based wiring we must JDBC, Hibernate or JDO in a consistent way. This allows us to
enable it in our Spring configuration file by switch between the persistence technologies fairly easily and
configuring <context:annotation-config/> element. to code without worrying about catching exceptions that are
E.Spring Data Access specific to each technology.
57. Which classes are present in spring JDBC API? 65. What are the ways to access Hibernate by
Spring framework provides the following approaches for Jdbc using Spring?
database access: There are two ways to access Hibernate with Spring:
JdbcTemplate Inversion of Control with a Hibernate Template and Callback.
SimpleJdbcTemplate
Extending HibernateDAOSupport and Applying an AOP Interceptor Aspects can be configured as normal spring beans.
node. d. As any other AOP framework, it weaves cross cutting
concerns into the classes, without making a call to the cross
66. ORM’s Spring support cutting concerns from those classes.
Spring supports the following ORM’s: e. Centralize or modularize the cross cutting concerns
Hibernate Easy to maintain and make changes to the aspects
iBatis Changes need to be made in one place.
JPA (Java Persistence API) In one of your classes you don’t want to have logging, it can
TopLink easily be achieved by modifying the point cut in the respective
JDO (Java Data Objects) aspect (logging aspect). So you need to make changes in only
OJB one place.
67. How can we integrate Spring and Hibernate f. Provision to create aspects using schema based (XML
configuration) or @AspectJ annotation based style.
using HibernateDaoSupport? g. Easy to configure
Use Spring’s SessionFactory called LocalSessionFactory . The
73. What are the AOP implementation?
integration process is of 3 steps:
AOP implementations:
Configure the Hibernate SessionFactory
Spring AOP:
Extend a DAO Implementation from HibernateDaoSupport
Runtime weaving through proxy is done
Wire in Transaction Support with AOP It supports only method level PointCut
68. Types of the transaction management Spring It is DTD based
Apache AspectJ
support Compile time weaving through AspectJ Java tools is done
Spring supports two types of transaction management: It suports field level Pointcuts
Programmatic transaction management: This means that you It is schema based and Annotation configuration
have managed the transaction with the help of programming. JBoss AOP
That gives you extreme flexibility, but it is difficult to JBoss AOP is not only a framework, but also a prepackaged
maintain. set of aspects that are applied via annotations, pointcut
Declarative transaction management: This means you expressions, or dynamically at runtime. Some of these include
separate transaction management from the business code. caching, asynchronous communication, transactions, security,
You only use annotations or XML based configuration to remoting, and many many more.
manage the transactions.
74. What are the AOP terminology?
69. What are the benefits of the Spring Aspect
Framework’s transaction management? Advice
It provides a consistent programming model across different Pointcut
transaction APIs such as JTA, JDBC, Hibernate, JPA, and JDO. JoinPoint
It provides a simpler API for programmatic transaction Introduction
management than a number of complex transaction APIs such Target Object
as JTA. AOP Proxy
It supports declarative transaction management. Weaving
It integrates very well with Spring’s various data access 75. Aspect
abstractions. The core construct of AOP is the aspect, which encapsulates
70. Which Transaction management type is more behaviors affecting multiple classes into reusable modules. It
ia a module which has a set of APIs providing cross-cutting
preferable? requirements. For example, a logging module would be called
Most users of the Spring Framework choose declarative AOP aspect for logging. An application can have any number
transaction management because it is the option with the of aspects depending on the requirement. In Spring AOP,
least impact on application code, and hence is most consistent aspects are implemented using regular classes annotated with
with the ideals of a non-invasive lightweight container. the @Aspect annotation ( @AspectJ style).
Declarative transaction management is preferable over
programmatic transaction management though it is less 76. Join point
flexible than programmatic transaction management, which The join point represents a point in an application where we
allows you to control transactions through your code. can plug-in an AOP aspect. It is the actual place in the
application where an action will be taken using Spring AOP
framework.
F.Spring Aspect Oriented Programming (AOP)
71. Explain AOP 77. Advice
The advice is the actual action that will be taken either before
Aspect-oriented programming, or AOP, is a programming
or after the method execution. This is actual piece of code that
technique that allows programmers to modularize
is invoked during the program execution by the Spring AOP
crosscutting concerns, or behavior that cuts across the typical
framework.
divisions of responsibility, such as logging and transaction
Spring aspects can work with five kinds of advice:
management.
before: Run advice before the a method execution.
72. What are the advantages of spring AOP? after: Run advice after the a method execution regardless of
a. It is non-invasive its outcome.
Your service/domain classes get advised by the aspects (cross after-returning: Run advice after the a method execution only
cutting concerns) without adding any Spring AOP related if method completes successfully.
classes or interfaces into the service/domain classes. after-throwing: Run advice after the a method execution only
Allows the developer to concentrate on the business code, if method exits by throwing an exception.
instead the cross cutting concerns. around: Run advice before and after the advised method is
b. Its implemented in pure Java invoked.
No need for a special compilation unit, or a special class 78. Pointcut
loader
c. It uses Spring’s IOC for dependency injection
The pointcut is a set of one or more joinpoints where an 89. List out all the concepts that are available in
advice should be executed. You can specify pointcuts using
expressions or patterns. the MVC Architecture?
79. What is Introduction? The browser sends a request to DispatcherServlet
DispatcherServlet knows the HanderMapping and can find the
An Introduction allows us to add new methods or attributes to
appropriate controllers
existing classes.
Controllers execute the request and put the data in the model
80. What is Target object? and return back the view name to the DispatcherServlet.
The target object is an object being advised by one or more DispatcherServlet uses the view name and ViewResolver to
aspects. It will always be a proxy object. It is also referred to map to the view.
as the advised object. 90. DispatcherServlet
81. What is a Proxy? The Spring Web MVC framework is designed around
A proxy is an object that is created after applying advice to a a DispatcherServlet that handles all the HTTP requests and
target object. When you think of client objects the target responses.
object and the proxy object are the same.
91. WebApplicationContext
82. What are the different types of AutoProxying? The WebApplicationContext is an extension of the
BeanNameAutoProxyCreator
plain ApplicationContext that has some extra features necessary
DefaultAdvisorAutoProxyCreator
Metadata autoproxying for web applications. It differs from a
normal ApplicationContext in that it is capable of resolving
83. What is Weaving? What are the different themes, and that it knows which servlet it is associated with.
points where weaving can be applied? 92. What is Controller in Spring MVC framework?
Weaving is the process of linking aspects with other Controllers provide access to the application behavior that you
application types or objects to create an advised object. typically define through a service interface. Controllers
Weaving can be done at compile time, at load time, or at interpret user input and transform it into a model that is
runtime. represented to the user by the view. Spring implements a
84. What is the difference between concern and controller in a very abstract way, which enables you to create
a wide variety of controllers.
cross-cutting concern in Spring AOP
The Concern is behavior we want to have in a module of an 93. How would you relate Spring MVC Framework
application. A Concern may be defined as a functionality we to MVC architecture?
want to implement. Spring MVC framework:
The cross-cutting concern is a concern which is applicable The Spring Framework is an open source application
throughout the application and it affects the entire framework and inversion of control container for the Java
application. For example, logging, security and data transfer platform.
are the concerns which are needed in almost every module of MVC architecture:
an application, hence they are cross-cutting concerns. Model View Controller (MVC) as it is popularly called, is a
85. Explain XML Schema-based aspect software design pattern for developing web applications
implementation? 94. What is ViewResolver in Spring MVC?
In this implementation case, aspects are implemented using Spring provides ViewResolver, which enable you to render
regular classes along with XML based configuration. models in a browser without tying you to a specific view
technology. Out of the box, Spring enables you to use JSPs,
86. Explain annotation-based (@AspectJ based) Velocity templates and XSLT views, for example. The two
aspect implementation interfaces which are important to the way Spring handles
This implementation case ( @AspectJ based implementation) views are ViewResolver and View. The ViewResolver provides
refers to a style of declaring aspects as regular Java classes a mapping between view names and actual views. The View
annotated with Java 5 annotations. interface addresses the preparation of the request and hands
the request over to one of the view technologies.