资讯详情

Spring全面详解(学习总结)

  • Spring FrameWork
  • 一、 前言
  • 二、IOC(控制反转)
    • 2.1 对于IOC的理解
    • 2.2如何使用IOC
    • 2.3.解释配置文件
    • 2.4IOC容器创建bean的两种方式
    • 2.5从IOC容器中取bean
    • 2.6bean的属性如果包含特殊字符
  • 三、DI(依赖注入)
  • 四、Spring中的bean
  • 五、Spring中的继承
  • 六、Spring的依赖
  • 七、Spring读取外部资源
  • 八、Springp命名空间
  • 九、Spring工厂方法
    • 9.静态工厂法
    • 9.2实例工厂方法
  • 十、Spring IOC 自动装配 autowire
    • 10.1自动装配
    • 10.2 Spring IOC基于注释的开发
    • 10.在实际开发中使用3
  • 十一、Spring AOP
??个人主页: 皮卡丘不断前进 ??博客描述: ----- 刷题神器 文章是看楠哥视频写的笔记 博客内容Spring说到基本知识点,篇幅比较长,可以用来复习,也可以在学习相关知识点的时候看相应的内容。对于一些困难,IOC,AOP等等,我可以通过流程图、代码、代码和文本的组合

Spring FrameWork

一、 前言

  • Spring是当前Java行业标准的发展,第一框架
  • Spring FrameWork已从最初取代EJB最新版本是5.x
  • Spring架构体系图 在这里插入图片描述
  • Spring两大核心机制:
    • IOC:工厂模式
    • AOP:代理模式

二、IOC(控制反转)

2.1 对于IOC的理解

在传统的开发中,当需要调用对象时,需要调用者手动创建被调用者的例子,即被调用者new出来的 但是在Spring在框架中,创建对象的工作不再由调用者完成,而是交给IOC创建容器,然后推给调用器,整个过程完成反转,因此控制反转 就比如说假设买东西,以前我们需要自己去超市买东西,但是现在我们可以不用自己去超市,我们只要把购物袋放在家门口,IOC我们会自己买我们想要的东西,然后放在袋子里。我们可以打开袋子拿起来用。 IOC其特点是解耦合。 例如,A需要使用B,在传统发展中,我们应该直接创建B的例子,但在Spring中,IOC这个容器将创建B的例子,然后注入BA

2.2如何使用IOC

  • 创建maven工程,在pom.xml引入相关依赖
<?xml version="1.0" encoding="UTF-8"?> <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">     <modelVersion>4.0.0</modelVersion>      <groupId>org.example</groupId>     <artifactId>springioc</artifactId>     <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.18</version>
        </dependency>
    </dependencies>

</project>
  • 创建实体类Student
public class Student { 
        
    private long id;
    private String name;
    private int age;

    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;
    }

    public int getAge() { 
        
        return age;
    }

    public void setAge(int age) { 
        
        this.age = age;
    }

    @Override
    public String toString() { 
        
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

  • 在resources路径下创建applicationContext.xml配置文件
<?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">

    <bean id="stu" class="com.zyh.pojo.Student"></bean>
</beans>
  • 传统的开发方式:手动new Student
        Student stu =new Student();
        stu .setAge(25);
        stu.setId(1001);
        stu.setName("张三");
        System.out.println(stu);
  • IOC容器通过读取配置文件,加载配置bean标签来创建对象
  • 就像买菜一样,我们不需要自己亲自买,但是要写一张单子,告诉说要买什么,程序也是类似的,我们要告诉Spring框架要创建哪些对象,怎样创建对象
  • 调用API,从IOC获取对象
//读取配置文件
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student stu = applicationContext.getBean("stu", Student.class);
        System.out.println(stu);

2.3配置文件的解读

  • 通过配置bean标签来完成对象的管理
  • id:对象名
  • class:对象的模板类(所有交给IOC容器来管理的类必须要有无参构造函数,因为Spring底层是通过反射机制来创建对象,调用的是无参构造)
  • 对象的成员变量通过property标签完成赋值
    • name:成员变量名
    • value:成员变量值(基本数据类型,String可以直接赋值,如果是其他引用类型不可以通过value赋值)
    • ref:把IOC中的另一个bean赋给当前成员变量(DI)

2.4IOC容器创建bean的两种方式

  • 无参构造函数(需要提供对应的set方法)

  • 有参构造函数

  <bean id="stu1" class="com.zyh.pojo.Student">
        <constructor-arg name="id" value="1">  </constructor-arg>
        <constructor-arg name="name" value="李四"></constructor-arg>
    </bean>
  <bean id="stu1" class="com.zyh.pojo.Student">
        <constructor-arg index=0 value="1">  </constructor-arg>
        <constructor-arg index=1 value="李四"></constructor-arg>
    </bean>

2.5从IOC容器中取bean

  • 通过id取值
 Student stu = (Student)applicationContext.getBean("stu");
  • 通过类型取值
 Student stu = applicationContext.getBean(  Student.class);
  • 当IOC容器中存在两个以上Student Bean的时候就会抛出异常,因为此时没有唯一的bean

2.6bean的属性如果包含特殊字符

三、DI(依赖注入)

  • DI:指bean之间的依赖注入,设置对象之间的级联关系
  • Classes
public class Classes { 
        
    private Integer id;
    private String name;
    还有对应的get,set方法
}
  • Student
public class Student { 
        
    private long id;
    private String name;
    private int age;
    private Classes classes;

    public Student(){ 
        
        System.out.println("使用无参构造创建对象");
    }
    public Student(long id,String name){ 
        
        this.id = id;
        this.name = name;
    }  
    public Classes getClasses() { 
        
        return classes;
    }

    public void setClasses(Classes classes) { 
        
        this.classes = classes;
    }

    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;
    }

    public int getAge() { 
        
        return age;
    }

    public void setAge(int age) { 
        
        this.age = age;
    }

    @Override
    public String toString() { 
        
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", classes=" + classes +
                '}';
    }
}

applicationContext-di.xml

<?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">
    <bean id="classes" class="com.zyh.pojo.Classes">
        <property name="name" value="1班"></property>
        <property name="id" value="1"></property>
    </bean>

    <bean id="student" class="com.zyh.pojo.Student">
        <property name="id" value="1001"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="22"></property>
        <property name="classes" ref="classes"></property>
    </bean>


</beans>

bean之间的级联需要使用ref属性,而不能用value属性,否则会抛出类型转换异常

<?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">
    <bean id="classes" class="com.zyh.pojo.Classes">
        <property name="name" value="1班"></property>
        <property name="id" value="1"></property>
        <property name="studentList">
            <list>
                <ref bean="student"></ref>
                <ref bean="student2"></ref>
            </list>
        </property>
    </bean>

    <bean id="student" class="com.zyh.pojo.Student">
        <property name="id" value="100"></property>
        <property name="name" value="张三"></property>
        <property name="age" value="22"></property>
        <property name="classes" ref="classes"></property>
    </bean>
    <bean id="student2" class="com.zyh.pojo.Student">
        <property name="id" value="200"></property>
        <property name="age" value="18"></property>
        <property name="name" value="李四"></property>
        <property name="classes" ref="classes"></property>
    </bean>


</beans>

如果把学生装到班级里面,又把班级装到学生里面,就导致无限递归循环装配,最终栈溢出

四、Spring中的bean

bean是根据scope来生成的,表示bean的作用域,scope有4种类型

  • singleton,单例,表示通过Spring容器获取的对象是唯一的,是默认值

  • prototype,原型,表示通过Spring容器获取的对象是不同的

    • 配置文件
<bean id="user" class="com.zyh.pojo.User" scope="prototype">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
    </bean>

  • request,请求,表示在异常HTTP请求内有效
  • session,会话,表示在一个用户会话内有效 request和session一般用于web项目 singleton模式下,只要加载IOC容器,不管是否从IOC种取出bean,配置文件中的bean都会被创建,而且只会创建一个对象 prototype模式下,如果不从IOC中取出bean,则不创建对象,取一次bean,就会创建一个对象

五、Spring中的继承

Spring中的继承不同于Java中的继承 Java中的继承是针对于类的 Spring中的继承是针对于对象(bean)

<?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">

    <bean id="user1" class="com.zyh.pojo.User" >
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
    </bean>
    <bean id="user2" class="com.zyh.pojo.User" parent="user1"></bean>
</beans>

  • 通过设置bean标签的parent属性建立继承关系,同时子bean可以覆盖父bean的属性值
  • Spring的继承是针对对象的,所以子bean和父bean并不需要同一个数据类型,只要其成员变量列表一致即可

六、Spring的依赖

  • 用来设置两个bean的创建顺序
  • IOC容器默认情况下是通过applicationContext.xml中bean的配置顺序来决定创建顺序的,配置在前面的bean会先被创建
  • 在不更改applicationContext.xml配置顺序的前提

标签: 吸收薄膜电容器xsd系列

锐单商城拥有海量元器件数据手册IC替代型号,打造 电子元器件IC百科大全!

 锐单商城 - 一站式电子元器件采购平台  

 深圳锐单电子有限公司