ZYAX4870
The Java singleton is scoped by the Java class loader, the Spring singleton is scoped by the container context.
Which basically means that, in Java, you can be sure a singleton is a truly a singleton only within the context of the class loader which loaded it. Other class loaders should be capable of creating another instance of it (provided the class loaders are not in the same class loader hierarchy), despite of all your efforts in code to try to prevent it.
In Spring, if you could load your singleton class in two different contexts and then again we can break the singleton concept.
So, in summary, Java considers something a singleton if it cannot create more than one instance of that class within a given class loader, whereas Spring would consider something a singleton if it cannot create more than one instance of a class within a given container/context.
Change spring-servlet.xml Filename
(Spring Web Contenxt Configuration Filename)
<servlet>
<servlet-name>
spring
</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>
1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>
spring
</servlet-name>
<url-pattern>
*.html
</url-pattern>
</servlet-mapping>
What if we want to load a file
called “bean.xml” instead of default “XXX-servlet.xml” ?
<servlet>
<servlet-name>
spring
</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
/WEB-INF/bean.xml
</param-value>
</init-param>
<load-on-startup>
1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>
spring
</servlet-name>
<url-pattern>
*.html
</url-pattern>
</servlet-mapping>
setContextConfigLocation
method
on DispatcherServlet
and
overrides default context config file. Note that it is possible to add multiple
locations separated by any number of commas and spaced.<servlet>
<servlet-name>
spring
</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>
contextConfigLocation
</param-name>
<param-value>
/WEB-INF/bean.xml, /WEB-INF/bean-service.xml, /WEB-INF/bean-dao.xml
</param-value>
</init-param>
<load-on-startup>
1
</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>
spring
</servlet-name>
<url-pattern>
*.html
</url-pattern>
</servlet-mapping>
--------------------------------------------------------------------------------------------------------------------------
How Spring Controller
Request Mapping works in Spring MVC
DispatcherServlet
receives a web request,
it determines which controllers should handle the incoming request.@Controller
annotation. The
dispatching process depends on the various @RequestMapping
annotations declared in a
controller class and its handler methods. There are three levels of request
mapping can be defined in Spring controller.@RequestMapping(“path”)
@RequestMapping(“path”, method=RequestMethod.GET). Other Http methods such as POST, PUT, DELETE, OPTIONS, and TRACE are are also supported.
@RequestMapping(“path”, method=RequestMethod.GET, params=”param1”)
@RequestMapping(“path”, header=”content-type=text/*”)
@Controller
public class Employee {
@RequestMapping("/employee/add")
public ModelAndView add(
@RequestParam(value = "firstName") String firstName,
@RequestParam(value = "surName") String surName) {
//....
//....
return null;
}
@RequestMapping(value={"/employee/remove","/employee/delete"})
public ModelAndView delete(
@RequestParam(value = "uuid") String uuid) {
//....
//....
return null;
}
}
add()
method is declared
with @RequestMapping("/employee/add")
. If the incoming request path
to matches is /employee/add
, then the add() handler method
will be invoked to process the request.@Controller
@RequestMapping("/employee/*")
public class Employee {
@RequestMapping("add")
public ModelAndView add(
@RequestParam(value = "firstName") String firstName,
@RequestParam(value = "surName") String surName) {
//....
//....
return null;
}
@RequestMapping(value={"remove","delete"})
public ModelAndView delete(
@RequestParam(value = "uuid") String uuid) {
//....
//....
return null;
}
}
@RequestMapping
annotation handles all
types of incoming HTTP request including GET, POST, PUT, DELETE, PATCH etc. By
default, it’s assumed all incoming requests to URLs are of the HTTP GET kind.
To differentiate the mapping by HTTP request type, we need to explicitly
specify the HTTP request method in the @RequestMapping declaration.@Controller
@RequestMapping("/employee/*")
public class Employee {
@RequestMapping(value = { "remove", "delete" }, method = { RequestMethod.POST, RequestMethod.DELETE })
public ModelAndView delete(@RequestParam(value = "uuid") String uuid) {
// ....
// ....
return null;
}
}
What is the front controller class of Spring MVC?
A front controller is defined as a controller that handles all requests for a Web Application. DispatcherServlet servlet is the front controller in Spring MVC that intercepts every request and then dispatches 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.
Dependency Inversion
Principle
1.1
Motivation
High Level Classes --> Abstraction Layer --> Low Level Classes
1.2
Intent
1.3
Example
// Dependency Inversion Principle - Bad example
classWorker {
publicvoidwork() {
//....working
}
}
ClassManager {
Workerworker;
PublicvoidsetWorker(Workerw)
{
worker=w; } publicvoidmanage(){ worker.work(); } } classSuperWorker { publicvoidwork(){ //....working much more } }
1.1
Conclusion
When this principle is applied it means the high level classes
are not working directly with low level classes, they are using interfaces as
an abstract layer. In this case instantiation of new low level objects inside
the high level classes (if necessary) cannot be done using the operator new.
Instead, some of the Creational design patterns can be used, such as Factory
Method, Abstract Factory, Prototype.
The Template Design Pattern is an example where the DIP
principle is applied.
Of course, using this principle implies an increased effort,
will result in more classes and interfaces to maintain, in a few words in more
complex code, but more flexible. This principle should not be applied blindly
for every class or every module. If we have a class functionality that is more
likely to remain unchanged in the future there is not need to apply this
principle.
Spring
Annotations: Contents:
For spring to process annotations, add the
following lines in your application-context.xml file.
<context:annotation-config />
<context:component-scan base-package="...specify your package name..." />
@Service
Annotate all your service classes with
@Service. All your business logic should be in Service classes.
@Repository
Annotate all your DAO classes with
@Repository. All your database access logic should be in DAO classes.
@Component
Annotate your other components (for example
REST resource classes) with @Component.
@Component is a generic stereotype for any Spring-managed
component. @Repository, @Service, and @Controller are specializations of
@Component for more specific use cases, for example, in the persistence,
service, and presentation layers, respectively.
@Autowired
Let Spring auto-wire other beans into your classes using
@Autowired annotation.
@Transactional
Configure your transactions with
@Transactional spring annotation.
The default @Transactional settings are as follows:
These default settings can be changed using various
properties of the @Transactional spring annotation.
@Scope
As with Spring-managed components in
general, the default and most common scope for autodetected components is
singleton. To change this default behavior, use @Scope spring annotation.
Similarly, you can annotate your component with
@Scope("prototype") for beans with prototype scopes.
Spring MVC Annotations
@Controller
Annotate your controller classes with
@Controller.
@RequestMapping
You use the @RequestMapping spring
annotation to map URLs onto an entire class or a particular handler method.
Typically the class-level annotation maps a specific request path (or path
pattern) onto a form controller, with additional method-level annotations
narrowing the primary mapping.
@PathVariable
You can use the @PathVariable spring
annotation on a method argument to bind it to the value of a URI template
variable. In our example below, a request path of /company/techferry will bind
companyName variable with 'techferry' value.
@RequestParam
You can bind request parameters to method
variables using spring annotation @RequestParam.
@ModelAttribute
An @ModelAttribute on a method argument
indicates the argument should be retrieved from the model. If not present in
the model, the argument should be instantiated first and then added to the
model. Once present in the model, the argument's fields should be populated
from all request parameters that have matching names. This is known as data
binding in Spring MVC, a very useful mechanism that saves you from having to
parse each form field individually.
@SessionAttributes
@SessionAttributes spring annotation
declares session attributes. This will typically list the names of model
attributes which should be transparently stored in the session, serving as
form-backing beans between subsequent requests.
@SessionAttribute works as follows:
The following listing illustrate these concepts. It is also
an example for pre-populating Model objects.
Spring Security Annotations
@PreAuthorize
Using Spring Security @PreAuthorize annotation,
you can authorize or deny a functionality. In our example below, only a user
with Admin role has the access to delete a contact.
Loose Coupling and Tight Coupling
Core Module is
the heart of Spring, tight coupling
and loose coupling is the heart concept of Core Module :-) so let us try to understand
about tight and loose coupling between java objects in
spring [ you can't move further without
understand this concepts, be care ]
Tight Coupling Between Java Objects
Let us see tight
coupling between java objects first, take
an example..
·
In the above
example, Traveler object is depends on car object. So traveler class
creating an object of Car class inside it [ line number 3 ]
·
If the other class
object is created in the dependent class [ like Car class object in Traveler
class ], there exist tight coupling, i mean if method in car object is changed
then we need to do the changes in the Traveler class too so its the tight coupling between Traveler and Car class objects
Loose Coupling between Java Objects
Let us see loose coupling between java objects, take an example..
In order to overcome tight coupling
between objects, spring framework uses dependency injection mechanism with the
help of POJO/POJI model and through dependency injectionits possible to achieve loose coupling
In the above example
Traveler, Car is tightly coupled. If we want to achieve loose coupling between the objects Traveler and Car, we need to re-write
the application like….
------------------
-----------------------------
You can mix both, Constructor-based and
Setter-based DI but it is a good rule of thumb to use constructor arguments for
mandatory dependencies and setters for optional dependencies.
Code is cleaner with the DI principle and
decoupling is more effective when objects are provided with their dependencies.
The object does not look up its dependencies, and does not know the location or
class of the dependencies rather everything is taken care by the Spring Framework.
4
Spring
Constructor-based Dependency Injection
Constructor-based
DI is accomplished when the container invokes a class constructor with a number
of arguments, each representing a dependency on other class.
TextEditor.java
packagecom.tutorialspoint;
public class TextEditor {
privateSpellCheckerspellChecker;
publicTextEditor(SpellCheckerspellChecker) {
System.out.println("Inside TextEditor
constructor." );
this.spellChecker = spellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
SpellChecker.java
packagecom.tutorialspoint;
public class SpellChecker {
publicSpellChecker(){
System.out.println("Inside SpellChecker
constructor." );
}
public void checkSpelling() {
System.out.println("Inside checkSpelling."
);
}
}
MainApp.java
packagecom.tutorialspoint;
importorg.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
newClassPathXmlApplicationContext("Beans.xml");
TextEditorte = (TextEditor)
context.getBean("textEditor");
te.spellCheck();
}
}
Beans.xml
<?xml version="1.0"
encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for textEditor bean -->
<bean id="textEditor"
class="com.tutorialspoint.TextEditor">
<constructor-arg
ref="spellChecker"/>
</bean>
<!-- Definition for spellChecker bean
-->
<bean id="spellChecker"
class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
OutPut :
Inside SpellChecker
constructor.
Inside
TextEditor constructor.
Inside
checkSpelling.
5
Spring
Setter-based Dependency Injection
Setter-based
DI is accomplished by the container calling setter methods on your beans after
invoking a no-argument constructor or no-argument static factory method to
instantiate your bean.
TextEditor.java
packagecom.tutorialspoint;
public class TextEditor {
privateSpellCheckerspellChecker;
// a
setter method to inject the dependency.
public void setSpellChecker(SpellCheckerspellChecker)
{
System.out.println("Inside
setSpellChecker." );
this.spellChecker = spellChecker;
}
// a
getter method to return spellChecker
publicSpellCheckergetSpellChecker() {
returnspellChecker;
}
public void spellCheck() {
spellChecker.checkSpelling();
}
}
SpellChecker.java
packagecom.tutorialspoint;
public class SpellChecker {
publicSpellChecker(){
System.out.println("Inside SpellChecker
constructor." );
}
public void checkSpelling() {
System.out.println("Inside checkSpelling."
);
}
}
MainApp.java
packagecom.tutorialspoint;
importorg.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
newClassPathXmlApplicationContext("Beans.xml");
TextEditorte = (TextEditor)
context.getBean("textEditor");
te.spellCheck();
}
}
Beans.xml
<?xml version="1.0"
encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for textEditor bean -->
<bean id="textEditor"
class="com.tutorialspoint.TextEditor">
<property name="spellChecker"
ref="spellChecker"/>
</bean>
<!-- Definition for spellChecker bean
-->
<bean id="spellChecker"
class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
AUTO - Wiring
Following is the
configuration file Beans.xml in normal condition:<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for textEditor bean --> <beanid="textEditor"class="com.tutorialspoint.TextEditor"> <propertyname="spellChecker"ref="spellChecker"/> <propertyname="name"value="Generic Text Editor"/> </bean>
<!-- Definition for spellChecker bean --> <beanid="spellChecker"class="com.tutorialspoint.SpellChecker"> </bean>
</beans>
6
Spring Autowiring 'byName'
But if you are
going to use autowiring 'byName', then your XML configuration file will become
as follows:<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for textEditor bean --> <beanid="textEditor"class="com.tutorialspoint.TextEditor" autowire="byName"> <propertyname="name"value="Generic Text Editor"/> </bean>
<!-- Definition for spellChecker bean --> <beanid="spellChecker"class="com.tutorialspoint.SpellChecker"> </bean>
</beans>
7
Spring Autowiring 'byType'
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for textEditor bean -->
<bean id="textEditor" class="com.tutorialspoint.TextEditor"
autowire="byType">
<property name="name" value="Generic Text Editor" />
</bean>
<!-- Definition for spellChecker bean -->
<bean id="SpellChecker" class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
8
Spring Autowiring by Constructor
<?xml version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<!-- Definition for textEditor bean -->
<bean id="textEditor"
class="com.tutorialspoint.TextEditor"
autowire="constructor">
<constructor-arg value="Generic Text
Editor"/>
</bean>
<!-- Definition for spellChecker bean
-->
<bean id="SpellChecker"
class="com.tutorialspoint.SpellChecker">
</bean>
</beans>
Spring MVC
In Spring Web MVC, DispatcherServlet class
works as the front controller. It is responsible to manage the flow of the
spring mvc application.
The @Controller annotation is used to mark
the class as the controller in Spring 3.
The @RequestMapping annotation is used to
map the request url. It is applied on the method.
Understanding the flow of Spring
Web MVC
1)
Create the request page (optional)
This is the simple
jsp page containing a link. It is optional page. You may direct invoke the
action class instead.
index.jsp
2)
Create the controller class
To create the
controller class, we are using two annotations @Controller and @RequestMapping.The @Controller annotation marks this class as Controller. The @Requestmapping annotation is used to map the class with the specified name. This class returns the instance of ModelAndView controller with the mapped name, message name and message value. The message value will be displayed in the jsp page.
HelloWorldController.java
3)
Provide the entry of controller in the web.xml file
In this xml file,
we are specifying the servlet class DispatcherServlet that acts as the front
controller in Spring Web MVC. All the incoming request for the html file will
be forwarded to the DispatcherServlet.
web.xml
4)
Define the bean in the xml file
This is the
important configuration file where we need to specify the ViewResolver and View
components.The context:component-scan element defines the base-package where DispatcherServlet will search the controller class. Here, the InternalResourceViewResolver class is used for the ViewResolver. The prefix+string returned by controller+suffix page will be invoked for the view component. This xml file should be located inside the WEB-INF directory.
spring-servlet.xml
5) Display the message in the JSP page
This is the simple JSP page, displaying the message returned by the
Controller.
It must be located inside the WEB-INF/jsp directory for this example
only.
hellopage.jsp
Question 5: What are different modules
in spring?
Answer
: spring have seven core modules
1. The Core container module
2. Application context module
3. AOP module (Aspect Oriented
Programming)
4. JDBC abstraction and DAO
module
5. O/R mapping integration
module (Object/Relational)
6. Web module
7. MVC framework module
4) What are the core interfaces of Hibernate?
The core interfaces of Hibernate framework are:
3) What are the steps to connect to the database in java?
Question 7: What type of transaction
Management Spring support?
Ans: This spring interview questions is little difficult as
compared to previous questions just because transaction management is a
complex concept and not every developer familiar with it. Transaction
management is critical in any applications that will interact with the
database. The application has to ensure that the data is consistent and the
integrity of the data is maintained. Two type of transaction management
is supported by spring
1.
Programmatic transaction management
2.
Declarative transaction management.
In Spring, the Inversion of Control (IoC) principle is
implemented using the Dependency Injection (DI) design
pattern. Let's understand dependency injection with the help of an example.
First we will see a java version of the example and later we will add spring
functionalities to it. As far as the example go, its pretty simple. The QuizMater interface
exposes the popQuestion() method. To keep things simple, our QuizMaster will
generate only one question.
QuizMaster.java
----------------
packagecom.vaannila;
public
interface QuizMaster {
public String popQuestion();
}
The StrutsQuizMaster and the SpringQuizMaster class
implements QuizMaster interface and they generate questions
related to struts and spring respectively.
StrutsQuizMaster.java
----------------------
packagecom.vaannila;
public
class StrutsQuizMaster implements QuizMaster {
@Override
public
String popQuestion() {
return
"Are you new to Struts?";
}
}
SpringQuizMaster.java
----------------------
packagecom.vaannila;
public
class SpringQuizMaster implements QuizMaster {
@Override
public
String popQuestion() {
return
"Are you new to Spring?";
}
}
We have a QuizMasterService class that displays the
question to the user. TheQuizMasterService class holds reference to
the QuizMaster.
QuizMasterService.java
-----------------------
packagecom.vaannila;
public
class QuizMasterService {
privateQuizMasterquizMaster
= new SpringQuizMaster();
public
void askQuestion()
{
System.out.println(quizMaster.popQuestion());
}
}
Finally we create the QuizProgram class to conduct
quiz.
QuizProgram.java
----------------
packagecom.vaannila;
public
class QuizProgram {
public
static void main(String[] args) {
QuizMasterServicequizMasterService
= new QuizMasterService();
quizMasterService.askQuestion();
}
}
As you can see it is pretty simple, here we create an instance of
the QuizMasterService class and call the askQuestion() method.
When you run the program as expected "Are you new to Spring?"
gets printed in the console.
Let's have a look at the class diagram of this example. The green arrows
indicate generalization and the blue arrows indicates association.
As you can see this architecture is tightly coupled. We create an
instance of the QuizMaster in theQuizMasterService class
in the following way.
privateQuizMasterquizMaster
= new SpringQuizMaster();
To make our quiz master Struts genius we need to make modifications to
the QuizMasterServiceclass like this.
privateQuizMasterquizMaster
= new StrutsQuizMaster();
So it is tightly coupled. Now lets see how we can avoid this by using
the Dependency Injectiondesign pattern. The Spring framework
provides prowerful container to manage the components. The container is based
on the Inversion of Control (IoC) principle and can be implemented by using the
Dependency Injection (DI) design pattern. Here the component only needs to
choose a way to accept the resources and the container will deliver the
resource to the components.
In this example instead of we, directly creating an object of the QuizMaster bean
in theQuizMasterService class, we make use of the container to do this
job for us. Instead of hard coding any values we will allow the container to
inject the required dependancies.
We can inject the dependancies using the setter or constructor
injection. Here we will see how we can do this using the setter injection.
QuizMasterService.java
-----------------------
packagecom.vaannila;
public
class QuizMasterService {
QuizMasterquizMaster;
public
void setQuizMaster(QuizMasterquizMaster) {
this.quizMaster
= quizMaster;
}
public
void askQuestion()
{
System.out.println(quizMaster.popQuestion());
}
}
The value for the QuizMaster will be set using
the setQuizMaster() method. The QuizMaster object is never
instantiated in the QuizMasterService class, but still we
access it. Usually this will throw a NullPointerException, but
here the container will instantiate the object for us, so it works fine.
After making all the changes, the class diagram of the example look like
this.
The container comes into picture and it helps in injecting the
dependancies.
The bean configuration is done in the beans.xml file.
<?xml
version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean
id="springQuizMaster"
class="com.vaannila.SpringQuizMaster"></bean>
<bean
id="strutsQuizMaster"
class="com.vaannila.StrutsQuizMaster"></bean>
<bean
id="quizMasterService"
class="com.vaannila.QuizMasterService">
<property
name="quizMaster">
<ref
local="springQuizMaster"/>
</property>
</bean>
</beans>
We define each bean using the bean tag. The id attribute
of the bean tag gives a logical name to the bean and the class attribute
represents the actual bean class. The property tag is used to
refer the property of the bean. To inject a bean using the setter injection you
need to use the ref tag.
Here a reference of SpringQuizMaster is injected to
the QuizMaster bean. When we execute this example, "Are
you new to Spring?" gets printed in the console.
To make our QuizMaster ask questions related to Struts,
the only change we need to do is, to change the bean reference in the ref tag.
<bean
id="quizMasterService" class="com.vaannila.QuizMasterService">
<property
name="quizMaster">
<ref
local="strutsQuizMaster"/>
</property>
</bean>
In this way the Dependency Injection helps in reducing the coupling
between the components.
To execute this example add the following jar files to the classpath.
antlr-runtime-3.0
commons-logging-1.0.4
org.springframework.asm-3.0.0.M3
org.springframework.beans-3.0.0.M3
org.springframework.context-3.0.0.M3
org.springframework.context.support-3.0.0.M3
org.springframework.core-3.0.0.M3
org.springframework.expression-3.0.0.M3
--------------------------------------------------------------------------------------------------------------------
Spring Objective Type Question
Q 1 - Which of the following is
correct about dependency injection?
A - It helps in decoupling
application objects from each other.
Answer : A
Explanation
Dependency injection helps in
decoupling application objects from each other.
Q 2 - What is bean scope?
A - Bean scope forces Spring
to produce a new bean instance as per the scope defined.
Answer : A
Explanation
Bean scope instructs Spring
Container to produce a new bean instance as per the scope defined.
Hide
Answer
Q 3 - What is request scope?
A - This scopes a bean
definition to an HTTP request.
Answer : A
Explanation
request scope instructs Spring
IoC container to create a instance per HTTP request.
Hide
Answer
Q 4 - What is no mode of
autowiring?
A - Default setting which
means no autowiring and you should use explicit bean reference for wiring.
Answer
: A
Explanation
no mode of autowiring is the
default mode which means no autowiring and you should use explicit bean
reference for wiring.
Hide Answer
Q 5 - What is Advice?
C - This is the actual action
to be taken either before or after the method execution.
Answer : C
Explanation
Advice is the actual action to be
taken either before or after the method execution.
Hide
Answer
Q 6 - Which of the following
aspect implementation spring supports?
C - Both of above.
Answer
: C
Explanation
Spring supports both XML Schema
based and @AspectJ based aspect implementation.
Show Answer
Q 7 - Core container has AOP as
one of its module.
B - False
Answer : B
Explanation
AOP is not the part of spring
core container.
Hide
Answer
Q 8 - Thread scoped bean is
introduced in which version of spring framework.
C - 3.0
Answer : C
Explanation
Thread scoped bean is introduced
in 3.0 version of spring framework.
Show
Answer
Q 9 - Which class is used to
map a database row to a java object in spring?
B - RowMapper
Answer
: B
Explanation
RowMapper class is used to map a
database row to a java object in spring.
Hide Answer
Q 10 - Which of the following
class can be used to call Stored Procedures in spring?
D - SimpleJdbcCall
Answer : D
Explanation
SimpleJdbcCall class can be used
to call Stored Procedures in spring.
------------------------------------------------------------------------------------------------------ Spring MVC Exception Handling
--------------------------------------------------------------------------------------------------------------------
Spring Hibernate Integration Example |