spring Flashcards

1
Q

IoC in general

A

IoC - Inversion of Control

IoC inverts the flow of control as compared to traditional control flow. In IoC, custom-written portions of a computer program receive the flow of control from a generic framework

we call library call
framework calls our code

console app vs window or web app (browser or windows calls our code)

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

DI as a form of IoC in Spring in general?

A

DI (Dependency injection) implementation in spring which moves object dependency management from object itself to container. The container is responsible for instantiating, managing lifecycle, and injection dependencies directly to beans that require it.

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

What are the advantages of using DI in spring?

A
  1. Reduced glue code.
    One of the biggest plus points of DI is its ability to dramatically reduce the amount of code you have to write to glue the components of your application together. Often this code is trivial, so creating a dependency involves simply creating a new instance of an object. However, the glue code can get quite complex when you need to look up dependencies in a JNDI repository or when the dependencies cannot be invoked directly, as is the case with remote resources.
  2. Simplified application configuration.
    By adopting DI, you can greatly simplify the process of configuring an application. You can use a variety of options to configure those classes that were injectable to other classes. You can use the same technique to express the dependency requirements to the “injector” for injecting the appropriate bean instance or property.
  3. Improved testability
    When you design your classes for DI, you make it possible to replace dependencies easily. This is especially handy when you are testing your application.
  4. Fostering good application design.
    Designing for DI means, in general, designing against interfaces. A typical injection-oriented
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the BeanFactory interface?

A

The root interface for accessing a Spring bean container.

This interface is implemented by objects that hold a number of bean definitions, each uniquely identified by a String name. Depending on the bean definition, the factory will return either an independent instance of a contained object (the Prototype design pattern), or a single shared instance (a superior alternative to the Singleton design pattern, in which the instance is a singleton in the scope of the factory). Which type of instance will be returned depends on the bean factory configuration: the API is the same.

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

What is ApplicationContext and how it differs from BeanFactory?

A

Application Context extends BeanFactory interface providing features

  1. Easier integration with Spring’s AOP features
  2. Message resource handling (for use in internationalization)
  3. Event publication
  4. Application-layer specific contexts such as the WebApplicationContext for use in web applications.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a Spring bean?

A

In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.

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

What ApplicationContext implementation do you know?

A
  1. ClassPathXmlApplicationContext
  2. FileSystemXmlApplicationContext
  3. AnnotationConfigApplicationContext
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is Configuration Metadata?

A

The Spring IoC container consumes a form of configuration metadata. This configuration metadata represents how you, as an application developer, tell the Spring container to instantiate, configure, and assemble the objects in your application.

Meta data contains bean definitions that can be loaded into Spring IoC container.

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

How we can split bean definitions over multiple files in xml?

A

with the help of import tag

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

What are BeanDefinitionReaders?

A

Configuration metadata can be loaded separately and loaded to ApplicationContext separately.

The most flexible variant is GenericApplicationContext in combination with reader delegates — for example, with XmlBeanDefinitionReader for XML files, as the following example shows:

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

How pull beans from ApplicationContext?

A

With the help of getBean() method

T getBean(String name, Class requiredType)

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

How bean information is stored internally?

A

Within the container itself, these bean definitions are represented as BeanDefinition objects, which contain (among other information) the following metadata

  1. A package-qualified class name: typically, the actual implementation class of the bean being defined.
  2. Bean behavioral configuration elements, which state how the bean should behave in the container (scope, lifecycle callbacks, and so forth).
  3. References to other beans that are needed for the bean to do its work. These references are also called collaborators or dependencies.
  4. Other configuration settings to set in the newly created object — for example, the size limit of the pool or the number of connections to use in a bean that manages a connection pool.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How we can add manually beans to IoC?

A

BeanFactory must implement BeanDefinitionRegistry interface in order to be able

registerBeanDefinition()

Singleton doesn’t require BeanDefinition because they accept object

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

How many names can have bean?

A

Every bean has one or more identifiers. These identifiers must be unique within the container that hosts the bean. A bean usually has only one identifier. However, if it requires more than one, the extra ones can be considered aliases.

PS. In XML name is supplied with id attribute, additional names can be added to name attribute separated by “,” , “;” or space.

In JavaConfig it is not possible only via @Bean annotation

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

What if the id is not set?

A

If name/id not set it will be generated:

com.apress.prospring5.ch2.gmsalex.BeanFour#0

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

Can we get beans by class if we have defined two beans?

A
If two beans with the same class can get by bean class will have an exception.
NoUniqueBeanDefinitionException

This can be resolved using primary.