9. 初识Spring框架

9. 初识Spring框架

   本节主要介绍了Spring框架的生态环境与概念,另外与传统创建对象方式进行对比,引出了IOC容器通过无参构造和有参构造创建对象的方式,并对Spring底层创建对象做了简要的说明。

1. Spring概述

        什么是 Spring?Spring 是一个企业级开发框架,是软件设计层面的框架,优势在于可以将应用程序体系进行分层架构,开发者可以自主选择组件,目前 Spring 已经是 Java 企业级项目开发事实上的行业标准。

Spring 三大核心模块:

  1. Spring Framework 是整个 Spring 生态的基石,通常所说的 Spring 就是 Spring Framework。
  2. Spring Boot 是一个快速开发的框架,自动集成了各种主流的组件;
  3. Spring Cloud 是一套整合了分布式应用常用模块的框架,快速实现微服务应用开发。

Spring 两大核心机制

  • IOC(控制反转) / DI(依赖注入)
  • AOP(面向切面编程)

        从开发的角度讲,我们使用 Spring 框架就是用它的 IOC 和 AOP,IOC(Inversion of Control,即控制反转)是典型的工厂模式,通过工厂去注入对象,AOP(Aspect Oriented Programming)则是代理模式的体现。

Spring 体系结构

在这里插入图片描述

从设计模式和数据持久化角度来看,通常开发者可以选择的组件如下:

  1. MVC:Strut2、Spring MVC
  2. ORMapping:Hibernate、MyBatis、Spring Data、MyBatis Plus

企业级项目特点:

  • 大规模:用户数量多、数据规模大、功能模块众多
  • 性能和安全要求高
  • 业务复杂
  • 灵活多变

Spring 的优点:

  1. 低侵入式设计
  2. 独立于各种应用服务器
  3. 依赖注入特性将组件关系透明化,降低了耦合度
  4. 面向切面编程的特性允许将通用任务进行集中式管理
  5. 与第三方框架的良好整合

2. Spring IOC

什么是控制反转?

        在传统的程序开发中,需要调用对象时,通常由调用者来创建被调用的实例,即对象是由调用者主动 new 出来的。但在 Spring 框架中创建对象的工作不再由调用者来完成,而是交给 IoC 容器来创建,再推送给调用者,整个流程完成反转,所以是控制反转。

创建实体类 Student:

package com.trainingl.entity;

import lombok.Data;

@Data
public class Student {
    private Integer id;
    private String name;
    private Double score;
}

使用传统的开发方式,手动实例化对象 new Student()

package com.trainingl.test;

import com.trainingl.entity.Student;

public class Test {
    public static void main(String[] args) {
        Student student = new Student();
        student.setId(1);
        student.setName("张三");
        student.setScore(85.5);
        System.out.println(student);
        /*
        	控制台打印输出:Student(id=1, name=张三, score=85.5)
        */
    }
}

2.1 IOC容器创建对象

在 Maven 工程下的 pom.xml 里添加 Spring 相关的依赖

<?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>com.trainingl</groupId>
    <artifactId>springPro</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <!--Spring框架的依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.2.4.RELEASE</version>
        </dependency>

        <!--lombok插件自动生成实体类的GET、SET、toString以及无参构造-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.12</version>
        </dependency>
    </dependencies>
</project>

在配置文件中添加管理对象的信息,XML格式的配置文件,文件名可以自定义,比如创建配置文件 spring.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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	   http://www.springframework.org/schema/context
	   http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
    <!--配置一个对象-->
    <bean id="student" class="com.trainingl.entity.Student">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="score" value="85.5"></property>
    </bean>
</beans>

说明:在 Spring IoC 容器中通过<bean> 标签配置一个对象,其中 id 来获取对象的标识,class 是创建对象的模板类。

调用 API 获取对象,Spring 提供了两种方式来获取对象:id 或者 运行时类

① 通过 id 获取对象

package com.trainingl.test;

import com.trainingl.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        //1.加载spring.xml配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        //2.通过id值来获取对象
        Student stu = (Student)applicationContext.getBean("student");
        System.out.println(stu);
    }
}

控制台的打印信息:
在这里插入图片描述

        Spring 底层解析:先通过 XML 解析来加载 spring.xml 配置文件,然后使用反射机制调用无参构造函数动态创建对象,并调用 setter 方法完成对象属性的赋值,最后将创建好的对象放在一个类似于 HashMap 的容器里,调用 getBean 方法获取对象时,相当于 map.get(id) 返回一个对象。

② 通过运行时类获取对象

package com.trainingl.test;

import com.trainingl.entity.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        //1.加载spring.xml配置文件
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring.xml");
        //2.通过运行时类来获取对象
        Student stu = (Student)applicationContext.getBean(Student.class);
        System.out.println(stu);
    }
}

这种方法当 spring.xml 中配置两个 Student 类的 bean 时,程序会报错,因为此时两个 bean 都是由 Student 类生 成的,IOC容器无法将两个 bean 都返回, 必须指定一个唯一的 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"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
	   http://www.springframework.org/schema/context
	   http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
    <bean id="student1" class="com.trainingl.entity.Student">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="score" value="85.5"></property>
    </bean>

    <bean id="student2" class="com.trainingl.entity.Student">
        <property name="id" value="1"></property>
        <property name="name" value="张三"></property>
        <property name="score" value="85.5"></property>
    </bean>
</beans>

控制台的打印信息如下:


2.2 配置文件详解

spring.xml

<!--配置一个对象,省略头信息-->
<bean id="student" class="com.trainingl.entity.Student">
    <property name="id" value="1"></property>
    <property name="name" value="张三"></property>
    <property name="score" value="85.5"></property>
</bean>

1、通过配置 bean 标签来完成对象的管理;

  • id:对象标识符(唯一);
  • class:对象的模板类(所有交给 IoC 容器来管理的类必须有构造方法,因为 Spring 底层是通过反射机制来创建对象时,调用的是无参构造或者有参构造);

2、对象的成员变量通过 property 标签完成赋值;

  • name:成员变量名;
  • value:成员变量值(基本数据类型和 String 类型可以直接赋值,如果是其他引用类型,不能通过 value 赋值);

3、通过有参构造创建 bean;

  1. 实体类中必须有对应的有参构造函数,否则会报错;
  2. 配置文件中添加参数标签 <constrcutor-arg></constructor>
<bean id="student" class="com.trainingl.entity.Student">
    <constructor-arg name="id" value="12"></constructor-arg>
    <constructor-arg name="name" value="王五"></constructor-arg>
    <constructor-arg name="score" value="85"></constructor-arg>
</bean>

与上面的无参构造的配置不同,有参构造创建对象时,边创建边赋值,而无参构造是先创建对象,然后再通过 setter 方法完成属性的赋值。

当然,也可以通过索引的方式改变顺序:

<bean id="student" class="com.trainingl.entity.Student">
    <constructor-arg index="0" value="1"></constructor-arg>
    <constructor-arg index="2" value="85"></constructor-arg>
    <constructor-arg index="1" value="王五"></constructor-arg>
</bean>

        在使用 Spring IoC容器创建对象时,调用无参构造 + setter方法还是直接有参构造创建对象仅仅是在XML配置上有所不同,另外一定要注意的是:采用何种方式时,一定要保证对应的该方法是存在的。例如,xml文件是全参构造来配置参数,但实体类里面没有全参构造方法,那必然是会报错的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值