• 依赖注入-7


    依赖注入

    1. 项目结构图

      [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Pij3tSkT-1667351873764)(C:\Users\13301\AppData\Roaming\Typora\typora-user-images\image-20221102091419425.png)]

    2. pom.xml

      
      <project xmlns="http://maven.apache.org/POM/4.0.0"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
          <parent>
              <artifactId>thinking-in-springartifactId>
              <groupId>org.xiaogegroupId>
              <version>1.0-SNAPSHOTversion>
              <relativePath>../pom.xmlrelativePath>  
          parent>
          <modelVersion>4.0.0modelVersion>
      
          <artifactId>ioc-container-overviewartifactId>
      
      
          <dependencies>
      
              <dependency>
                  <groupId>org.springframeworkgroupId>
                  <artifactId>spring-contextartifactId>
              dependency>
          dependencies>
      
      project>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
    3. dependency-lookup-context.xml

      
      <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="user" class="org.xiaoge.thinking.in.spring.ioc.overview.domain.User">
              <property name="id" value="1" />
              <property name="name" value="xiaoge"/>
          bean>
      
          
          <bean id="objectFactory" class="org.springframework.beans.factory.config.ObjectFactoryCreatingFactoryBean">
              <property name="targetBeanName" value="user" />
          bean>
      
          
          <bean id="superUser" class="org.xiaoge.thinking.in.spring.ioc.overview.domain.SuperUser" parent="user" primary="true">
              <property name="address" value="武汉" />
          bean>
      
      beans>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
    4. dependency-injection-context.xml

      
      <beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:util="http://www.springframework.org/schema/util"
             xsi:schemaLocation="
              http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
              http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
      
          
          <import resource="dependency-lookup-context.xml" />
      
          <bean id="userRepository" class="org.xiaoge.thinking.in.spring.ioc.overview.repository.UserRepository"
          autowire="byType"> 
      
      
      
      
      
      
      
      
          bean>
      
      
      beans>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
    5. Super注解

      package org.xiaoge.thinking.in.spring.ioc.overview.annotation;
      
      import java.lang.annotation.ElementType;
      import java.lang.annotation.Retention;
      import java.lang.annotation.RetentionPolicy;
      import java.lang.annotation.Target;
      
      /**
       * @Classname Super
       * @Date 2022/11/1 16:06
       * @Created by ZhangXiao
       * @Description TODO
       */
      @Target({ElementType.TYPE})
      @Retention(RetentionPolicy.RUNTIME)
      public @interface Super {
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
    6. User.class

      package org.xiaoge.thinking.in.spring.ioc.overview.domain;
      
      /**
       * @Classname User
       * @Date 2022/10/17 14:57
       * @Created by ZhangXiao
       * @Description TODO
       */
      public class User {
      
          private Long id;
      
          private String name;
      
          public Long getId() {
              return id;
          }
      
          public void setId(Long id) {
              this.id = id;
          }
      
          public String getName() {
              return name;
          }
      
          public void setName(String name) {
              this.name = name;
          }
      
          @Override
          public String toString() {
              return "User{" +
                      "id=" + id +
                      ", name='" + name + '\'' +
                      '}';
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
    7. SuperUser.class

      package org.xiaoge.thinking.in.spring.ioc.overview.domain;
      
      import org.xiaoge.thinking.in.spring.ioc.overview.annotation.Super;
      
      /**
       * @Classname SuperUser 超级用户
       * @Date 2022/11/1 16:06
       * @Created by ZhangXiao
       * @Description TODO
       */
      @Super
      public class SuperUser extends User{
      
          private String address;
      
          public String getAddress() {
              return address;
          }
      
          public void setAddress(String address) {
              this.address = address;
          }
      
          @Override
          public String toString() {
              return "SuperUser{" +
                      "address='" + address + '\'' +
                      "} " + super.toString();
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
    8. UserRepository.class

      package org.xiaoge.thinking.in.spring.ioc.overview.repository;
      
      import org.springframework.beans.factory.BeanFactory;
      import org.springframework.beans.factory.ObjectFactory;
      import org.springframework.context.ApplicationContext;
      import org.xiaoge.thinking.in.spring.ioc.overview.domain.User;
      
      import java.util.Collection;
      
      /**
       * @Classname UserRepository
       * @Date 2022/11/1 16:52
       * @Created by ZhangXiao
       * @Description TODO
       */
      public class UserRepository {
      
          private Collection<User> users; // 自定义  bean
      
          private BeanFactory beanFactory; // 内建 非 bean 对象 (依赖)
      
          private ObjectFactory<ApplicationContext> objectFactory;
      
          public Collection<User> getUsers() {
              return users;
          }
      
          public void setUsers(Collection<User> users) {
              this.users = users;
          }
      
          public BeanFactory getBeanFactory() {
              return beanFactory;
          }
      
          public void setBeanFactory(BeanFactory beanFactory) {
              this.beanFactory = beanFactory;
          }
      
          public ObjectFactory<ApplicationContext> getObjectFactory() {
              return objectFactory;
          }
      
          public void setObjectFactory(ObjectFactory<ApplicationContext> objectFactory) {
              this.objectFactory = objectFactory;
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
    9. 测试类

      package org.xiaoge.thinking.in.spring.ioc.overview.dependency.injection;
      
      import org.springframework.beans.factory.BeanFactory;
      import org.springframework.beans.factory.ListableBeanFactory;
      import org.springframework.beans.factory.ObjectFactory;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      import org.xiaoge.thinking.in.spring.ioc.overview.annotation.Super;
      import org.xiaoge.thinking.in.spring.ioc.overview.domain.User;
      import org.xiaoge.thinking.in.spring.ioc.overview.repository.UserRepository;
      
      import java.util.Map;
      
      /**
       * 注意: 依赖注入/依赖查找 它们的来源并不是一个
       * 依赖注入示例 看 UserRepository
       *  1. 名称集合注入: util:list
       *  2. 类型集合注入: autowire
       *  3. 注入内部bean: users
       *  4. 注入非内建bean: BeanFactory
       *  5. 延迟注入: ObjectFactory
       *
       * @Classname DependencyInjectionDemo
       * @Date 2022/10/17 14:56
       * @Created by ZhangXiao
       * @Description TODO
       */
      public class DependencyInjectionDemo {
      
          public static void main(String[] args) {
              // 配置xml配置文件
              // 启动spring应用上下文
              BeanFactory beanFactory = new ClassPathXmlApplicationContext("classpath:META-INF/dependency-injection-context.xml");
      
              UserRepository userRepository = beanFactory.getBean("userRepository", UserRepository.class);
      
              // 依赖注入, 注入内部bean
              System.out.println(userRepository.getUsers());
      
              // 依赖注入  注入非bean对象
              System.out.println(userRepository.getBeanFactory());
              System.out.println(userRepository.getBeanFactory() == beanFactory);
      
              // 依赖查找 (错误)
              // System.out.println(beanFactory.getBean(BeanFactory.class));
      
              // 延迟注入
              ObjectFactory<ApplicationContext> objectFactory = userRepository.getObjectFactory();
              System.out.println(objectFactory.getObject());
      
              System.out.println(objectFactory.getObject() == beanFactory);
      
          }
      
      
      }
      
      
      
      
      /*
      	运行结果
      		[User{id=1, name='xiaoge'}, SuperUser{address='武汉'} User{id=1, name='xiaoge'}]
      		
      		org.springframework.beans.factory.support.DefaultListableBeanFactory@22f71333: defining beans 			
      		[user,objectFactory,superUser,userRepository]; root of factory hierarchy
      		
      		false
      		
      		org.springframework.context.support.ClassPathXmlApplicationContext@7e6cbb7a, started on Wed Nov 02 09:17:05 CST 2022
              
              true
      
      */
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71
      • 72
      • 73
      • 74
  • 相关阅读:
    将数据库中的数据接入Echarts图表
    MySQL高性能数据_第三版(读书笔记)
    【案例分享】BenchmarkSQL 5.0 压测 openGauss 5.0.0
    使用axios 请求库结合iview组件做登录页面
    PHP循环获取Excel表头字母A-Z,当超过时输出AA,AB,AC,AD······
    让EXCEL VBA支持鼠标滚轮,vb6 IDE鼠标滚轮插件原理
    pytorch: hook机制,取网络层的输入输出和梯度
    基于MS16F3211芯片的触摸控制灯的状态变化和亮度控制(11.16)
    dubbo源码解析之服务发现
    Qt优秀开源项目之九:qTox
  • 原文地址:https://blog.csdn.net/zsx1314lovezyf/article/details/127645513