Spring学习——IoC 容器

绝地灬酷狼 2022-05-14 07:05 345阅读 0赞

Spring IoC(Inversion of Control,控制反转) GitHub demo源码

Spring 容器是 Spring 框架的核心。容器将创建对象、配置对象、操作对象、管理对象、连接对象,控制对象从创建到销毁的整个生命周期。

  1. Spring容器包括**ApplicationContext容器**和**BeanFactory容器**
  2. //ApplicationContext
  3. package com.spring.example.hello;
  4. import org.springframework.context.ApplicationContext;
  5. import org.springframework.context.support.ClassPathXmlApplicationContext;
  6. public class SpringMainContext {
  7. public static void main(String[] args){
  8. ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); //获取beans.xml配置文件
  9. HelloWorldBean bean = (HelloWorldBean) context.getBean("helloWorld");//通过id获取bean对象
  10. //bean.setMessage("Hi,Hello Spring");
  11. System.out.println(bean.getMessage());
  12. }
  13. }
  14. //BeanFactory
  15. package com.spring.example.hello;
  16. import org.springframework.beans.factory.BeanFactory;
  17. import org.springframework.beans.factory.xml.XmlBeanFactory;
  18. import org.springframework.context.support.ClassPathXmlApplicationContext;
  19. import org.springframework.core.io.ClassPathResource;
  20. public class SpringMainFactory {
  21. public static void main(String[] args){
  22. //BeanFactory factory = new XmlBeanFactory(new ClassPathResource("Beans.xml")); //XmlBeanFactory方法过期
  23. BeanFactory factory = new ClassPathXmlApplicationContext("Beans.xml");//获取beans.xml配置文件
  24. HelloWorldBean bean = (HelloWorldBean) factory.getBean("helloWorld");//通过id获取bean对象
  25. //bean.setMessage("Hi,Hello Spring");
  26. System.out.println(bean.getMessage());
  27. }
  28. }

beans.xml配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
  6. <!-- 声明一个bean对象,id是唯一识别,class是目标路径 -->
  7. <bean id="helloWorld" class="com.spring.example.hello.HelloWorldBean" init-method="init" destroy-method="destroy">
  8. <!-- 属性配置,name是bean对象中的属性,value是给该属性赋值 -->
  9. <property name="message" value="Hello World!"/>
  10. </bean>
  11. </beans>

bean对象

  1. package com.spring.example.hello;
  2. public class HelloWorldBean {
  3. private String message;
  4. public String getMessage() {
  5. return message;
  6. }
  7. public void setMessage(String message) {
  8. this.message = message;
  9. }
  10. public void init(){
  11. System.out.println("Bean-init.");
  12. }
  13. public void destroy(){
  14. System.out.println("Bean-destroy");
  15. }
  16. }

容器将创建对象,把它们连接在一起,配置它们,并管理他们的整个生命周期从创建到销毁。
Spring 容器使用依赖注入(DI)来管理组成一个应用程序的组件。这些对象被称为 Spring Beans

1、主程序启动入口
1>通过读取beans.xml文件,获取生成工厂 bean

  1. 2>获取beans.xml中注册的【目标对象】
  2. 3>由于beans.xml已经对【目标对象】的部分属性进行赋值操作,可以直接获取该赋值

2、beans.xml 文件
一个bean对应一个对象,id是唯一标志,class是类的包路径
property对应属性,name是属性名,value是属性值

  1. <bean id="" class="">
  2. <property name="" value=""/>
  3. </bean>

3、beans.xml配置
beans.xml

  1. <!-- 基础bean -->
  2. <bean id="..." class="...">
  3. <property name="..." value="..."/>
  4. </bean>
  5. <!-- 延迟初始化 -->
  6. <bean id="..." class="..." lazy-init="true">
  7. </bean>
  8. <!-- 初始化方法 -->
  9. <bean id="..." class="..." init-method="...">
  10. </bean>
  11. <!-- 销毁方法 -->
  12. <bean id="..." class="..." destroy-method="...">
  13. </bean>

4、生命周期
init-method -> method -> destroy-method

发表评论

表情:
评论列表 (有 0 条评论,345人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Spring IoC容器

    Spring IoC(Inversion of Control,控制反转)容器是Spring框架的核心功能之一,它是一个轻量级的容器,负责管理Java对象的生命周期和依赖关系。

    相关 Spring IoC容器

    我们将详细介绍 Spring 的 Ioc 容器。 IoC 是指在程序开发中,实例的创建不再由调用者管理,而是由 Spring 容器创建。Spring 容器会负责控制程序之

    相关 Spring IOC容器

    IOC(控制反转):不负责对象的创建,只负责使用,由外部容器创建 DI(依赖注入):创建对象并且组装对象之间的关系        ![watermark_type