https://spring.io/guides/gs/rest-service/
http://www.javacodegeeks.com/2014/05/spring-interview-questions-and-answers.html#2
1. What is Spring?
It is a lightweight, loosely coupled and integrated framework for developing enterprise applications in java.
2. What are the advantages of spring framework?
- Predefined Templates
- Loose Coupling
- Easy to test
- Lightweight
- Fast Development
- Powerful Abstraction
- Declarative support
3. What are the modules of spring framework?
- Test
- Spring Core Container
- AOP, Aspects and Instrumentation
- Data Access/Integration
- Web
4. What is Dependency Injection?
Dependency Injection is a design pattern to provide loose coupling. It removes the dependency from the program, which says 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 IoC container is then responsible for hooking it all up.
5. What is the role of IOC container in spring?
The IoC container is responsible to instantiate
, configure
and assemble the objects
. The IoC container gets information from the XML file and works accordingly. The main tasks performed by IoC container are:
- to instantiate the application class
- to configure the object
- to assemble the dependencies between the objects
6. What are the types of IOC container in spring?
- BeanFactory
- ApplicationContext
7. What is the difference between BeanFactory and ApplicationContext?
ApplicationContext extends the BeanFactory interface. ApplicationContext provides more facilities than BeanFactory
Application contexts can publish events to beans that are registered as listeners.
Application contexts provide a means for resolving text messages, including support for i18n of those messages.
BeanFactory instantiate bean when you call getBean() method while ApplicationContext instantiate Singleton bean when container is started, It doesn't wait for getBean() to be called.
BeanFactory is the actual container which instantiates, configures, and manages a number of beans. These beans typically collaborate with one another, and thus have dependencies between themselves. These dependencies are reflected in the configuration data used by the BeanFactory. A BeanFactory is represented by the interface "org.springframework.beans.factory".
8. What is the difference between constructor injection and setter injection?
- Partial dependency: can be injected using setter injection but it is not possible by constructor. Suppose there are 3 properties in a class, having 3 arg constructor and setters methods. In such case, if you want to pass information for only one property, it is possible by setter method only.
- Overriding: Setter injection overrides the constructor injection. If we use both constructor and setter injection, IOC container will use the setter injection.
- Changes: We can easily change the value by setter injection. It doesn't create a new bean instance always like constructor. So setter injection is flexible than constructor injection.
9. What is autowiring in spring? What are the autowiring modes?
Spring Auto-Wiring Beans
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 without using <constructor-arg>
and <property>
elements.
The autowiring functionality has five modes: no
, byName
, byType
, constructor
, detct
.
10. Explain Bean lifecycle in Spring framework
- The spring container finds the bean’s definition from the XML file and instantiates the bean.
- Spring populates all of the properties as specified in the bean definition (DI).
- If the bean implements
BeanNameAware
interface, spring passes the bean’s id to setBeanName() method. - If Bean implements
BeanFactoryAware
interface, spring passes the beanfactory to setBeanFactory() method. - If there are any bean
BeanPostProcessors
associated with the bean, Spring calls postProcesserBeforeInitialization() method. - If the bean implements
IntializingBean
, its afterPropertySet() method is called. If the bean has init method declaration, the specified initialization method is called. - If there are any
BeanPostProcessors
associated with the bean, their postProcessAfterInitialization() methods will be called. - Then we can use this bean.
- If the bean implements
DisposableBean
, it will call the destroy() method.
11.