What is Spring?
Spring is
a framework for developing enterprise Java applications. The
benefit of using Spring over other frameworks is that it’s open source. This
means developers can build reusable code without any vendor lock-in. Another
major advantage of Spring is the layered architecture that enables you to
select only the components you need while offering a seamless J2EE application
development framework.
List the advantages of the Spring framework.
Spring
has the following advantages:
- Layered architecture that allows you to use what you need while leaving what you don’t need.
- Spring allows developers to focus on Plain Old Java Object (POJO) Programming. This allows for continuous testing and integration.
- Being an open source, there is no vendor lock-in.
- Dependency injection and inversion of control makes Java Database Connectivity (JDBC) simpler.
List some features of Spring.
Container:
Spring
manages and contains the configuration and life cycle of application objects.
Lightweight:
When it
comes to transparency and size, Spring is a lightweight application framework. The
lightest version of the Spring framework only takes up 1MB. Additionally, the
overhead in terms of processing is similarly minuscule.
MVC
Framework:
Spring
utilizes the model-view-controller (MVC) web application framework which is
built on the core Spring functionality. This framework accommodates several
view technologies such as JSP, Tiles, Velocity, POI, and iText. Additionally,
it is highly configurable with the use of strategy interfaces. However, several
other frameworks could be easily used in place of the Spring MVC Framework. You
can learn more about other MVC frameworks from this
Inversion
Of Control (IOC):
Spring
achieves loose coupling through the use of Inversion of Control. Objects
provide their dependencies rather than looking for or creating dependent
objects.
Transaction
Management:
A generic
abstraction layer is supplied by the Spring framework for the purpose of
transaction management. This allows developers to include pluggable transaction
managers while making it very easy to separate transactions while avoiding
low-level issues. This transaction support is not connected with the J2REE
environment. Also, it can be utilized in containerless environments.
Aspect
Oriented Programming (AOP):
By
separating system services from application business logic, Spring supports
aspect oriented programming. This also allows for cohesive deployment of
applications.
JDBC
Exception Handling:
The Java
Database Connectivity (JDBC) abstraction layer of Spring provides a useful
exception hierarchy. This makes error handling strategy much easier to develop.
Additionally, Spring offers great integration services with JDO, iBATIS, and
Hibernate.
What is Dependency Injection (AKA IOC)?
Dependency
Injection or Inversion of Control (IOC), at the most basic level, allows you to
describe how objects should be created rather than creating them directly. That
is, you describe the services that are needed by different components using a
configuration file rather than directly connecting these components and
services in code. In the case of the Spring framework, these services and
components are then connected by the IOC container.
For
example, objects are given their dependencies when they are created by an
external process that manages each object within a system. In other words, the
dependencies are inserted into objects. This is contrary from the way
dependencies are handled in other frameworks, which is why the term inversion
of control was coined. This signifies an inversion of responsibility for
creating references to dependencies within objects. Here’s a course that can
teach you more about the basics of Spring 3.2 Framework.
List some different types of Dependency Injection
(IOC)
The three
(3) types of dependency injection, or IOC, are:
- Setter Injection (used by Spring): JavaBeans properties are used to assign dependencies.
- Interface Injection (used by Avalon): An interface is used for injection.
- Constructor Injection (used by Spring, Pico container, and others): Constructor parameters are used to provide dependencies.
It’s
important to note that Spring only supports Setter Injection and Constructor
Injection.
1) What is spring framework? Why Java programmer should
use Spring framework
Very common Spring interview question, Spring is a framework which helps Java programmer in development. Spring provides dependency Injection and IOC container, Spring MVC flow and several useful API for Java programmer.
2) What is default scope of bean in Spring framework ?
default scope of bean is Singleton, you can read this article which explains about all possible scope of a spring bean :
3) Does Spring singleton beans are thread-safe ?
No, Spring singleton beans are not thread-safe. Singleton doesn't mean bean would be thread-safe
4) What is dependency Injection?
Dependency Injection is one of the design pattern, which allows to inject dependency on Object, instead of object resolving dependency.
5) What is Inversion of Control concept, how does Spring support IOC?
6) What is Spring MVC ? Can you explain How one request is processed ?
7) How to you create controller in Spring ?
8) What is view Resolver pattern ? how it work in Spring MVC
View Resolver patter is a J2EE pattern which allows a web application to dynamically choose it's view technology e.g. HTML, JSP, Tapestry, JSF, XSLT or any other view technology. In this pattern, View resolver holds mapping of different views, controller return name of view, which is than passed to View Resolver for selecting appropriate view. Spring MVC framework supplies inbuilt view resolver for selecting views.
9) What is Spring Security ?
Spring security is a project under spring framework umbrella, which provides support for security requirements of enterprise Java projects. Spring Security formerly known as aegis security provides out of box support for creating login screen, remember me cookie support, securing URL, authentication provider to authenticate user from database, LDAP and in memory, concurrent active session management support and many more. In order to use Spring security in a Spring MVC based project, you need to include spring-security.jar and configure it in application-Context-security.xml file, you can name it whatever you want, but make sure to supply this to ContextLoaderListener, which is responsible for creating Spring context and initializing dispatcher servlet.
10) How do you control concurrent Session on Java web application using Spring Security.
11) What types of dependency injection is supported by Spring Framework? When do you use Setter and Constructor Injection, pros and cons?
12) What is difference between ApplicationContext and BeanFactory in Spring framework?
13) How do you call stored procedure by using Spring framework?
14) What does JdbcTemplate and JmsTemplate class offer in Spring?
15) Can we use more than one configuration file for our Spring project?
These were some of the Core Spring framework and MVC Interview questions from my collection, I have given short answers for most of these Spring interview question. I suggest to research more or read along those Spring question to prepare for follow up Spring interview questions.
Very common Spring interview question, Spring is a framework which helps Java programmer in development. Spring provides dependency Injection and IOC container, Spring MVC flow and several useful API for Java programmer.
2) What is default scope of bean in Spring framework ?
default scope of bean is Singleton, you can read this article which explains about all possible scope of a spring bean :
3) Does Spring singleton beans are thread-safe ?
No, Spring singleton beans are not thread-safe. Singleton doesn't mean bean would be thread-safe
4) What is dependency Injection?
Dependency Injection is one of the design pattern, which allows to inject dependency on Object, instead of object resolving dependency.
5) What is Inversion of Control concept, how does Spring support IOC?
6) What is Spring MVC ? Can you explain How one request is processed ?
7) How to you create controller in Spring ?
8) What is view Resolver pattern ? how it work in Spring MVC
View Resolver patter is a J2EE pattern which allows a web application to dynamically choose it's view technology e.g. HTML, JSP, Tapestry, JSF, XSLT or any other view technology. In this pattern, View resolver holds mapping of different views, controller return name of view, which is than passed to View Resolver for selecting appropriate view. Spring MVC framework supplies inbuilt view resolver for selecting views.
9) What is Spring Security ?
Spring security is a project under spring framework umbrella, which provides support for security requirements of enterprise Java projects. Spring Security formerly known as aegis security provides out of box support for creating login screen, remember me cookie support, securing URL, authentication provider to authenticate user from database, LDAP and in memory, concurrent active session management support and many more. In order to use Spring security in a Spring MVC based project, you need to include spring-security.jar and configure it in application-Context-security.xml file, you can name it whatever you want, but make sure to supply this to ContextLoaderListener, which is responsible for creating Spring context and initializing dispatcher servlet.
10) How do you control concurrent Session on Java web application using Spring Security.
11) What types of dependency injection is supported by Spring Framework? When do you use Setter and Constructor Injection, pros and cons?
12) What is difference between ApplicationContext and BeanFactory in Spring framework?
13) How do you call stored procedure by using Spring framework?
14) What does JdbcTemplate and JmsTemplate class offer in Spring?
15) Can we use more than one configuration file for our Spring project?
These were some of the Core Spring framework and MVC Interview questions from my collection, I have given short answers for most of these Spring interview question. I suggest to research more or read along those Spring question to prepare for follow up Spring interview questions.
Question
1: What is difference between a process and thread?
A process
is a single execution of a program whereas a thread is a single execution
sequence within a process. A process may contain multiple threads. A thread is
sometimes referred to as a lightweight process. Threads in a JVM share the same
heap space. Hence, threads can share same object. Threads have their own stack
space, this is how one invocation of a method and its local variables are kept
thread safe. Heap is not thread safe and hence must be synchronised for thread
safety.
Question 2: What are the different ways of creating Threads?
1) By implementing Runnable
2) By Extending Thread class
3) By using Executor Framework
Question 3: Which of the above method should be use?
Generally we should try to create threads by implementing Runnable as it will come in handy, when we need multiple inheritance.
Question 4: What are Thread states possible?
Thread can have the following states:
1) Runnable - Thread has started and is waiting for the Thread Scheduler to pick and execute.
2) Running - Thread is actively executing the code.
3) Waiting - Waiting for some external event like finish a file I/O to finish.
4) Sleeping - Thread are put to sleep for a certain period of time and will become runnable when it wakes from sleeping duration.
5) Blocked on I/O or Synchronised: Blocked by an external call to I/O or waiting to acquire lock.
6) Dead - Thread has finished executing code.
Question 5: What is difference between sleep and
wait method?
Sleep method is used to stop the thread execution for a certain period of time, whereas wait method is used to make the thread wait for some external event or woke up by another thread by calling notify() or notifyAll().
Question 6: What is the difference between notify() and notifyAll()?
notify() and notifyAll() methods are used to pass the control to another thread that might be in the waiting state. When we call notify() it only means that the call will wake up the thread next in line. However, by calling notifyAll() we are actually passing the control to a larger number of threads which in turn may again compete to get control.
Question 7: What is difference between Thread.start() and Thread.run()?
Thread.start() actually does the job of calling Thread.run(). If we call the Thread.run() directly then it would be a simple method call from the main thread. It will not cause the thread to run independently.
Question 8: When is the InvalidMonitorStateException is thrown? why?
This exception is thrown when we try to call wait(), notify() or notifyAll() methods on an object in your program where you do not have the lock on the object. InvalidMonitorStateException extends RuntimeException and hence we do not need to catch it.
Question 9: What happens if the static method is defined as synchronized?
Executing such a method would mean that the thread has acquired a "Class" level lock, which means other threads cannot executed any other "static synchronized" method. This is different then the instance method synchronization in the way that two threads can execute the same synchronized instance method as long as they are executing it on the different instance of the object.
Sleep method is used to stop the thread execution for a certain period of time, whereas wait method is used to make the thread wait for some external event or woke up by another thread by calling notify() or notifyAll().
Question 6: What is the difference between notify() and notifyAll()?
notify() and notifyAll() methods are used to pass the control to another thread that might be in the waiting state. When we call notify() it only means that the call will wake up the thread next in line. However, by calling notifyAll() we are actually passing the control to a larger number of threads which in turn may again compete to get control.
Question 7: What is difference between Thread.start() and Thread.run()?
Thread.start() actually does the job of calling Thread.run(). If we call the Thread.run() directly then it would be a simple method call from the main thread. It will not cause the thread to run independently.
Question 8: When is the InvalidMonitorStateException is thrown? why?
This exception is thrown when we try to call wait(), notify() or notifyAll() methods on an object in your program where you do not have the lock on the object. InvalidMonitorStateException extends RuntimeException and hence we do not need to catch it.
Question 9: What happens if the static method is defined as synchronized?
Executing such a method would mean that the thread has acquired a "Class" level lock, which means other threads cannot executed any other "static synchronized" method. This is different then the instance method synchronization in the way that two threads can execute the same synchronized instance method as long as they are executing it on the different instance of the object.
Q. What do you understand by the
terms Dependency Inversion Principle (DIP), Dependency Injection (DI)
and Inversion of Control (IoC) container ?
A.
A.
- Dependency Inversion Principle (DIP) is a design principle which is in some ways related to the Dependency Injection (DI) pattern. The idea of DIP is that higher layers of your application should not directly depend on lower layers. Dependency Inversion Principle does not imply Dependency Injection. This principle doesn’t say anything about how higher la yers know what lower layer to use. This could be done as shown below by coding to interface using a factory pattern or through Dependency Injection by using an IoC container like Spring framework, Pico container, Guice, or Apache HiveMind.
The Dependency Inversion Principle (DIP) states that
- High level modules should not depend upon low level modules. Both should depend upon abstractions.
- Abstractions should not depend upon details. Details should depend upon abstractions.
When this principle is applied,
the higher level classes will not be working directly with the lower level
classes, but with an abstract layer. This gives us the flexibility at the cost
of increased effort.Here are some code snippets for DIP.
Firstly define the abstraction layer.
Firstly define the abstraction layer.
1
2
3
4
5
|
<span class="IL_AD" id="IL_AD4">package</span>
principle_dip2;
public interface AnimalHandler {
public abstract void handle( );
}
|
1
2
3
4
5
|
package principle_dip2;
public interface AnimalHelper {
public abstract void help( );
}
|
Now the implementation that depends on the abstraction as opposed to the implementation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
package principle_dip2;
public class CircusService {
AnimalHandler handler;
public void setHandler(AnimalHandler handler) {
this.handler =
handler;
}
public void showStarts( ) {
//code omitted for
brevity
handler.handle( );
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
package principle_dip2;
public class TigerHandler implements
AnimalHandler{
AnimalHelper helper;
public void setHelper(AnimalHelper helper) {
this.helper = helper;
}
public void handle( ){
//...
helper.help( );
//...
}
}
|
1
2
3
4
5
6
7
8
|
package principle_dip2;
public class TigerHelper implements
AnimalHelper{
public void help( ){
//......
}
}
|
- Dependency Injection (DI) is a pattern of injecting a class’s dependencies into it at runtime. This is achieved by defining the dependencies as interfaces, and then injecting in a concrete class implementing that interface to the constructor. This allows you to swap in different implementations without having to modify the main class. The Dependency Injection pattern also promotes high cohesion by promoting the Single Responsibility Principle (SRP), since your dependencies are individual objects which perform discrete specialized tasks like data access (via DAOs) and business services (via Service and Delegate classes) .
- The Inversion of Control Container (IoC) is a container that supports Dependency Injection. In this you use a central container like Spring framework, Guice, or HiveMind, which defines what concrete classes should be used for what dependencies throughout your application. This brings in an added flexibility through looser coupling, and it makes it much easier to change what dependencies are used on the fly. 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 container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up. Applying IoC, objects are given their dependencies at creation time by some external entity that coordinates each object in the system. That is, dependencies are injected into objects. So, IoC means an inversion of responsibility with regard to how an object obtains references to collaborating objects.
The real power of DI and IoC is realized in its ability to replace the compile time binding of the relationships between classes with binding those relationships at runtime. For example, in Seam framework, you can have a real and mock implementation of an interface, and at runtime decide which one to use based on a property, presence of another file, or some precedence values. This is incredibly useful if you think you may need to modify the way your application behaves in different scenarios. Another real benefit of DI and IoC is that it makes your code easier to unit test. There are other benefits like promoting looser coupling without any proliferation of factory and singleton design patterns, follows a consistent approach for lesser experienced developers to follow, etc. These benefits can come in at the cost of the added complexity to your application and has to be carefully manged by using them only at the right places where the real benefits are realized, and not just using them because many others are using them.
Note: The CDI (Contexts and Dependency Injection) is an attempt at describing a true standard on Dependency Injection. CDI is a part of the Java EE 6 stack, meaning an application running in a Java EE 6 compatible container can leverage CDI out-of-the-box. Weld is the reference implementation of CDI.
Q. In your experience, why would you use Spring framework?
A.
- Spring has a layered architecture with over 20 modules to choose from. This means, use what you need and leave what you don't need now. Spring simplifies JEE through POJO programming. There is no behind the scene magic in Spring as in JEE programming. POJO programming enables continuous integration and testability.
- Spring framework's core functionality is dependency injection (DI). Dependency injection promotes easy unit testing and more maintainable and flexible code. DI code is much easier to test. The functionality expressed by the object can be tested in a black box by building 'mock' objects implementing the interfaces expected by your application logic. DI code is much easier to reuse as the 'depended' functionality is extrapolated into well defined interfaces, allowing separate objects whose configuration is handled by a suitable application platform to be plugged into other objects at will. DI code is more flexible. It is innately loosely coupled code to an extreme. This allows the programmer to pick and choose how objects are connected based exclusively on their required interfaces on one end and their expressed interfaces on the other.
- Spring supports Aspect Oriented Programming (AOP), which enables cohesive development by separating application business logic from system services. Supporting functionalities like auditing, gathering performance and memory metrics, etc can be enabled through AOP.
- Spring also provides a lot of templates which act as base classes to make using the JEE standard technologies a breeze to work with. For example, the JdbcTemplate works well with JDBC, the JpaTemplate does good things with JPA, JmsTemplate makes JMS pretty straightforward. The RestTemplate is simply awesome in it's simplicity. Simplicity means more readable and maintainable code.
- When writing software these days, it is important to try and decouple as much middleware code from your business logic as possible. The best approach when using remoting is to use Spring Remoting which can then use any messaging or remoting technology under the covers. Apache Camel is a powerful open source integration framework based on known Enterprise Integration Patterns with powerful Bean Integration. Apache Camel is designed to work nicely with the Spring Framework in a number of ways.
- It also provides declarative transactions, job scheduling, authentication, a fully-fledged MVC web framework, and integration to other frameworks like Hibernate, iBatis, JasperReports, JSF, Struts, Tapestry, Seam, Quartz job scheduler, etc.
- Spring beans can be shared between different JVMs using Terracotta. This allows you to take existing beans and spread them across a cluster, turn Spring application context events into distributed events, export clustered beans via Spring JMX, and make your Spring applications highly available and clustered. Spring also integrate well with other clustering solutions like Oracle's Coherance.
- Spring favors unchecked exceptions and eliminates unsightly try, catch, and finally (and some times try/catch within finally itself) blocks. The Spring templates like JpaTemplate takes care of closing or releasing a database connection. This prevents any potential resource leaks and promotes more readable code.
- It prevents the proliferation of factory and singleton pattern classes that need to be created to promote loose coupling if not for using a DI framework like Spring or Guice.
Q. In your experience, what
do you don't like about Spring? Are there any pitfalls?
A.
A.
- Spring has become very huge and bulky. So, don't over do it by using all its features because of the hype that Spring is good. Look at what parts of Spring really provides some benefits for your project and use those parts. In most cases, it is much better to use proven frameworks like Spring than create your own equivalent solution from a maintenance and applying the best practices perspective. For example, all spring templates (jdbc, rest, jpa etc.) have the following advantages -- perform common setup routines for you, let you skip the boilerplate and concentrate on the logic you want.
- Spring MVC is probably not the best Web framework. There are other alternatives like Struts 2, Wicket, and JSF. Having said this, Spring integrates well with the other Web frameworks like Struts, JSF, etc.
- The XML files can get bloated. This can be minimized by carefully considering other options like annotations, JavaConfig, and having separate XML configuration files.
Q. What are the different types of IoC (dependency injection) ?
A. There are three types of dependency injection:
- Constructor Injection (e.g. Spring): Dependencies are provided as constructor parameters.
- Setter Injection (e.g. Spring): Dependencies are assigned through JavaBeans properties (ex: setter methods).
- Interface Injection (e.g. Avalon): Injection is done through an interface.
Q. Have you used any other Dependency Injection (DI) frameworks?
A. Yes, Guice, Hivemind, and Seam.
Spring interview questions and answers are below
Questions : 1
|
What is IOC (or Dependency Injection)?
|
Answers : 1
|
The basic
concept of the Inversion of Control pattern (also known as dependency
injection) 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 container (in the case of the Spring framework,
the IOC container) is then responsible for hooking it all up. i.e., Applying IoC, objects are given their dependencies at creation time by some external entity that coordinates each object in the system. That is, dependencies are injected into objects. So, IoC means an inversion of responsibility with regard to how an object obtains references to collaborating objects. . |
Questions : 2
|
What are the different types of IOC
(dependency injection) ?
|
Answers : 2
|
There are
three types of dependency injection: ==>Constructor Injection (e.g. Pico container, Spring etc): Dependencies are provided as constructor parameters. ==>Setter Injection (e.g. Spring): Dependencies are assigned through JavaBeans properties (ex: setter methods). ==>Interface Injection (e.g. Avalon): Injection is done through an interface. Note: Spring supports only Constructor and Setter Injection |
Questions : 3
|
What are the benefits of IOC (Dependency
Injection)?
|
Answers : 3
|
Benefits
of IOC (Dependency Injection) are as follows: · Minimizes the amount of code in your application. With IOC containers you do not care about how services are created and how you get references to the ones you need. You can also easily add additional services by adding a new constructor or a setter method with little or no extra configuration. · Make your application more testable by not requiring any singletons or JNDI lookup mechanisms in your unit test cases. IOC containers make unit testing and switching implementations very easy by manually allowing you to inject your own objects into the object under test. · Loose coupling is promoted with minimal effort and least intrusive mechanism. The factory design pattern is more intrusive because components or services need to be requested explicitly whereas in IOC the dependency is injected into requesting piece of code. Also some containers promote the design to interfaces not to implementations design concept by encouraging managed objects to implement a well-defined service interface of your own. · IOC containers support eager instantiation and lazy loading of services. Containers also provide support for instantiation of managed objects, cyclical dependencies, life cycles management, and dependency resolution between managed objects etc. |
Questions : 4
|
What is Spring ?
|
Answers : 4
|
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. . |
Questions : 5
|
What are the advantages of Spring framework?
|
Answers : 5
|
The
advantages of Spring are as follows: 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 continuous integration and testability. Dependency Injection and Inversion of Control Simplifies JDBC Open source and no vendor lock-in. . |
Questions : 6
|
What are features of Spring ?
|
Answers : 6
|
Lightweight:
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 (AOP): Spring supports 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 strategy. Integration with Hibernate, JDO, and iBATIS: Spring provides best Integration services with Hibernate, JDO and iBATIS |
Questions : 7
|
How many modules are there in Spring? What
are they?
|
Answers : 7
|
Spring
comprises of seven modules. They are.. The 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. |
Questions : 8
|
What are the types of Dependency Injection
Spring supports?>
|
Answers : 8
|
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 |
Questions : 9
|
What is Bean Factory ?
|
Answers : 9
|
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. |
Questions : 10
|
What is Application Context?
|
Answers : 10
|
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. But it also provides: A means for resolving text messages, including support for internationalization. A generic way to load file resources. Events to beans that are registered as listeners. |
Questions : 11
|
What is the difference between Bean Factory
and Application Context
|
Answers : 11
|
On the
surface, an application context is same as a bean factory. But application
context offers much more.. 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 |
Questions : 12
|
What do you mean by Bean wiring ?
|
Answers : 12
|
The act of creating associations between application components (beans) within the Spring container is reffered to as Bean wiring. |
Questions : 13
|
What do you mean by Auto Wiring?
|
Answers : 13
|
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. · no · byName · byType · constructor · autodirect |
Questions : 14
|
What is DelegatingVariableResolver?
|
Answers : 14
|
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 |
Questions : 15
|
How to integrate Java Server Faces (JSF) with
Spring?
|
Answers : 15
|
? JSF and
Spring do share some of the same features, most noticeably in the area of IOC
services. By declaring JSF managed-beans in the faces-config.xml
configuration file, you allow the FacesServlet to instantiate that bean at
startup. Your JSF pages have access to these beans and all of their
properties.We can integrate JSF and Spring in two ways: DelegatingVariableResolver: Spring comes with a JSF variable resolver that lets you use JSF and Spring together. < ?xml version="1.0" encoding="UTF-8 "? > < ! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd" > < faces-config > < application > < variable-resolver > org.springframework.web.jsf.DelegatingVariableResolver < / variable-resolver > < / application > < / faces-config > The DelegatingVariableResolver will first delegate value lookups to the default resolver of the underlying JSF implementation, and then to Spring's 'business context' WebApplicationContext. This allows one to easily inject dependencies into one's JSF-managed beans. FacesContextUtils:custom VariableResolver works well when mapping one's properties to beans in faces-config.xml, but at times one may need to grab a bean explicitly. The FacesContextUtils class makes this easy. It is similar to WebApplicationContextUtils, except that it takes a FacesContext parameter rather than a ServletContext parameter. ApplicationContext ctx = FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()); |
Questions : 16
|
What is Java Server Faces (JSF) - Spring
integration mechanism?
|
Answers : 16
|
Spring
provides a custom JavaServer Faces VariableResolver implementation that
extends the standard JavaServer Faces managed beans mechanism. When asked to
resolve a variable name, the following algorithm is performed: Does a bean with the specified name already exist in some scope (request, session, application)? If so, return Is there a standard JavaServer Faces managed bean definition for this variable name? If so, invoke it in the usual way, and return the bean that was created. Is there configuration information for this variable name in the Spring WebApplicationContext for this application? If so, use it to create and configure an instance, and return that instance to the caller. If there is no managed bean or Spring definition for this variable name, return null instead. BeanFactory also takes part in the life cycle of a bean, making calls to custom initialization and destruction methods. As a result of this algorithm, you can transparently use either JavaServer Faces or Spring facilities to create beans on demand. e. |
Questions : 17
|
How to integrate your Struts application with
Spring?
|
Answers : 17
|
To
integrate your Struts application with Spring, we have two options: 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 agetWebApplicationContext() |
Questions : 18
|
What are the ways to access Hibernate using
Spring ?
|
Answers : 18
|
There are
two approaches to Spring’s Hibernate integration: Inversion of Control with a HibernateTemplate and Callback Extending HibernateDaoSupport and Applying an AOP Interceptor nodes. |
Questions : 19
|
What is AOP? and How the AOP used in Spring?
|
Answers : 19
|
Aspect-oriented
programming, or AOP, is a programming technique that allowsprogrammers to
modularize crosscutting concerns, or behavior that cuts across the typical
divisions of responsibility, such as logging and transaction management. The
core construct of AOP is the aspect, which encapsulates behaviors affecting
multiple classes into reusable modules. 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, which builds on the Spring Framework's transaction abstraction.To allow users to implement custom aspects, complementing their use of OOP with AOP. |
Questions : 20
|
Explain about the Spring DAO support
|
Answers : 20
|
The Data Access Object (DAO) support in Spring is aimed at making it easy to work with data access technologies like JDBC, Hibernate or JDO in a consistent way. This allows one to switch between the persistence technologies fairly easily and it also allows one to code without worrying about catching exceptions that are specific to each technology. |
Questions : 21
|
What is SQLExceptionTranslator ?
|
Answers : 21
|
SQLExceptionTranslator, is an interface to be implemented by classes that can translate between SQLExceptions and Spring's own data-access-strategy-agnostic org.springframework.dao.DataAccessException. |
No comments:
Post a Comment