C1 - Core Technologies

Download as xlsx, pdf, or txt
Download as xlsx, pdf, or txt
You are on page 1of 6

1.

The IoC Container


0. Dependecies
Spring context

1. Introduction to the Spring IoC Container and Beans


This chapter covers the Spring Framework implementation of the Inversion of Control (IoC) principle.

Dependency injection (DI) is a specialized form of IoC, whereby objects define their dependencies (that is, the ot
or properties that are set on the object instance after it is constructed or returned from a factory method.

The IoC container then injects those dependencies when it creates the bean. This process is fundamentally the i
using direct construction of classes or a mechanism such as the Service Locator pattern.

The org.springframework.beans and org.springframework.context packages are the basis for Spring Framework’s
- The BeanFactory interface provides an advanced configuration mechanism capable of managing any type
- ApplicationContext is a sub-interface of BeanFactory. It adds:
- 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.

2. Container Overview
Interface đại diện cho Spring IoC container là org.springframework.context.ApplicationContext, nó đảm nhận việ

configuration metadata có thể biểu diễn dưới các dạng sau:


- annotated component classes
- configuration classes with factory methods
- external XML files
- Groovy scripts

Ta có thể tạo instance của interface ApplicationContext thông qua các class sau:
- Đối với standalone application:
- AnnotationConfigApplicationContext
- ClassPathXmlApplicationContext
Configuration Metadata

This configuration metadata represents how you, as an application developer, tell the Spring container to instan

Có các loại config sau:


- XML-based Configuration Metadata (<beans/>)
- Annotation-based configuration (@Autowired)
- Java-based configuration (@Configuration, @Bean, @Import, @DependsOn)

XML-based Configuration

Rule:
Phải để file config trong folder [main/resources]

Template: XML-based configuration metadata configures these beans as <bean/> elements inside a top-level <b

<?xml version="1.0" encoding="UTF-8"?>


<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="..." class="...">


<!-- collaborators and configuration for this bean go here -->
</bean>

<bean id="..." class="...">


<!-- collaborators and configuration for this bean go here -->
</bean>

<!-- more bean definitions go here -->

</beans>

- The id attribute is a string that identifies the individual bean definition. The value of the id attribute can be use
- The class attribute defines the type of the bean and uses the fully qualified class name.

Nếu dùng XML-based Configuration thì ta sẽ khởi tạo container bằng class ClassPathXmlApplicationContext

ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");

Sau khi khởi tạo được container, ta có thể lấy được các object (bean) được khởi tạo từ container ra qua method
ol (IoC) principle.

ependencies (that is, the other objects they work with) only through constructor arguments, arguments to a factory method,
ed from a factory method.

ocess is fundamentally the inverse (hence the name, Inversion of Control) of the bean itself controlling the instantiation or location of its d

asis for Spring Framework’s IoC container.


able of managing any type of object.

use in web applications.

onContext, nó đảm nhận việc khởi tạo (initialize) object, tuy biến (configuration) và assemply
e Spring container to instantiate, configure, and assemble the components in your application.

ments inside a top-level <beans/> element.

the id attribute can be used to refer to collaborating objects.

tên file config XML


XmlApplicationContext

ml", "daos.xml");

từ container ra qua method get()


actory method,

antiation or location of its dependencies by

You might also like