spring Flashcards
IoC in general
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)
DI as a form of IoC in Spring in general?
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.
What are the advantages of using DI in spring?
- 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. - 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. - 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. - Fostering good application design.
Designing for DI means, in general, designing against interfaces. A typical injection-oriented
What is the BeanFactory interface?
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.
What is ApplicationContext and how it differs from BeanFactory?
Application Context extends BeanFactory interface providing features
- Easier integration with Spring’s AOP features
- Message resource handling (for use in internationalization)
- Event publication
- Application-layer specific contexts such as the WebApplicationContext for use in web applications.
What is a Spring bean?
In Spring, the objects that form the backbone of your application and that are managed by the Spring IoC container are called beans.
What ApplicationContext implementation do you know?
- ClassPathXmlApplicationContext
- FileSystemXmlApplicationContext
- AnnotationConfigApplicationContext
What is Configuration Metadata?
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 we can split bean definitions over multiple files in xml?
with the help of import tag
What are BeanDefinitionReaders?
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 pull beans from ApplicationContext?
With the help of getBean() method
T getBean(String name, Class requiredType)
How bean information is stored internally?
Within the container itself, these bean definitions are represented as BeanDefinition objects, which contain (among other information) the following metadata
- A package-qualified class name: typically, the actual implementation class of the bean being defined.
- Bean behavioral configuration elements, which state how the bean should behave in the container (scope, lifecycle callbacks, and so forth).
- References to other beans that are needed for the bean to do its work. These references are also called collaborators or dependencies.
- 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 we can add manually beans to IoC?
BeanFactory must implement BeanDefinitionRegistry interface in order to be able
registerBeanDefinition()
Singleton doesn’t require BeanDefinition because they accept object
How many names can have bean?
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
What if the id is not set?
If name/id not set it will be generated:
com.apress.prospring5.ch2.gmsalex.BeanFour#0