Spring IoC , Inversion of Control trong Spring

Nguồn: https://stackjava.com/spring/spring-ioc-container.html

1. IoC là gì?

IoC(Inversion of Control): Đảo ngược điều khiển, nó giúp làm thay đổi luồng điều khiển của chương trình một cách linh hoạt.

Thường dùng với Denpendency Injection.

Các bạn có thể xem lại bài về Dependency Injection để hiểu rõ hơn: https://stackjava.com/design-pattern/dependency-injection-di-la-gi.html

2. Spring IoC

IoC Container là thành phần thực hiện IoC.

Trong Spring, Spring Container (IoC Container) sẽ tạo các đối tượng, lắp rắp chúng lại với nhau, cấu hình các đối tượng và quản lý vòng đời của chúng từ lúc tạo ra cho đến lúc bị hủy.

Spring container sử dụng DI để quản lý các thành phần, đối tượng để tạo nên 1 ứng dụng. Các thành phần, đối tượng này gọi là Spring Bean (mình sẽ nói về Spring Bean trong các bài sau)

Để tạo đối tượng, cấu hình, lắp rắp chúng, Spring Container sẽ đọc thông tin từ các file xml và thực thi chúng.

IoC Container trong Spring có 2 kiểu là:

BeanFactory
ApplicationContext

Sự khác nhau giữa BeanFactory và ApplicationContext:

BeanFactory và ApplicationContext đều là các interface thực hiện IoC Container. ApplicationContext được xây dựng BeanFactory nhưng nó có thêm một số chức năng mở rộng như tích hợp với Spring AOP, xử lý message, context cho web application.

3. Ví dụ với BeanFactory và ApplicationContext.

3.1 BeanFactory

Để sử dụng Spring Bean ta cần khai báo thư viện spring-bean sau:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>4.3.13.RELEASE</version>
</dependency>

Class HelloWorld.java

public class HelloWorld {
  private String message;

  public void setMessage(String message) {
    this.message = message;
  }

  public void getMessage() {
    System.out.println("Print : " + message);
  }
}

Để tạo đối tượng HelloWorld thông qua IoC container ta sẽ cấu hình nó trong file beans.xml

<?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
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id = "helloWorld" class = "stackjava.com.springioc.beanfactory.HelloWorld" >
      <property name = "message" value = "Hello World!"/>
   </bean>

</beans>

Bây giờ ta sẽ tạo một BeanFactory để đọc các thông tin cấu hình và tạo ra đối tượng HelloWorl.

BeanFactory chỉ là 1 interface, nên ở đây mình dùng DefaultListableBeanFactory, một implement của BeanFactory. Ở các version cũ thì bạn sẽ thấy hay sử dụng XmlBeanFactory nhưng nó bị đánh dấu @Deprecated ở các version mới.

// tạo factory		
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();

// đọc thông tin file cấu hình và gán vào factory
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory);
reader.loadBeanDefinitions(new ClassPathResource("beans.xml"));

//tạo đối tượng từ factory
HelloWorld obj = (HelloWorld) factory.getBean("helloWorld");
obj.getMessage();

Kết quả:

Print : Hello World!

3.2 Application Context

Để sử dụng Spring Bean ta cần khai báo thư viện spring-context sau:

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>4.3.13.RELEASE</version>
</dependency>

Mình sẽ tạo đối tượng phức tạp hơn HelloWorl.java một chút.

Ví dụ class DataResource.java chứa thông tin kết nối tới database.

public class DataResource {
  private String driverClassName;
  private String url;
  private String username;
  private String password;

  public String getDriverClassName() {
    return driverClassName;
  }

  public void setDriverClassName(String driverClassName) {
    this.driverClassName = driverClassName;
  }

  public String getUrl() {
    return url;
  }

  public void setUrl(String url) {
    this.url = url;
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public String getPassword() {
    return password;
  }

  public void setPassword(String password) {
    this.password = password;
  }

  public void printConnection() {
    System.out.println("url: " + this.url + "\n" + "username/password: " + this.username + "/" + this.password);
  }
}

Để tạo đối tượng HelloWorld thông qua IoC container ta sẽ cấu hình nó trong file applicationContext.xml (lưu ý là bạn đặt tên file là gì cũng được: bean.xml, applicationContext.xml, dataresource.xml… nhưng cần phải nhớ file cấu hình cho cái gì)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <bean id="dataResource" class="stackjava.com.springioc.applicationcontext.DataResource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost/database_name" />
    <property name="username" value="root" />
    <property name="password" value="admin1234" />
  </bean>

</beans>

Tạo một đối tượng ApplicationContext để lấy thông tin từ file cấu hình và tạo đối tượng DataResource


public static void main(String[] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  DataResource obj = (DataResource) context.getBean("dataResource");
  obj.printConnection();
}

Kết quả:

url: jdbc:mysql://localhost/database_name
username/password: root/admin1234

Done!

Bây giờ bạn muốn thay đổi messge trong đối tượng HelloWorld, hay database của bạn thay đổi username/password hay bạn đổi kết nối sang database khác bạn chỉ cần đổi lại thông tin trong file config .xml là đã thay đổi được luồng chạy của chương trình, đó chính là IoC.

Download code ví dụ tại đây