Spring Flashcards
What is IOC?
A key characteristic of IOC is the calling code has the ability to customize the calling methods according to an external configuration (XML) file. The most common implementation of IOC is dependency injection.
What is dependency injection?
A software design pattern that promotes the single responsibility principle, which is simply saying that one module of a project only does one thing, and it does it well.
It uses a Builder pattern to obtain valid instances of your object’s dependencies and pass them to your object during the object’s creation and/or initialization. Using polymorphism and interfaces, the class that needs the dependency does not need to reference the concrete class type. Instead, you declaratively express dependencies through a configuration medium (like XML). The main goal is to decouple classes and test concrete classes in isolation.
What is decoupling?
Decoupling is also known as loose coupling, or loosen the relationship between two classes.
What is isolation testing?
Isolation testing is the process of breaking down the system into various modules so that defects can be spotted easily in isolation.
What are the different types of dependency injection and which does Spring support?
● Constructor Injection (Pico container, Spring etc):
● Setter Injection (eSpring):
● Interface Injection (Avalon): Note: Spring supports only Constructor and Setter Injection
What is Constructor Injection in Spring?
Dependencies are provided as constructor parameters.
We can inject the dependency by constructor. The tag constructor-arg subelement of tag bean is used for constructor injection.
What is Setter Injection in Spring?
Dependencies are assigned through JavaBeans properties (ex: setter methods).
What is Interface Injection in Spring?
Injection is done through an interface.
What are the benefits of IOC?
● Minimizes the amount of code in your application.
● Make your application more testable
● Loose coupling is promoted
● IOC containers support eager instantiation and lazy loading of services.
How does IOC minimize code?
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.
How does IOC make application 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.
How does IOC promote loose coupling?
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.
How does IOC 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.
What is Spring?
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.
What are some features of Spring?
● Lightweight: ● Inversion of control (IOC): ● AsWhat are some features of Spring?pect oriented (AOP): ● Container: ● MVC Framework: ● Transaction Management: ● JDBC Exception Handling:
What makes Spring 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.
What is Spring Inversion Control?
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.
What is Spring Aspect oriented Programming(AOP)?
Spring supports Aspect oriented programming and enables cohesive development by separating application business logic from system services.
What is Spring Container?
Spring contains and manages the life cycle and configuration of application objects.
What is Spring MVC Framework?
Model View Controller
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.
What is Spring 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.
What is Spring 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
Name the modules of Spring.
The basic Spring Framework contains the following libraries grouped by functionality.
- Core Container
- Test
- AOP
- Instrumentation
- Web and Remoting
- Data Access and Integration
What is Bean Factory?
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.
What does a Bean Factory do?
● 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.
What is Application Context?
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.
ApplicationContext will preinstantiate singleton beans and 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.
What is the difference between BeanFactory and ApplicationContext ?
On the surface, an ApplicationContext is the same as a the bean factory, but it offers much more..
● ApplicationContext preinstantiates the beans (Singletons) while BeanFactory does lazy initialization.
● ApplicationContext provides a means for resolving text messages including support for I18N.
● ApplicationContext provide a generic way to load file resources such as images.
● ApplicationContext 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 ApplicationContext itself is a ResourceLoader, Hence provides an application with access to deployment-specific Resource instances.
● MessageSource support: The ApplicationContext implements MessageSource, an interface used to obtain localized messages, with the actual implementation being pluggable.
What are the common implementations of the Application Context ?
The three commonly used implementation of ‘Application Context’ are
●ClassPathXmlApplicationContext: ●FileSystemXmlApplicationContext :
● XmlWebApplicationContext :
What does ClassPathXmlApplicationContext do?
It loads context definition from an XML file located in the classpath, treating context definitions as classpath resources. The application context is loaded from the application’s classpath by using the code .
What does FileSystemXmlApplicationContext do?
It loads a context definition from an XML file in the file system. The application context is loaded from the file system by using the code .
What does XmlWebApplicationContext do?
It loads context definition from an XML file contained within a web application.
What is the typical Bean life cycle in Spring Bean Factory Container ?
Bean life cycle in Spring Bean Factory Container is as follows:
● The spring container finds the bean’s definition from the XML file and instantiates the bean.
● Using the dependency injection, spring populates all of the properties as specified in the bean definition
● If the bean implements the BeanNameAware interface, the factory calls setBeanName() passing the bean’s ID.
● If the bean implements the BeanFactoryAware interface, the factory calls setBeanFactory(), passing an instance of itself.
● If there are any BeanPostProcessors associated with the bean, their post- ProcessBeforeInitialization() methods will be called.
● If an init-method is specified for the bean, it will be called.
● Finally, if there are any BeanPostProcessors associated with the bean, their postProcessAfterInitialization() methods will be called.
What do you mean by bean wiring?
The act of creating associations between application components (beans) within the Spring container is referred to as Bean wiring.
What do you mean by autowiring?
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 four modes. ● byName ● byType ● constructor ● autodetect
What is autowiring byName?
Spring, “Autowiring by Name” means, if the name of a bean is same as the name of other bean property, auto wire it. For example, if a “customer” bean exposes an “address” property, Spring will find the “address” bean in current container and wire it automatically. And if no matching found, just do nothing.
What is autowiring byType?
In Spring, “Autowiring by Type” means, if data type of a bean is compatible with the data type of other bean property, auto wire it. For example, a “person” bean exposes a property with data type of “ability” class, Spring will find the bean with same data type of class “ability” and wire it automatically. And if no matching found, just do nothing.
What is autowiring constructor?
In Spring, “Autowiring by Constructor” is actually autowiring by Type in constructor argument. It means, if data type of a bean is same as the data type of other bean constructor argument, auto wire it.
What is autowiring by autodetect?
In Spring, “Autowiring by AutoDetect“, means chooses “autowire by constructor” if default constructor (argument with any data type), otherwise uses “autowire by type“.
Name the design pattern used in the Spring MVC architecture and the control element?
The design pattern followed by all MVC frameworks including Struts, JSF, and Spring is the Front Controller pattern. The central control element in Spring MVC is the Dispatcher Servlet.
What does DefaultAnnotationHandlerMapping do in Spring MVC?
This is one of five handler mapping classes available in Spring. It is used by DispatcherServlet to map requests to specific controllers and controller methods annotated with @RequestMapping.
What does the ViewResolver do in Spring MVC?
The last task to be performed in HTTP request handling is rendering output to the user. The ViewResolver exchanges a logical view name supplied by the controller for a an actual view (JSP) that renders the result. There are 12 ViewResolver classes to choose from depending on the desired rendering technology.
How do you turn ON annotation support in Spring MVC?
Use tag mvc:annotation-driven in mvc-dispatcher-servlet.xml to switch ON annotation support, form validation, message conversion, and field formatting.
List the annotations used to configure a basic controller class in Spring MVC.
The annotations required to configure a basic spring controller class are:
@Controller
@RequestMapping