Spring
一,spring简介
1,框架的概念
- 软件工程框架:经过验证的,具有一定功能的,半成品软件
- 框架的作用:提高开发效率,提供编写规范,增强可重用性,解耦底层实现原理,方便程序测试,方便集成各种优秀框架。
2, spring概念与体系结构
- Spring是分层的JavaSE/EE应用 full-stack(一站式)轻量级开源框架。
- 体系结构:两大主要核心
- AOP:面向切面编程,不修改源代码的情况下进行功能增强
- IOC:控制反转,方便解耦,简化开发(之前的是自己new对象,现在创建对象,对象与对象之间的关系包括调用,通过Spring的方式进行实现)
二,Ioc
1,控制反转概念
-
IoC思想基于IoC容器完成,IoC容器底层就是对象工厂,
-
Spring提供IoC容器实现的两种方式(两个接口)
- BeanFactory:Spring内部使用接口(加载配置文件的时候不会创建对象,只有在获取对象getBean时才会去创建对象)
- ApplicationContext:BeanFactory接口的子接口,增加了更强大的功能(加载配置文件的时候就会把配置文件中的对象创建好)
-
loc(Inversion of control) 控制反转,spring反向控制应用程序所需要的外部资源。把**对象创建和对象之间的调用过程,**交给Spring进行管理。
-
spring控制的资源全部放置在Spring容器中,该容器称为Ioc容器。(Spring来帮我们创建对象,注入属性,进行对象的实例化,不再需要我们自己去new了,我们需要的时候直接被动的取就行,不用再主动地去new,降低了耦合)
2,耦合与内聚(低耦合高内聚)
- 耦合(Coupling):代码书写过程中所使用技术的结合紧密度,用于衡量软件中各个模块之间的互联程度
- 内聚(Cohesion):代码书写过程中单个模块内部各组成部分间的联系,用于衡量软件中各个功能模块内部的功能联系
- 程序书写的目标:高内聚,低耦合
- 就是同一个模块内的各个元素之间要高度紧密,但是各个模块之间的相互依存度却不要那么紧密
3.IOC入门案例
-
模拟三层架构中表现层调用业务层功能
- 表现层:UerApp模拟UserServlet(使用main方法模拟)
- 业务层:UserService
-
步骤
-
-
导入spring坐标(5.1.9.release)也就是pom文件中导入dependency依赖
-
编写业务层与表现层(模拟)接口与实现类
-
建立spring配置文件 (applicationContext.xml)
-
配置所需资源(Service)为spring控制的资源
-
表现层(App)通过spring获取资源(Service实例)
<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"> <!--创建spring控制的资源,这里相当于new了一个实现类,然后把这个实现类放入他自己的core容器--> <bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/> </beans>
public class UserApp { public static void main(String[] args) { //加载配置文件 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml"); //获取资源 UserService userService = (UserService) ctx.getBean("userService");//这里的类名需要和配置文件的bean的id名相同 userService.save(); } }
- 注意
- 这里的id为了规范写成对应的接口名。
- 这里getBean中强转,是对应实现类实现的接口类型
- 这里调用实现类的方法,需要new一个实现类,所以配置文件中注入实现类的bean,然后测试中getBean拿到spring提供的资源(new userServiceImpl),直接调用方法即可。
- 注意
-
-
三,IoC配置
1,bean的基本配置
-
Bean管理
- Spring创建对象
- Spring注入属性
-
Bean管理操作方式有两种(xml配置文件实现和注解方式实现)
-
bean中属性解释:
User user=context.getBean("user",User.class);
- bean标签中id属性是一个标识,配置文件中id是user,那么getBean中第一个参数就是user,要一一对应
- class:类全路径包括包名
- 创建对象时,默认执行无参构造,如果写了有参构造,要再写一个无参构造,不然报错
- name:bean的名称,可以通过name值获取bean,用于多人配合时给bean起别名(测试类中getBean()中的参数可以换成这些别名也可以执行)
-
基于xml方式注入属性
- DI:依赖注入,就是注入属性,注入属性前要先创建对象(set注入和构造方法注入)
-
名称: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起别名
2,scope属性
-
bean属性scope
-
名称:scope
⚫ 类型:属性
⚫ 归属:bean标签
⚫ 作用:定义bean的作用范围
⚫ 格式:
<bean scope="singleton"></bean>
-
取值:
◆ singleton:设定创建出的对象保存在spring容器中,是一个单例的对象
◆ prototype:设定创建出的对象保存在spring容器中,是一个非单例的对象
◆ request、session、application、 websocket :设定创建出的对象放置在web容器对应的位置
3,bean的生命周期
-
名称:init-method,destroy-method
⚫ 类型:属性
⚫ 归属:bean标签
⚫ 作用:定义bean对象在初始化或销毁时完成的工作
⚫ 格式:
<!--init和destory是实现类中对应的方法名--> <bean init-method="init" destroy-method="destroy></bean>
-
取值:bean对应的类中对应的具体方法名
- init-method:在构造方法执行完成后执行初始化方法。
- destory-method:在容器正常关闭的时候执行此方法。
-
注意事项:
◆ 当scope=“singleton”时,spring容器中有且仅有一个对象,init方法在创建容器时仅执行一次
◆ 当scope=“prototype”时,spring容器要创建同一类型的多个对象,init方法在每个对象创建时均执行一次 (也就是说,当非单例时,加载容器并不创建对象,只有在getBean获取对象时才创建,获取一次创建一次)
◆ 当scope=“singleton”时,关闭容器会导致bean实例的销毁,调用destroy方法一次
◆ 当scope=“prototype”时,对象的销毁由垃圾回收机制gc()控制,destroy方法将不会被执行
4,静态工厂与实例工厂创建bean
-
名称:factory-bean ,factory-method
⚫ 类型:属性
⚫ 归属:bean标签
⚫ 作用:定义bean对象创建方式,使用静态工厂或者实例工厂的形式创建bean,兼容早期遗留系统的升级工作
⚫ 注意事项:
◆ 使用实例工厂创建bean首先需要将实例工厂配置bean,交由spring进行管理
◆ factory-bean是实例工厂的beanId
<!--静态工厂创建bean,创建出来的对象是--> <bean id="userService4" class="com.itheima.service.UserServiceFactory" factory-method="getService"/> <!--实例工厂--> <bean id="factoryBean" class="com.itheima.service.UserServiceFactory2"/> <bean id="userService5" factory-bean="factoryBean" factory-method="getService"/>
public class UserServiceFactory{
public static UserService getService(){
System.out.pringtln("1234");
return new UserServiceImpl();
}
}
//静态工厂需要new一个UserService的实例UserServiceImpl,所以配置文件上面这样写,调用方法getService,拿到真正的实例UserServiceImpl。
5,依赖注入概念(DI)
-
IoC(Inversion Of Control)控制反转,Spring反向控制应用程序所需要使用的外部资源
DI(Dependency Injection)依赖注入,应用程序运行依赖的资源由Spring为其提供,资源进入应用
程序的方式称为注入
-
IoC与DI的关系
- IoC与DI是同一件事站在不同角度看待问题
-
依赖注入的两种方式
-
set注入
-
构造器注入
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CPAJFm8a-1595069739390)(E:\每日讲课笔记\Spring\img\1594702170105.png)]
-
6,set注入
-
名称:property
⚫ 类型:标签
⚫ 归属:bean标签
⚫ 作用:使用set方法的形式为bean提供资源
-
基本属性
◆ name:对应bean中的属性名,要求该属性必须提供可访问的set方法(严格规范为此名称是set方法对应名称,也就是set方法后面的方法名,首字母小写,eg:setAccountDao,name就是accountDao)
◆ value:设定非引用类型属性对应的值,不能与ref同时使用
◆ ref:设定引用类型属性对应bean的id ,不能与value同时使用
⚫ 注意:一个bean可以有多个property标签
7,外部bean注入
-
创建两个类service和dao类
-
service中调用dao里面的方法
-
在spring配置文件中进行配置
public class UserService{ private UserDao userDao; public void setUserDao(UserDao userDao){ this.userDao=usereDao; } public void add(){ System.out.println("service add..."); userDao.update(); } }
<!--把service和dao两个对象先创建出来-->
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<!--注入userDao对象
name属性:类里面定义的属性名称
ref属性:创建userDao对象bean标签的id值,要对应上
-->
<property name="userDao" ref="userDaoImpl"/>
</bean>
<bean id="userDaoImpl" class="com.itheima.dao.impl.UserDaoImpl"></bean>
<!--这代表注入属性是引用类型的话,要在外面创建一个对象(比如 <bean id="userDaoImpl" class="com.itheima.dao.impl.UserDaoImpl"></bean>),然后用ref="userDaoImpl"引入。第二种方式就是下面讲的内部bean方法-->
8,注入属性-内部bean和级联赋值
<!--上面说的内部bean-->
<bean id="emp" class="com.itheima.bean.Emp">
<!--设置两个普通属性-->
<property name="ename" value="lucy"/>
<property name="gender" value="女"/>
<!--第三个属性是引用类型,所以用内部bean方式-->
<property name="dept">
<bean id="dept" class="com.itheima.Dept">
<property name="dname" value="安保"/>
</bean>
</property>
</bean>
9,IoC操作Bean管理(xml注入集合属性)
-
两个注意点
-
在集合里面设置的是对象类型值怎么办(场景:集合中不只有放普通类型的数据,还有放引用类型数据的情况)
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"> <!--注入list集合类型,有一个值是对象类型--> <property name="courseList"> <list> <ref bean="courseList1"></ref> <ref bean="courseList2"></ref> </list> </property> </bean> <!--创建多个course对象,上面ref的bean属性引入course对象类型--> <bean id="course1" class="com.itheima.Course"> <property name="cname" value="Spring框架"></property> </bean> <bean id="course2" class="com.itheima.Course"> <property name="cname" value="Mybatis框架"></property> </bean>
-
把集合注入部分提取出来,方便公共使用
-
10,构造方法注入
-
名称:constructor-arg
⚫ 类型:标签
⚫ 归属:bean标签
⚫ 作用:使用构造方法的形式为bean提供资源,兼容早期遗留系统的升级工作
<bean> <constructor-arg /> </bean>
-
基本属性:
<constructor-arg name="argsName" value="argsValue />
-
name:对应bean中的构造方法所携带的参数名
value:设定非引用类型构造方法参数对应的值,不能与ref同时使用 ,ref设定一个引用值
⚫ 注意:一个bean可以有多个constructor-arg标签
public class Student{ private String name; private String gender; public Student (String name, String gender){ this.name=name; this.gender=gender; } }
<bean id="student" class="com.itheima.dao.Student"> <constructor-arg name="name" value="jl"/> <constructor-arg name="gender" value="nv"/> </bean>
11,集合注入
-
名称:array,list,set,map,props
⚫ 类型:标签
⚫ 归属:property标签 或 constructor-arg标签
⚫ 作用:注入集合数据类型属性
-
格式
<!--List集合类型注入数据--> <property name="myList"> <list> <value>itheima</value> <value>666</value> <ref bean="userService"/> <bean class="com.itheima.service.ApplyService"/> </list> </property>
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<property name="al">
<array>
<value>1</value>
<value>2</value>
</array>
</property>
<property name="properties">
<props>
<prop key="diver">com.mycql.jdbc.diver</prop>
</props>
</property>
<property name="hm">
<map>
<entry key="name" value="itheima"/>
<entry key="student" value-ref="student"/><!--这里对应student的bean的id-->
</map>
</property>
</bean>
12,p命名空间的引入与使用
13,SpEL
14,读取properties文件信息
-
Spring提供了读取外部properties文件的机制,使用读取到的数据为bean的属性赋值
⚫ 操作步骤
-
准备外部properties文件
-
开启context命名空间支持(这些可以打出<context:property-placeholder回车可以自动创造出来)
xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd
-
加载指定的properties文件
<context:property-placeholder location="classpath:filename.properties"/>
-
使用加载的数据
<property name="propertyName" value="${propertiesName}"/>
⚫ 注意:如果需要加载所有的properties文件,可以使用***.properties**表示加载所有的properties文件
⚫ 注意:读取数据使用**${propertiesName}格式进行,其中propertiesName**指properties文件中的属性名,name="propertyName"指的是类中的属性名,注意这两个名字不一样。
<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"> <!--创建spring控制的资源--> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="pasd" class="com.itheima.dao.UserDao"> <property name="username" value="${jdbc.name}"/> <property name="password" value="${jdbc.driver}"/> </bean> </beans>
-
15,import导入配置文件
-
加载配置文件是在主配置文件中加载,对应的key可以在子配置文件中使用,idea爆红也无所谓。
<context:property-placeholder location="classpath:jdbc.properties"/> //此标签最好只出现一次,不然会报各种错,建议只在主配置文件中出现一次
16,ApplicationContext对象层次结构
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DhhaXlZ2-1595069739395)(E:\每日讲课笔记\Spring\img\1594723099083.png)]
- 区别:BeanFactory创建的bean采用延迟加载形式,使用才创建,也就是getBean时才创建
- ApplicationContext创建的bean采用立即加载形式,也就是加载容器时就创建了
17,第三方bean的配置方式
-
阿里数据源方案Druid
-
先导入坐标
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.16</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency>
-
然后applicationContext.xml中导入bean
<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>
结合老师的笔记
四,综合案例
1,spring整合mybatis案例基础环境分析
-
⚫ 使用spring整合mybatis技术,完成账户模块(Account)的基础增删改查功能
⚫ 账户模块对应字段
◆ 编号:id
◆ 账户名:name
◆ 余额:money
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wkhgX9SV-1595069739399)(E:\每日讲课笔记\Spring\img\1594813443212.png)]
-
准备工作
-
⚫ 环境准备
- 导入Spring坐标,MyBatis坐标,MySQL坐标,Druid坐标
⚫ 业务类与接口准备
-
创建数据库表,并制作相应的实体类Account
-
定义业务层接口与数据层接口
-
在业务层调用数据层接口,并实现业务方法的调用
⚫ 基础配置文件
- jdbc.properties
- MyBatis映射配置文件
-
⚫ 整合前基础准备工作
-
spring配置文件,加上context命名空间,用于加载properties文件
-
开启加载properties文件
-
配置数据源druid(备用)
-
定义service层bean,注入dao层bean
-
dao的bean无需定义,使用代理自动生成
-
-
⚫ 整合工作
-
导入Spring整合MyBatis坐标
-
将mybatis配置成spring管理的bean(SqlSessionFactoryBean)
◆ 将原始配置文件中的所有项,转入到当前配置中
◼ 数据源转换
◼ 映射转换
-
通过spring加载mybatis的映射配置文件到spring环境中
-
设置类型别名
⚫ 测试结果
-
使用spring环境加载业务层bean,执行操作
⚫ 需要专用的spring整合mybatis的jar包
⚫ Mybatis核心配置文件消失
◼ 环境environment转换成数据源对象
◼ 映射Mapper扫描工作交由spring处理
◼ 类型别名交由spring处理
⚫ 业务发起使用spring上下文对象获取对应的bean
-
2,spring整合mybatis代码
-
项目结构
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-K2fu6HxH-1595069739406)(E:\每日讲课笔记\Spring\img\1594814076134.png)]
jdbc配置文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.23.129:3306/db14
jdbc.username=root
jdbc.password=root
dao层
package com.itheima.dao;
import com.itheima.domain.Account;
/* dao的bean无需定义,使用代理自动生成*/
import java.util.List;
public interface AccountDao {
void save(Account account);
void delete(Integer id);
void update(Account account);
List<Account> findAll();
Account findById(Integer id);
}
对应的配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.dao.AccountDao">
<!--配置根据id查询-->
<select id="findById" resultType="account" parameterType="int">
select * from account where id = #{id}
</select>
<!--配置查询所有-->
<select id="findAll" resultType="account">
select * from account
</select>
<!--配置保存-->
<insert id="save" parameterType="account">
insert into account(name,money)values(#{name},#{money})
</insert>
<!--配置删除-->
<delete id="delete" parameterType="int">
delete from account where id = #{id}
</delete>
<!--配置更新-->
<update id="update" parameterType="account">
update account set name=#{name},money=#{money} where id=#{id}
</update>
</mapper>
domain层
package com.itheima.domain;
import java.io.Serializable;
public class Account implements Serializable {
private Integer id;
private String name;
private Double money;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", name='" + name + '\'' +
", money=" + money +
'}';
}
}
service层接口
package com.itheima.service;
import com.itheima.domain.Account;
import java.util.List;
public interface AccountService {
void save(Account account);
void delete(Integer id);
void update(Account account);
List<Account> findAll();
Account findById(Integer id);
}
service层实现类
package com.itheima.service.impl;
import com.itheima.dao.AccountDao;
import com.itheima.domain.Account;
import com.itheima.service.AccountService;
import java.util.List;
/* 定义service层bean,注入dao层bean
dao的bean无需定义,使用代理自动生成*/
public class AccountServiceImpl implements AccountService {
//相当于需要AccountDao accountDao=new AccountDaoImpl,也就是需要dao层的对象
//去调用他其中的方法,下面就是注入dao层bean
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;//set方法注入
}
//增删改查调用方法
public void save(Account account) {
accountDao.save(account);
}
public void update(Account account){
accountDao.update(account);
}
public void delete(Integer id) {
accountDao.delete(id);
}
public Account findById(Integer id) {
return accountDao.findById(id);
}
public List<Account> findAll() {
return accountDao.findAll();
}
}
配置文件
<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">
<!--加载properties配置文件的信息-->
<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"/>
<!--ref的值就是需要动态代理的AccountDao的首字母小写-->
</bean>
<!--spring整合mybatis后控制的创建连接用的对象-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/><!--需要一个连接池-->
<property name="typeAliasesPackage" value="com.itheima.domain"/><!--类型别名,AccountDao.xml中可以直接类名-->
</bean>
<!--加载mybatis映射配置的扫描,将其作为spring的bean进行管理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.dao"/><!--这里就是实现上面的动态代理-->
</bean>
</beans>
测试
import com.alibaba.druid.pool.DruidDataSource;
import com.itheima.domain.Account;
import com.itheima.service.AccountService;
import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class UserApp {
public static void main(String[] args) {
//加载配置文件
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext1.xml");
//获取资源
AccountService accountService = (AccountService) ctx.getBean("accountService");//这里的类名需要和配置文件的bean的id名相同
List<Account> all = accountService.findAll();
System.out.println(all);
}
}
爱了吗
<property name="basePackage" value="com.itheima.dao"/><!--这里就是实现上面的动态代理-->
</bean>
```
测试
import com.alibaba.druid.pool.DruidDataSource;
import com.itheima.domain.Account;
import com.itheima.service.AccountService;
import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class UserApp {
public static void main(String[] args) {
//加载配置文件
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext1.xml");
//获取资源
AccountService accountService = (AccountService) ctx.getBean("accountService");//这里的类名需要和配置文件的bean的id名相同
List<Account> all = accountService.findAll();
System.out.println(all);
}
}
爱了吗