• spring


    Spring IOC

    1.Spring提供的IOC容器的使用?

    • Spring给我们提供了核心IOC容器,提供了依赖注入(DI)的核心功能。所以使用Spring首先需要研究核心容器。

    • Spring的核心容器使用步骤

      • 导入依赖
          <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-context</artifactId>
             <version>5.3.11</version>
          </dependency>
        
        • 1
        • 2
        • 3
        • 4
        • 5

        context = aop + beans + context + core + expression + jcl

      • 配置配置文件+交给Spring管理的类
        <?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.xsd">
             <!-- 以上 规定.xml文件格式规范   --> 
        
            <!-- 向Spring的IOC容器中注册一个bean对象 ,对象类型是Student 标示[名字]是:stu-->
            <bean id="stu" class="com.etoak.student.entity.Student"></bean>
            <bean id="sch" class="com.etoak.student.entity.School"></bean>
        </beans>
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
      • 通过Spring提供的核心容器获得对象。
        • BeanFactory是Spring IOC容器的核心接口。其中所有的IOC容器可以认为都是实现了该接口的具体类。(该接口中提供了最核心的方法getBean

        • XmlBeanFactory:从XML文件获得IOC容器的bean工厂。也是BeanFactory接口的一个实现类。已过时 不推荐

        • ApplicationContext接口 来自于spring-context包,也是BeanFactory接口的子接口,与WEB容器配置更紧密,一般WEB开发使用ApplicationContext更多。

        • ClassPathXmlApplicationContext: 从类路径中获得资源的ApplicationContext(IOC容器).

        • AnnotationConfigApplicationContext:从注解中获得配置信息的ApplicationContext(IOC容器)

        • BeanFactory采用延迟加载,ApplicationContext采用的立即加载方式。延迟加载:当使用到对象才会创建,立即加载:不管是否使用到对象,只要创建IOC容器(ApplicationContext)对象就会创建所有的容器管理的对象
        • Spring管理的bean对象【就是注册到IOC容器中的】默认是单例的,可以通过Scope属性进行修改,Scope默认值是:singleton可以改成prototype
      • 工程名字预告:

        • spring_ioc_demo【小案例】

          controller----service----dao-----db

        • spring_ioc_student_xml【基于XML的配置】

        • spring_ioc_student_xml_annotation【基于xml+注解的混合配置】

        • spring_ioc_student_annotation【基于注解的配置】

    2.关于Spring 配置文件的头信息?

    <beans 
           xmlns:xmlnamespace:xml的命名空间
           命名空间:相当于JAVA中的package,避免命名的冲突
           xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           //schemaLocation:schema:XML的约束文件的位置
           xsi:schemaLocation=
           "http://www.springframework.org/schema/beans 
           http://www.springframework.org/schema/beans/spring-beans.xsd"> schame文件后缀名 xsd
      
      XML文档的约束文件有两种:
      	DTD文档:  语法简单 老  后缀:xx.dtd
      schema文档: 功能强大 新  后缀:xx.xsd
        <!-- 向Spring的IOC容器中注册一个bean对象 ,对象类型是Student 标示[名字]是:stu-->
        <bean id="stu" class="com.etoak.student.entity.Student"></bean>
        <bean id="sch" class="com.etoak.student.entity.School"></bean>
    
    </beans>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    3.SpringIOC容器构造对象的方式?

    • 无参构造方法
       <bean id="stu"  scope="prototype"
                class="com.etoak.student.entity.Student"></bean>
          
       <bean id="sch" class="com.etoak.student.entity.School"></bean>
      
      • 1
      • 2
      • 3
      • 4
    • 带参数构造方法
       <!--2.构造对象的方式:带参数构造方法-->
          <!--形参的名字-->
       <bean id="stu1" class="com.etoak.student.entity.Student">
           <constructor-arg name="username" value="etoak" />
           <constructor-arg name="pwd" value="abc" />
       </bean>
          <!--形参的类型-->
      <bean id="stu2" class="com.etoak.student.entity.Student">
       <constructor-arg type="java.lang.String" value="etoak" />
       <constructor-arg type="java.lang.String" value="abc" />
      </bean>
          <!--索引 下标-->
      <bean id="stu3" class="com.etoak.student.entity.Student">
      		<constructor-arg index="0" value="etoak" />
          <constructor-arg index="1" value="abc" />
      </bean>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
    • 静态工厂
      <!-- 静态工厂:向IOC容器中注册bean对象,标示是stu4,
          类型是:StudentFactory 中getStu静态方法返回值-->
          <bean id="stu4"
                class="com.etoak.student.entity.StudentFactory"
                factory-method="getStu"></bean>
      
      • 1
      • 2
      • 3
      • 4
      • 5
    • 实例工厂
       <!--非静态工厂 实例工厂方法-->
          <bean id="f" class="com.etoak.student.entity.StudentFactory"></bean>
          <bean id="stu5" factory-method="getStu1" factory-bean="f"></bean>
      
      • 1
      • 2
      • 3
    • 实现FactoryBean接口的方式

      java

      public class StudentFactoryBean  implements FactoryBean<Student> {
          public StudentFactoryBean(){
              System.out.println("1111111111");
          }
          public Student getObject() throws Exception {
              System.out.println("非常复杂的构造对象的过程");
              return new Student();
          }
          public Class<?> getObjectType() {
              return Student.class;
          }
      }
      
      <!--向IOC容器中注册一个bean对象,标示、名字叫stu6
          对象是:(因为实现FactoryBean接口)StudentFactoryBean类中的getObject方法返回值-->
          <bean id="stu6" class="com.etoak.student.entity.StudentFactoryBean"></bean>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
    • 注解

    4.Spring IOC容器属性赋值的方式?

    • setter (前提! 属性有setter方法)
       <bean id="stu"  scope="prototype"
                class="com.etoak.student.entity.Student">
              <property name="username" value="etoak"></property>
              <property name="pwd" value="aaaa"></property>
              <property name="emails">
                  <list>
                      <value>etoak@qq.com</value>
                      <value>et@qq.com</value>
                  </list>
              </property>
              <property name="score">
                  <map>
                      <entry key="corejava" value="100"></entry>
                      <entry key="db" value="120"></entry>
                      <entry key="spring" value="150"></entry>
                  </map>
              </property>
              <property name="strs">
                  <array>
                      <value>abc</value>
                      <value>dfa</value>
                  </array>
              </property>
              <property name="sch" ref="sch"></property>
              <property name="properties">
                  <props>
                      <prop key="key">value</prop>
                      <prop key="key1">value1</prop>
                  </props>
              </property>
      </bean>
      
      • 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
    • 构造方法
       <bean id="stu1" class="com.etoak.student.entity.Student">
              <constructor-arg name="a" value="etoak"></constructor-arg>
              <constructor-arg name="pwd" value="abc"></constructor-arg>
          </bean>
          <!--形参的类型-->
          <bean id="stu2" class="com.etoak.student.entity.Student">
              <constructor-arg type="java.lang.String" value="etoak"></constructor-arg>
              <constructor-arg type="java.lang.String" value="abc"></constructor-arg>
          </bean>
          <!--索引 下标-->
          <bean id="stu3" class="com.etoak.student.entity.Student">
              <constructor-arg index="0" value="etoak"></constructor-arg>
              <constructor-arg index="1" value="abc"></constructor-arg>
          </bean>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
    • 注解

    5. BeanFactory和FactoryBean的区别?

    • BeanFactory是Spring提供的IOC容器的核心接口。 getBean是其核心方法,用来创建、管理、获得bean对象。

    • FactoryBean是IOC容器的构造对象的方式之一。 当使用该接口时,我们写一个类实现该接口并且提供getObject方法,就可以像IOC容器中注册bean对象。一般XXFactoryBean就是用来提供 XX对象的,如:StudentFactoryBean就是用来提供Student对象的。

  • 相关阅读:
    自动化测试测试框架封装改造
    java计算机毕业设计基于ssm的基于web的考研助手网站
    JAVA虚拟机--JVM
    实战PyQt5: 150-QChart图表之如何使用图例标记
    GESP:2024-6月等级5-T1-黑白格
    Netty selector的运行
    速来查分!2022监理工程师成绩查询入口开启
    75.颜色分类
    RocketMQ—(总结)一篇就搞懂 RocketMQ
    智慧大棚——用科技让大棚“开口说话”
  • 原文地址:https://blog.csdn.net/qq_45041558/article/details/125529292