前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring IOC的理解

Spring IOC的理解

原创
作者头像
CoffeeLand
修改2020-03-09 16:45:31
4110
修改2020-03-09 16:45:31
举报
文章被收录于专栏:CoffeeLand

Table of Content

  • What is the IoC/DI?
  • How to understand IoC/DI?
  • How spring did the bean creation?
  • how Spring use DI to find the Beans?

What is the IoC/DI?

Sping IoC is Inversion of Control. 控制反转.

Spring DI 是 Dependency Injection 依赖注入.

它是user把创建对象的控制权交给了spring container.

这样做的好处就是可以Decoupling解耦.

控制反转是Spring 的一种设计思想, 使用的途径就是依赖注入

How to understand IoC/DI?

代码语言:javascript
复制
public class Car
{
    private Wheel wheel = new Wheel();
}

public class Wheel
{
    private float size;
}          

publicclassCar{private Wheel wheel;publicCar(Wheel wheel){this.wheel = wheel;}}publicclassWheel{private float size;}

使用IoC/DI来创建对象

代码语言:javascript
复制
public class Car
{
    private Wheel wheel;
    
    // 这样做的好处就是能够解耦, 并且方便开发者能够用mock测试
    public Car(Wheel wheel)
    {
        this.wheel = wheel;
    }
}

public class Wheel
{
    private float size;
}   

Spring IoC container

spring IoC 容器创建对象, 组装,配置, 管理bean的生命周期从创建到销毁.

spring容器使用DI来管理application的组件

How spring did the bean creation?

  • How to create a bean
  • Bean's lifecycle details
  • Bean's dependencies

How to create a bean

1.Create a metadata object

代码语言:javascript
复制
package com.tutorialspoint;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }
   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

2. Create Bean Configuration File

代码语言:javascript
复制
<?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 = "com.tutorialspoint.HelloWorld">
      <property name = "message" value = "Hello World!"/>
   </bean>

</beans>

3. Create bean

代码语言:javascript
复制
package com.tutorialspoint;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
      HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
      obj.getMessage();
   }
}

4. Run program

代码语言:javascript
复制
Your Message : Hello World!

ClassPathXmlApplicationContext().This API loads beans configuration file and eventually based on the provided API, it takes care of creating and initializing all the objects

ClassPathXmlApplicationContext在程序运行时会加载bean的xml配置文件,来创建bean和初始化

Bean's lifecycle details

Spring bean factory is responsible for managing the life cycle of beans created through spring container

Spring framework provides following 4 ways for controlling life cycle events of a bean:

  1. InitializingBean and DisposableBean callback interfaces
  2. *Aware interfaces for specific behavior
  3. Custom init() and destroy() methods in bean configuration file
  4. @PostConstruct and @PreDestroy annotations

how Spring use DI to find the Beans?

使用反射

代码语言:javascript
复制

public static Object newInstance(String className) {
		Class<?> cls = null;
		Object obj = null;
		try {
			cls = Class.forName(className);
			obj = cls.newInstance();
		} catch (ClassNotFoundException e) {
			throw new RuntimeException(e);
		} catch (InstantiationException e) {
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		}
		return obj;
}

Refers

https://www.tutorialspoint.com/spring/spring_bean_life_cycle.htm

https://howtodoinjava.com/spring-core/spring-bean-life-cycle/

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Table of Content
  • What is the IoC/DI?
  • How to understand IoC/DI?
  • Spring IoC container
  • How spring did the bean creation?
  • how Spring use DI to find the Beans?
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档