IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> spring -> 正文阅读

[Java知识库]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>
      

      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>
      
    • 通过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>

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

  • 无参构造方法
     <bean id="stu"  scope="prototype"
              class="com.etoak.student.entity.Student"></bean>
        
     <bean id="sch" class="com.etoak.student.entity.School"></bean>
    
  • 带参数构造方法
     <!--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>
    
  • 静态工厂
    <!-- 静态工厂:向IOC容器中注册bean对象,标示是stu4,
        类型是:StudentFactory 中getStu静态方法返回值-->
        <bean id="stu4"
              class="com.etoak.student.entity.StudentFactory"
              factory-method="getStu"></bean>
    
  • 实例工厂
     <!--非静态工厂 实例工厂方法-->
        <bean id="f" class="com.etoak.student.entity.StudentFactory"></bean>
        <bean id="stu5" factory-method="getStu1" factory-bean="f"></bean>
    
  • 实现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>
    
  • 注解

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>
    
  • 构造方法
     <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>
    
  • 注解

5. BeanFactory和FactoryBean的区别?

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

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

  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-07-03 10:36:15  更:2022-07-03 10:39:43 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/18 4:29:33-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码