Container, Dependency, and IOC Flashcards Preview

Core Spring > Container, Dependency, and IOC > Flashcards

Flashcards in Container, Dependency, and IOC Deck (5)
Loading flashcards...
1
Q

What is dependency injection and what are the advantages?

A

Dependency Injection is a process of linking the consumer and the provider components, where a consumer is a component dependent on other and using other components services, and provider - provides services to other component.

Advantages of using DI:

  • Cleaner code.
  • Reduced coupling - less/or no direct dependencies are used, therefore, the dependency can be changed more easily, and with less effect of the dependent (consumer) bean.
  • Increased code re-usability - code becomes more generic and suitable for more implementations.
  • Increased testability - components can be tested by themselves by using mock or stubs implementations of dependencies
2
Q

What is an interface and what are the advantages of making use of them in Java?

A
Interface is an abstract representation of a class that provides insight on it's functionality to other classes by exposing methods that the class will have implemented. 
Advantages: provides the required level of abstraction to reduce coupling, and facilitate dependency injection.
3
Q

What is meant by “application context” and how do you create one?

A
The application context is a class implementing the Spring interface org.springframework.context.ApplicationContext that needs to be instantiated with the configuration file given as an argument. The application context will manage all objects instantiated and initialized by the Spring IoC (beans). 
The ApplicationContext is the central interface within a Spring Application for providing configuration information to the application. It is read-only at run time , but can be reloaded if necessary and supported by the application.
4
Q

What is the concept of a “container” and what is it’s lifecycle?

A

The interface org.springframework.context.ApplicationContext represents the Spring IoC container and is responsible for instantiating, configuring, and assembling the aforementioned beans. The container gets its instructions on what objects to instantiate, configure, and assemble by reading configuration metadata. The configuration metadata is represented in XML, Java annotations, or Java code. It allows you to express the objects that compose your application and the rich interdependencies between such object.

5
Q

Scopes for Spring beans. What is the default?

A

Out of the box, the Spring Framework supports seven scopes, five of which are available only if you use a web-aware ApplicationContext.

singleton - (Default) Scopes a single bean definition to a single object instance per Spring IoC container.

  • prototype - Scopes a single bean definition to any number of object instances.
  • request - Scopes a single bean definition to the lifecycle of a single HTTP request; that is, each HTTP request has 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.
  • session - Scopes a single bean definition to the lifecycle of an HTTP Session. Only valid in the context of a web-aware Spring ApplicationContext.
  • globalSession - 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.
  • application - Scopes a single bean definition to the lifecycle of a ServletContext. Only valid in the context of a web-aware Spring ApplicationContext.
  • websocket - Scopes a single bean definition to the lifecycle of a WebSocket. Only valid in the context of a web-aware Spring ApplicationContext.