Pages

Spring


What is Spring ?


Spring was developed by Rod Johnson in 2003. Spring framework makes the easy development of Java Web Application. Spring is a lightweight framework. Spring can be thought of as a framework of frameworks because it provides support to various frameworks like Hibernate, Struts, Java Persistance API.

What are Spring Modules ?


Types of Spring Modules :
  1. Test
  2. Core Container
  3. AOP
  4. Aspects
  5. Instrumentation
  6. Data Access and Integration
  7. Web and Remoting

What is Spring IOC Container ?


Definition : The Spring Container creates the object, wires them together, configures them and manages their complete life cycle. The Spring container makes use of Dependency Injection to manage the components that make up an application. The container receives instructions for which objects to instantiate, configure, and assemble by reading the configuration metadata provided. This metadata can be provided either by XML, Java annotations or Java code.

Types of Spring IOC Container :
  1. Bean Factory
  2. Application Context

What do you mean by Dependency Injection ?


Definition : you do not have to create your objects but have to describe how they should be created. You don’t connect your components and services together in the code directly, but describe which services are needed by which components in the configuration file. The IoC container will wire them up together.

Types of Dependency Injection :
  1. Constructor Injection
  2. Setter Injection
  3. Interface Injection

How many types of IOC containers are there in spring ?


BeanFactory : BeanFactory is like a factory class that contains a collection of beans. It instantiates the bean whenever asked for by clients.

ApplicationContext : The ApplicationContext interface is built on top of the BeanFactory interface. It provides some extra functionality on top BeanFactory.

What is Bean in Spring Framework ?


The objects that form the backbone of your application and that are managed by the Spring IoC container are called beans. A bean is an object that is instantiated, assembled, and otherwise managed by a Spring IoC container. These beans are created with the configuration metadata that you supply to the container.

What is the Bean life cycle in Spring Bean Factory Container ?


Bean Life Cycle :
  1. Instantiate
  2. Populate Properties
  3. Set Bean Name
  4. Set Bean Factory
  5. Pre Initialization
  6. Initialize Bean
  7. Post Initialization
  8. Ready to Use
  9. Destroy

What is the Spring MVC Execution Flow ?


Execution Flow :
  1. When a client request is given, a web container will receive request.
  2. Web Container loads web.xml file and verifies whether the url pattern are verified or not.
  3. If matches web container transfer the request to Dispatcher Servlet.
  4. Dispatcher Servlet will take help of HandlerMapping and get to know the controller class name associated with the given request.
  5. So, request transfer to the Controller and then Controller will process the request by executing appropriate methods and return ModelAndView Object back to the DispatcherServlet.
  6. Now, DispatcherServlet send the model object to the ViewResolver to get the actual view page.
  7. Finally, DispatcherServlet will pass the model object to the view page to display the result.

List some of the Spring and Spring Boot Annotation ?


@Configuration @Bean @ComponentScan @Component
@Service @PropertySource @Repository @Autowired
@Controller @RequestMapping @PathVariable @RequestParam
@ModelAttribute @RequestBody @RequestHeader @ResponseBody
@ResponseHeader @Transactional @RequestPart @ResponseStatus
@EnableWebMVC @EnableWebSecurity @RestController @SpringBootApplication
@SpringBootConfiguration @EnableAutoConfiguration @GetMapping @PostMapping
@PutMapping @DeleteMapping @PatchMapping @ExceptionHandler
@Scheduled @EnableConfiServer @Lazy @Required
@Scope @Value @Qulifier @Mapping & @Mappings


Differentiate between Constructor Injection and Setter Injection ?


Constructor Injection Setter Injection
Constructor Injection can not be partial injection Setter Injection can be partial injection.
Constructor Injection doesn’t override the setter property Setter Injection overrides the constructor property
Constructor Injection will create a new instance if any modification is done Setter Injection will not create new instance if any modification is done.
Constructor Injection works better for many properties Setter Injection works better for few properties.


Differentiate between BeanFactory and ApplicationContext ?


BeanFactory ApplicationContext
Bean Factory is an interface defined in org.springframework.beans.factory.BeanFactory Application Context is an interface defined in org.springframework.context.ApplicationContext
BeanFactory uses Lazy initialization ApplicationContext uses Eager/Aggressive initialization
BeanFactory explicitly provides a resource object using the syntax ApplicationContext creates and manages resource objects on its own
BeanFactory doesn’t supports internationalization ApplicationContext supports internationalization
BeanFactory doesn’t supports annotation based dependency ApplicationContext supports annotation based dependency

Differentiate between Spring and Struts Framework ?


Spring Framework Struts Framework
Spring is an application framework Struts is a web framework
Spring implements IOC Desgin Pattern Struts implements MVC Design Pattern
Spring can provides easy integration Struts can not provides easy integration
Spring is light weight Struts is heavy weight
Spring is layed architecture Struts is not a layed architecture
Spring is loosely coupled Struts is tightly coupled
Spring is a non-invarsive framework Struts is a invarsive framework