Spring基础入门

一.Spring简介

什么是框架?

  • 源自于建筑学,隶属土木工程,后发展到软件工程领域
  • 软件工程框架:经过验证的,具有一定功能的,半成品软件
    • 经过验证
    • 具有一定功能
    • 半成品

在这里插入图片描述

框架的作用

在这里插入图片描述

Spring是什么?

  • Spring是分层的JavaSE/EE应用 full-stack轻量级开源框架
    在这里插入图片描述

Spring体系结构

  • 底层是核心容器
    • Beans
    • Core
    • Context
    • SpringEL表达式
  • 中间层技术
    • AOP
    • Aspects
  • 应用层技术
    • 数据访问与数据集成
    • Web集成
    • Web实现
  • 基于Test测试

Spring发展史

在这里插入图片描述

Spring优势

  • 方便解耦,简化开发
  • 方便集成各种优秀框架
  • 方便程序的测试
  • AOP编程的支持
  • 声明式事务的支持
  • 降低JavaEE API的使用难度
  • Java源码是经典学习范例(长期积累)

二. Spring_IoC

什么是耦合与内聚?

  • 耦合(Coupling):代码书写过程中所使用技术的结合紧密度,用于衡量软件中各个模块之间的互联程度
  • 内聚(Cohesion):代码书写过程中单个模块内部各组成部分间的联系,用于衡量软件中各个功能模块内部的功能联系
  • 程序书写的目标:高内聚,低耦合
    • 就是同一个模块内的各个元素之间要高度紧密,但是各个模块之间的相互依存度却不要那么紧密

工厂模式发展史

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

Spring发展历程

在这里插入图片描述

IoC - 控制反转

  • IoC(Inversion Of Control)控制反转,Spring反向控制应用程序所需要使用的外部资源(需要就向容器内拿)
  • Spring控制的资源全部放置在Spring容器中,该容器称为IoC容器
    在这里插入图片描述

入门案例

IoC入门案例制作步骤

  1. 导入spring坐标(5.1.9.release)
<dependency> 
  <groupId>org.springframework</groupId> 
  <artifactId>spring-context</artifactId> 
  <version>5.1.9.RELEASE</version>
</dependency>
  1. 编写业务层与表现层(模拟)接口与实现类
public interface UserService {
     //业务方法
    void save();
}

public class UserServiceImpl implements UserService{
     //业务方法实现
     public void save(){
     System.out.println("user service running...");
  } 
}
  1. 建立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
https://www.springframework.org/schema/beans/spring-beans.xsd">
 <!-- 1.创建spring控制的资源-->
 <bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/>
</beans>
  1. 配置所需资源(Service)为spring控制的资源
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/>
  1. 表现层(App)通过spring获取资源(Service实例)
public static void main(String[] args) {
//2.加载配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//3.获取资源
  UserService userService = (UserService) ctx.getBean("userService");
  userService.save();
}

三.IoC配置(XML格式)

bean

  • 名称:bean
  • 类型:标签
  • 归属:beans标签
  • 作用:定义spring中的资源,受此标签定义的资源将受到spring控制
  • 格式:
<beans> 
     <bean />
</beans>
  • 基本属性:
<bean id="beanId" name="beanName1,beanName2" class="ClassName"></bean>
  • id:bean的名称,通过id值获取bean
  • class:bean的类型
  • name:bean的名称,可以通过name值获取bean,用于多人配合时给bean起别名

bean属性scope

  • 名称:scope
  • 类型:属性
  • 归属:bean标签
  • 作用:定义bean的作用范围
  • 格式:
<bean scope="singleton"></bean>
  • 取值:
    • singleton:设定创建出的对象保存在spring容器中,是一个单例的对象
    • prototype:设定创建出的对象保存在spring容器中,是一个非单例的对象
    • request、session、application、 websocket :设定创建出的对象放置在web容器对应的位置(不常用)

bean生命周期

  • 名称:init-method,destroy-method
  • 类型:属性
  • 归属:bean标签
  • 作用:定义bean对象在初始化或销毁时完成的工作
  • 格式:
<bean init-method="init" destroy-method="destroy"></bean>
  • 取值:bean对应的类中对应的具体方法名
  • 注意事项:
    • 当scope=“singleton”时,spring容器中有且仅有一个对象,init方法在创建容器时仅执行一次
    • 当scope=“prototype”时,spring容器要创建同一类型的多个对象,init方法在每个对象创建时均执行一次
    • 当scope=“singleton”时,关闭容器会导致bean实例的销毁,调用destroy方法一次
    • 当scope=“prototype”时,对象的销毁由垃圾回收机制gc()控制,destroy方法将不会被执行

bean对象创建方式

  • 名称:factory-bean
  • 类型:属性
  • 归属:bean标签
  • 作用:定义bean对象创建方式,使用静态工厂的形式创建bean,兼容早期遗留系统的升级工作
  • 格式:
<bean class="FactoryClassName" factory-method="factoryMethodName"></bean>
  • 取值:工厂bean中用于获取对象的静态方法名
  • 注意事项:
    • class属性必须配置成静态工厂的类名

bean对象创建方式二

  • 名称:factory-bean,factory-method
  • 类型:属性
  • 归属:bean标签
  • 作用:定义bean对象创建方式,使用实例工厂的形式创建bean,兼容早期遗留系统的升级工作
  • 格式:
<bean factory-bean="factoryBeanId" factory-method="factoryMethodName"></bean>
  • 取值:工厂bean中用于获取对象的实例方法名
  • 注意事项:
    • 使用实例工厂创建bean首先需要将实例工厂配置bean,交由spring进行管理
    • factory-bean是实例工厂的beanID

DI

  • IoC(Inversion Of Control)控制翻转,Spring反向控制应用程序所需要使用的外部资源
  • DI(Dependency Injection)依赖注入,应用程序运行依赖的资源由Spring为其提供,资源进入应用程序的方式称为注入
    在这里插入图片描述

IoC与DI的关系

  • IoC与DI是同一件事站在不同角度看待问题
    • 半杯水 --> 同样是半杯水,乐观的人看待是还剩半杯水,悲观的人看待是只剩半杯水
      在这里插入图片描述

依赖注入的两种方式

  • set注入
  • 构造器注入

set注入(主流)

  • 名称:property
  • 类型:标签
  • 归属:bean标签
  • 作用:使用set方法的形式为bean提供资源
  • 格式:
<bean>
    <property />
</bean>
  • 基本属性:
<property name="propertyName" value="propertyValue" ref="beanId"/>

◆ name:对应bean中的属性名,要求该属性必须提供可访问的set方法(严格规范为此名称是set方法对应名称)
◆ value:设定非引用类型属性对应的值,不能与ref同时使用
◆ ref:设定引用类型属性对应bean的id ,不能与value同时使用

  • 注意:一个bean可以有多个property标签

构造器注入(了解)

  • 名称:constructor-arg
  • 类型:标签
  • 归属:bean标签
  • 作用:使用构造方法的形式为bean提供资源,兼容早期遗留系统的升级工作
  • 格式:
<bean>
    <constructor-arg />
</bean>
  • 基本属性:
<constructor-arg name="argsName" value="argsValue />

◆ name:对应bean中的构造方法所携带的参数名
◆ value:设定非引用类型构造方法参数对应的值,不能与ref同时使用

  • 注意:一个bean可以有多个constructor-arg标签

构造器注入二(了解)

  • 名称:constructor-arg
  • 类型:标签
  • 归属:bean标签
  • 作用:使用构造方法的形式为bean提供资源,兼容早期遗留系统的升级工作
  • 其他属性:
<constructor-arg index="arg-index" type="arg-type" ref="beanId"/>

◆ ref:设定引用类型构造方法参数对应bean的id ,不能与value同时使用
◆ type :设定构造方法参数的类型,用于按类型匹配参数或进行类型校验
◆ index :设定构造方法参数的位置,用于按位置匹配参数,参数index值从0开始计数

集合类型数据注入

  • 名称:array,list,set,map,props
  • 类型:标签
  • 归属:property标签 或 constructor-arg标签
  • 作用:注入集合数据类型属性
  • 格式:
<property>
     <list></list>
</property>

集合类型数据注入——list

<!--List集合类型注入数据--> 
<property name="myList"> 
  <list>
    <value>itheima</value>
    <value>666</value> 
    <ref bean="userService"/>
    <bean class="com.itheima.service.ApplyService"/>
  </list>
</property>

集合类型数据注入——props

<!--Properties类型注入数据-->
<property name="myProps">
  <props> 
    <prop key="username">root</prop>
    <prop key="password">root</prop>
 </props>
</property>

集合类型数据注入——array (了解)

<!--数组类型注入数据--> 
<property name="myArray">
 <array> 
    <value>itheima</value>
    <value>666</value>
    <ref bean="userService"/>
    <bean class="com.itheima.service.ApplyService"/>
 </array>
</property>

集合类型数据注入——set(了解)

<!--Set集合类型注入数据-->
<property name="mySet"> 
 <set>
    <value>itheima</value>
    <value>666</value>
    <ref bean="userService"/>
    <bean class="com.itheima.service.ApplyService"/>
 </set>
</property>

集合类型数据注入——map(了解)

<!--Map集合类型注入数据--> 
<property name="myMap"> 
<map>
   <entry key="name" value-ref="itheima"/>
   <entry key="fame" value-ref="666"/>
   <entry key="userService">
     <ref bean="userService"></ref>
   </entry> 
   <entry key="applyService">
        <bean class="applyService"/>
   </entry>
</map>
</property>

使用p命名空间简化配置(了解)

  • 名称:p:propertyName,p:propertyName-ref
  • 类型:属性
  • 归属:bean标签
  • 作用:为bean注入属性值
  • 格式:
<bean p:propertyName="propertyValue" p:propertyName-ref="beanId"/>
  • 注意:使用p命令空间需要先开启spring对p命令空间的的支持,在beans标签中添加对应空间支持
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
      https://www.springframework.org/schema/beans/spring-beans.xsd">

SpEL (了解)

  • Spring提供了对EL表达式的支持,统一属性注入格式
  • 类型:属性值
  • 归属:value属性值
  • 作用:为bean注入属性值
  • 格式:
<property value="EL"></bean>
  • 注意:所有属性值不区分是否引用类型,统一使用value赋值
  • 所有格式统一使用 value=“*****”
    在这里插入图片描述

properties文件

  • Spring提供了读取外部properties文件的机制,使用读取到的数据为bean的属性赋值
  • 操作步骤
  1. 准备外部properties文件
  2. 开启context命名空间支持
xmlns:context="http://www.springframework.org/schema/context"
  1. 加载指定的properties文件

<context:property-placeholder location="classpath:filename.properties"> //filename可以写*通配符
  1. 使用加载的数据
<property name="propertyName" value="${propertiesName}"/>
  • 注意:如果需要加载所有的properties文件,可以使用*.properties表示加载所有的properties文件
  • 注意:读取数据使用${propertiesName}格式进行,其中propertiesName指properties文件中的属性名

实用_团队开发

  • 名称:import
  • 类型:标签
  • 归属:beans标签
  • 作用:在当前配置文件中导入其他配置文件中的项
  • 格式:
<beans> 
    <import />
</beans>
  • 基本属性:
<import resource=“config.xml"/>

◆ resource:加载的配置文件名

  • Spring容器加载多个配置文件
new ClassPathXmlApplicationContext("config1.xml","config2.xml");
  • Spring容器中的bean定义冲突问题
    • 同id的bean,后定义的覆盖先定义的
    • 导入配置文件可以理解为将导入的配置文件复制粘贴到对应位置
    • 导入配置文件的顺序与位置不同可能会导致最终程序运行结果不同

ApplicationContext对象

在这里插入图片描述

BeanFactory对象

Resource res = new ClassPathResource("applicationContext.xml");
BeanFactory bf = new XmlBeanFactory(res);
UserService userService = (UserService)bf.getBean("userService");

第三方资源配置

  • 阿里数据源方案Druid
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
    <property name="url" value="jdbc:mysql://localhost:3306/spring_ioc"></property> 
    <property name="username" value="root"></property> 
    <property name="password" value="root"></property>
</bean>

综合案例

案例介绍

  • 使用spring整合mybatis技术,完成账户模块(Account)的基础增删改查功能
  • 账户模块对应字段
    • 编号:id
    • 账户名:name
    • 余额:money

在这里插入图片描述

案例制作步骤——基础准备工作

  • 环境准备

    1. 导入Spring坐标,MyBatis坐标,MySQL坐标,Druid坐标
  • 业务类与接口准备

    1. 创建数据库表,并制作相应的实体类Account
    2. 定义业务层接口与数据层接口
    3. 在业务层调用数据层接口,并实现业务方法的调用
  • 基础配置文件

    1. jdbc.properties
    2. MyBatis映射配置文件

案例制作步骤——整合准备工作

  • 整合前基础准备工作
    1. spring配置文件,加上context命名空间,用于加载properties文件
    2. 开启加载properties文件
    3. 配置数据源druid(备用)
    4. 定义service层bean,注入dao层bean
    5. dao的bean无需定义,使用代理自动生成

案例制作步骤——整合工作

  • 整合工作

    1. 导入Spring整合MyBatis坐标

    2. 将mybatis配置成spring管理的bean(SqlSessionFactoryBean)

      ◆ 将原始配置文件中的所有项,转入到当前配置中
      ◼ 数据源转换
      ◼ 映射转换

    3. 通过spring加载mybatis的映射配置文件到spring环境中

    4. 设置类型别名

  • 测试结果

    1. 使用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"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

    <!--加载perperties配置文件的信息-->
    <context:property-placeholder location="classpath:*.properties"/>

    <!--加载druid资源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

    <!--配置service作为spring的bean,注入dao-->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>

    <!--spring整合mybatis后控制的创建连接用的对象-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="typeAliasesPackage" value="com.itheima.domain"/>
    </bean>

    <!--加载mybatis映射配置的扫描,将其作为spring的bean进行管理-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.itheima.dao"/>
    </bean>



</beans>
测试类实现
import com.itheima.domain.Account;
import com.itheima.service.AccountService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        AccountService accountService = (AccountService) ctx.getBean("accountService");
        Account ac = accountService.findById(2);
        System.out.println(ac);

    }
}

注意:

  • 其他接口以及Mapper映射文件,实体类.这里省略.按照三层架构来写即可.
<property name="typeAliasesPackage" value="com.itheima.domain"/>
  • 这一步写的是Mapper映射文件里的返回值以及参数别名,别名为该实体类类名(开头小写) 可以省略不写,但如果不写,Mapper映射文件内的Sql语句返回值类型以及参数类型都必须写该实体类的全限定类名.
  • 10
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值