Spring Flashcards

1
Q

What is IOC?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is dependency injection?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is decoupling?

A

Decoupling is also known as loose coupling, or loosen the relationship between two classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is isolation testing?

A

Isolation testing is the process of breaking down the system into various modules so that defects can be spotted easily in isolation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is Constructor Injection in Spring?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Setter Injection in Spring?

A

Dependencies are assigned through JavaBeans properties (ex: setter methods).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is Interface Injection in Spring?

A

Injection is done through an interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the benefits of IOC?

A

● 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does IOC minimize code?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How does IOC make application testable?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How does IOC promote loose coupling?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How does IOC support eager instantiation and lazy loading of services?

A

Containers also provide support for instantiation of managed objects, cyclical dependencies, life cycles management, and dependency resolution between managed objects etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is Spring?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are some features of Spring?

A
● Lightweight: 
● Inversion of control (IOC): 
● AsWhat are some features of Spring?pect oriented (AOP): 
● Container: 
● MVC Framework: 
● Transaction Management: 
● JDBC Exception Handling:
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is Spring Inversion Control?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Spring Aspect oriented Programming(AOP)?

A

Spring supports Aspect oriented programming and enables cohesive development by separating application business logic from system services.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is Spring Container?

A

Spring contains and manages the life cycle and configuration of application objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

What is Spring MVC Framework?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Name the modules of Spring.

A

Spring MVC, Spring Data, Spring Test, Spring ORM, Spring Rest

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

What is Bean Factory?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What does a Bean Factory do?

A

● 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What is Application Context?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

ApplicationContext will preinstantiate singleton beans and also provides:

A

● 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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

What is the difference between BeanFactory and ApplicationContext ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

What are the common implementations of the Application Context ?

A

The three commonly used implementation of ‘Application Context’ are
●ClassPathXmlApplicationContext: ●FileSystemXmlApplicationContext :
● XmlWebApplicationContext :

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

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

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

What do you mean by bean wiring?

A

The act of creating associations between application components (beans) within the Spring container is referred to as Bean wiring.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

What do you mean by autowiring?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

What is autowiring byName?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

What is autowiring byType?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

What is autowiring constructor?

A

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.

32
Q

What is autowiring by autodetect?

A

In Spring, “Autowiring by AutoDetect“, means chooses “autowire by constructor” if default constructor (argument with any data type), otherwise uses “autowire by type“.

33
Q

List the annotations used to configure a basic controller class in Spring MVC.

A

The annotations required to configure a basic spring controller class are:
@Controller
@RequestMapping

34
Q

What is the @Controller in Spring MVC?

A

This registers the class as a controller and must be used in conjunction with context: component-scan tag in the XML to automatically discover and register the controllers as Spring beans.

The class defines a Spring MVC controller.

35
Q

What is @RequestMapping in Spring MVC?

A

This identifies the method as a request handler type for a given path.

36
Q

How do you turn ON annotation-based autowiring in Spring?

A

To turn ON autowiring use the context:annotation-config tag in the spring xml. This will enable
@Autowired,
@Inject
@Resource

37
Q

How does the @Autowired annotation work?

A

It is used to automatically wire values into properties, methods, and constructors. It works similar to xml-based autowiring but can be selectively used. Bean properties can be annotated directly allowing removal of the setters in the class and the property tags in the spring xml. If no bean is found to wire to the annotated property a NoSuchBeanDefinitionException is thrown by default.

38
Q

What annotations are used to register a class as a spring bean?

A
By default, context:component-scan tag in the spring.xml tells the container to look for classes that are annotated with one of the following: 
@Component 
@Controller  
@Repository 
@Service
39
Q

What ORMs does Spring support?

A

Spring supports the following ORMs :
● Hibernate
● JPA (Java Persistence API)

40
Q

How do you integrate Spring and Hibernate using Contextual Sessions?

A
  1. XML approach:

2. Annotation approach:

41
Q

What is the XML approach when you integrate Spring and Hibernate using Contextual Sessions?

A

1) XML approach:
● Create the DaoImpl class with a setter for the Session Factory.
● Wire up the following beans: DataSource, SessionFactory, HibernateTransactionManager, and DaoImpl class.
● Bind each CRUD method to a transaction in the appContext.xml. Define the AOP configuration using the aop:config tags and the transaction advice using the tx:advice tags and the transaction properties (propagation, isolation, and rollback) for each method with the tx:method tags.

42
Q

What are Bean scopes in Spring Framework ?

A

The Spring Framework supports exactly five scopes (of which three are available only if you are using a web-aware ApplicationContext). The scopes supported are listed below:

  1. Singleton
  2. Prototype
  3. Request
  4. Session
  5. Global Session
43
Q

What is the Singleton Bean scope?

A

Scopes a single bean definition to a single object instance per Spring IoC container.

44
Q

What is the Prototype Bean scope?

A

Scopes a single bean definition to any number of object instances.

45
Q

What is the Request Bean scope?

A

Scopes a single bean definition to the lifecycle of a single HTTP request; that is each and every HTTP request will have its own instance of a bean created off the back of a single bean definition. Only valid in the context of a web-aware Spring ApplicationContext.

46
Q

What is the Session Bean scope?

A

Scopes a single bean definition to the lifecycle of a HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.

47
Q

What is the Global Session scope?

A

Scopes a single bean definition to the lifecycle of a global HTTP Session. Typically only valid when used in a portlet context. Only valid in the context of a web-aware Spring ApplicationContext.

48
Q

What is AOP?

A

Aspect-oriented programming, or AOP, is a programming technique that allows programmers to modularize crosscutting concerns, or behavior that cuts across the typical divisions of responsibility, such as logging, security, exception handling and transaction management. The core construct of AOP is the aspect, which encapsulates behaviors affecting multiple classes into reusable modules.

49
Q

How is AOP used in Spring?

A

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.

50
Q

What do you mean by Aspect?

A

A modularization of a concern that cuts across multiple objects. Transaction management is a good example of a crosscutting concern in J2EE applications. In Spring AOP, aspects are implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation (@AspectJ style).

51
Q

What do you mean by join point?

A

A point during the execution of a program, such as the execution of a method or the handling of an exception. In Spring AOP, a join point always represents a method execution.

52
Q

What do you mean by Advice?

A

Action taken by an aspect at a particular join point. Different types of advice include “around,” “before” and “after” advice. Many AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors “around” the join point.

53
Q

What are the types of Advice?

A

Types of Advice:

  1. Introduction
  2. Before Advice
  3. After returning Advice
  4. After throwing Advice
  5. After (finally) Advice
  6. Around Advice
54
Q

What is the Introduction Advice?

A

● Introduction : Introduction advice cannot be used with any pointcut, as it applies only at class, rather than method, level. You can only use introduction advice with the IntroductionAdvisor.

55
Q

What is the Before Advice?

A

● Before advice: Advice that executes before a join point, but which does not have the ability to prevent execution flow proceeding to the join point (unless it throws an exception).

56
Q

What is the After returning Advice?

A

● After returning advice: Advice to be executed after a join point completes normally: for example, if a method returns without throwing an exception.

57
Q

What is the After throwing Advice?

A

● After throwing advice: Advice to be executed if a method exits by throwing an exception.

58
Q

What is the After (finally) Advice?

A

● After (finally) advice: Advice to be executed regardless of the means by which a join point exits (normal or exceptional return).

59
Q

What is the Around Advice?

A

● Around advice: Advice that surrounds a join point such as a method invocation. This is the most powerful kind of advice. Around advice can perform custom behavior before and after the method invocation. It is also responsible for choosing whether to proceed to the join point or to shortcut the advised method execution by returning its own return value or throwing an exception

60
Q

What are the types of the transaction management Spring supports?

A

Spring Framework supports:
● Programmatic transaction management.
● Declarative transaction management.

61
Q

What is Spring Framework Programmatic transaction management?

A

This means that you have manage the transaction with the help of programming. That gives you extreme flexibility, but it is difficult to maintain. Programmatic transaction management is usually a good idea only if you have a small number of transactional operations.

62
Q

What is the @Configuration annotation in Spring?

A

Indicates that a class declares one or more @Bean methods and may be processed by the Spring container to generate bean definitions and service requests for those beans at runtime.

63
Q

What is the @Repository annotation in Spring?

A

The class defines a data repository such as a DAO implementation class.

Indicates that an annotated class is a “Repository” as “a mechanism for encapsulating storage, retrieval, and search behavior which emulates a collection of objects”.

64
Q

What is the @Controller annotation in Spring?

A

Base Controller interface, representing a component that receives HttpServletRequest and HttpServletResponse instances just like a HttpServlet but is able to participate in an MVC workflow. Controllers are comparable to the notion of a Struts Action.

Any implementation of the Controller interface should be a reusable, thread-safe class, capable of handling multiple HTTP requests throughout the lifecycle of an application. To be able to configure a Controller easily, Controller implementations are encouraged to be (and usually are) JavaBeans.

65
Q

What is the @Service annotation in Spring?

A

The class defines a service.

Indicates that an annotated class is a "Service", originally defined by Domain-Driven Design "an operation offered as an interface that stands alone in the model, with no encapsulated state."
May also indicate that a class is a "Business Service Facade" (in the Core J2EE patterns sense), or something similar. This annotation is a general-purpose stereotype and individual teams may narrow their semantics and use as appropriate.

This annotation serves as a specialization of @Component, allowing for implementation classes to be autodetected through classpath scanning.

66
Q

What is the @Component annotation in Spring?

A

Indicates that an annotated class is a “component”. Such classes are considered as candidates for auto-detection when using annotation-based configuration and classpath scanning.

The class defines a Spring component.

67
Q

What is Spring DispatcherServlet?

A

The job of the DispatcherServlet is to take an incoming URI and find the right combination of handlers (generally methods on Controller classes) and views (generally JSPs) that combine to form the page or resource that’s supposed to be found at that location.

68
Q

What is the @RequestBody annotation in Spring?

A

Annotation indicating a method parameter should be bound to the body of the web request. The body of the request is passed through an HttpMessageConverter to resolve the method argument depending on the content type of the request. Optionally, automatic validation can be applied by annotating the argument with @Valid

69
Q

What is the @RequestMapping annotation in Spring?

A

Annotation for mapping web requests onto specific handler classes and/or handler methods. Provides a consistent style between Servlet and Portlet environments, with the semantics adapting to the concrete environment.

70
Q

What is the @ResponseBody annotation in Spring?

A

Annotation that indicates a method return value should be bound to the web response body. Supported for annotated handler methods in Servlet environments.
As of version 4.0 this annotation can also be added on the type level in which case it is inherited and does not need to be added on the method level.

71
Q

What is AOP Cross Cutting concerns?

A

A Concern is a term that refers to a part of the system divided on the basis of the functionality.

Concerns are two types:

The concerns representing single and specific functionality for primary requirements are known as core concerns.
OR
Primary functionality of the system is known as core concerns.
For example: Business logic
The concerns representing functionalities for secondary requirements are referred to as crosscutting concerns or system-wide concerns.
OR
The crosscutting concern is a concern which is applicable throughout the application and it affects the entire application.
For example: logging, security and data transfer are the concerns which are needed in almost every module of an application, hence they are cross-cutting concerns.

72
Q

What is AOP JoinPoint

A

The joinpoint is any potential point any aspect can jump in and do its job. Can potentially happen.

73
Q

What is AOP PointCut

A

The point in which an aspect intercepts and does it works. Can actually happen.

@Pointcut(“execution(public void somemethod())”)

74
Q

What is AOP Advice

A

Combination of what and when to work for aspects. We can tell the aspect to do its job before or after a method, after it returns a value, throws an exception.

75
Q

AOP Contextual Session

A

A given session is in effect throughout the scope of a given context.

76
Q

What is IoC Container?

A

IoC is also known as dependency injection (DI). It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method. The container then injects those dependencies when it creates the bean. This process is fundamentally the inverse, hence the name Inversion of Control (IoC), of the bean itself controlling the instantiation or location of its dependencies by using direct construction of classes, or a mechanism such as the Service Locator pattern.

https://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html