知识点梳理
课堂讲义
1)Spring简介
1.1)什么是框架
-
源自于建筑学,隶属土木工程,后发展到软件工程领域
-
软件工程中框架的特点:
经过验证
具有一定功能
半成品
1.2)框架的优势
提高开发效率
增强可重用性
提供编写规范
节约维护成本
解耦底层实现原理
既然软件工程中的框架有这么多的优势,那广大软件工程师就就很有必要去学习和使用框架。
1.3)Spring是什么
Spring是分层的JavaSE/EE应用full-stack轻量级开源框架。
分层:针对三层架构设计: Controller层(WEB层)-> Service层(业务层)-> 数据层(DAO)
full-stack(全栈):Spring功能非常强大,能够服务于Java开发过程中的各个层面
轻量级:资源消耗较低,运行速度较快
1.4)Spring的体系结构
两大核心:
IoC:控制反转
AOP:面向切面编程(一种编程思想)
1.5)Spring的发展历史
作者:Rod Johnson 罗宾·约翰逊
1.6)Spring优势
SPRING的优势 | |
---|---|
方便解耦,简化开发 | 第一天 |
方便集成各种优秀框架,比如MyBatis | 第一天 |
方便程序的测试,集成JUnit | 第二天 |
AOP编程的支持 | 第三天 |
声明式事务的支持 | 第四天 |
Java源码是经典学习范例 | 长期学习 |
2)IoC简介
2.1)回顾数据库查询
2.2)耦合与内聚
耦合(Coupling):用于衡量软件中各个模块之间的互联程度。举例说明
内聚(Cohesion):代码书写过程中单个模块内部各组成部分间的联系,用于衡量软件中各个功能模块内部的功能联系,比如UserServiceImpl中的增删查改需要写到一个类中
-
优质程序代码的制作原则:高内聚,低耦合
保证同一个模块内的各个功能之间要高度紧密,但是尽量降低多个模块之间的相互依赖
2.3)工厂模式发展史
UserServiceImpl中需要调用userDao对象:自己通过new UserDaoImpl()产生对象
UserServiceImpl中需要调用userDao对象:通过UserDao工厂类获取
UserServiceImpl中需要调用userDao对象:在工厂类中读取配置文件通过反射产生UserDao的对象
2.4)IoC衍生过程
2.5)IoC概念
IoC(Inversion Of Control)控制反转,Spring反向控制应用程序所需要使用的外部资源。举例
Spring控制的资源将全部放置在Spring容器中,该容器称为IoC容器
IoC是面向对象编程的一种设计原则,可以用来降低代码之间的耦合度
3)入门案例(重点)
3.1)案例环境说明
模拟三层架构中表现层调用业务层功能
表现层:UserApp(使用main方法)模拟UserServlet
业务层:UserService
3.2)IoC入门案例制作步骤
1.导入spring坐标(5.1.9.RELEASE)
2.编写业务层接口与实现类
3.建立spring配置文件:resources\applicationContext.xml
4.配置所需资源(Service)为spring控制的资源
5.表现层(App)通过spring获取资源(Service实例)
3.2.1)导入Spring坐标
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
</dependencies>
3.2.2)编写业务层接口和实现类
public interface UserService {
//业务方法
void save();
}
public class UserServiceImpl implements UserService {
public void save() {
System.out.println("user service running...");
}
}
3.2.3)编写Spring配置文件
src\main\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
https://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
3.2.4)配置UserServiceImpl为Spring控制的资源
<!-- 1.创建spring控制的资源-->
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl"/>
3.2.5)模拟表现层调用业务层
public class UserApp {
public static void main(String[] args) {
//2.加载配置文件
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
//3.获取资源
UserService userService = (UserService) ctx.getBean("userService");
userService.save();
}
}
4)IoC配置(XML格式)
4.1)bean
名称:bean
类型:标签
归属:beans标签
作用:定义spring中的资源,受此标签定义的资源将受到spring控制
-
基本属性:
<bean id="userService" name="userService1, userService2" class="com.itheima.service.impl.UserServiceImpl"/>
id:bean的名称,通过id值获取bean
class:bean的类型
name:bean的名称,可以通过name值获取bean,用于多人共同开发时给bean起别名
4.2)bean属性scope
名称:scope(范围,作用域)
类型:属性
归属:bean标签
作用:定义bean的作用范围
-
格式:
<bean scope="singleton" />
-
取值:
singleton:设定创建出的对象保存在spring容器中,是一个单例的对象
prototype:设定创建出的对象保存在spring容器中,是一个非单例的对象
request、session、application、 websocket :设定创建出的对象放置在web容器对应的位置
public class UserServiceImpl implements UserService {
//通过构造方法判断实例化了几次
public UserServiceImpl(){
System.out.println(" constructor is running...");
}
}
4.3)bean生命周期
名称:init-method,destroy-method
类型:属性
归属:bean标签
作用:定义bean对象在初始化或销毁时完成的工作
-
格式:
<bean id="userService3" init-method="init" destroy-method="destroy"
class="com.itheima.service.impl.UserServiceImpl"/> 取值:bean对应的类中对应的具体方法名
-
在UserServiceImpl中编写init()和destroy()
public void init(){
System.out.println("init....");
}
public void destroy(){
System.out.println("destroy....");
} -
如果需要执行destroy(),必须手动调用ClassPathXmlApplicationContext对象的close的方法
ctx.close();
public class UserApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"applicationContext.xml");
UserService userService = (UserService) ctx.getBean("userService5");
userService.save();
ctx.close();
}
} -
注意事项:
当scope=“singleton”时,spring容器中有且仅有一个对象,init方法在创建容器时仅执行一次
当scope=“singleton”时,关闭容器会导致bean实例的销毁,调用destroy方法一次
当scope=“prototype”时,spring容器要创建同一类型的多个对象,init方法在每个对象创建时均执行一次
当scope=“prototype”时,对象的销毁由垃圾回收机制gc()控制,destroy方法将不会被执行
4.4)bean对象创建方式(了解)
(1)使用静态工厂:factory-method
package com.itheima.service;
public class UserServiceFactory {
public static UserService getService(){
System.out.println("factory create object...");
return new UserServiceImpl();
}
}
名称:factory-method
类型:属性
归属:bean标签
作用:定义bean对象创建方式,使用静态工厂的形式创建bean,兼容早期遗留系统的升级工作
格式:
-
注意事项:class属性必须配置成静态工厂的类名
<bean id="userService5" class="com.itheima.service.UserServiceFactory"
factory-method="getService" /> 取值:工厂bean中用于获取对象的静态方法名
(2)使用实例工厂:factory-bean,factory-method
package com.itheima.service;
public class UserServiceFactory2 {
public UserService getService(){
System.out.println(" instance factory create object...");
return new UserServiceImpl();
}
}
名称:factory-bean,factory-method
类型:属性
归属:bean标签
作用:定义bean对象创建方式,使用实例工厂的形式创建bean,兼容早期遗留系统的升级工作
-
格式:
<!--实例工厂对应的bean-->
<bean id="userServiceFactory" class="com.itheima.service.UserServiceFactory2"/> <!--实例工厂创建bean,依赖工厂对象对应的bean-->
<bean id="userService" factory-bean="userServiceFactory" factory-method="getService"/> 取值:工厂bean中用于获取对象的实例方法名
-
注意事项:
使用实例工厂创建bean首先需要将实例工厂配置bean,交由spring进行管理
factory-bean是实例工厂的beanId
4.5)DI
-
IoC(Inversion Of Control)控制反转
Spring反向控制应用程序所需要使用的外部资源
-
DI(Dependency Injection)依赖注入
应用程序运行依赖的资源由Spring为其提供,资源进入应用程序的方式称为注入
IoC与DI的关系:同一件事站在不同角度看待问题
4.6)set注入(重点)
名称:property
类型:标签
归属:bean标签
作用:使用set方法的形式为bean提供资源
-
格式:
<bean>
<property />
</bean> -
基本属性:
引用类型:自定义类产生的对象,使用ref指定
非引用类型:基本数据类型(int, char,...)和特殊类型String,使用value指定
<!--非引用类型注入-->
<property name="propertyName" value="propertyValue" /> <!--引用类型注入-->
<property name="propertyName" ref="beanId"/>
name:对应bean中的属性名,要求该属性必须提供可访问的set方法(严格规范:此名称是set方法对应名称)
value:设定非引用类型属性对应的值,不能与ref同时使用
ref:设定引用类型属性对应bean的id ,不能与value同时使用
-
使用set注入
-
在UserServiceImpl中添加三个属性,并添加set方法
public class UserServiceImpl implements UserService { /*
1. 基本数据类型:int, char
2. 特殊类型:String
3. 引用类型:自定义类产生的对象
*/
private int num;
private String version; private UserDao userDao; //1.对需要进行诸如的变量添加set方法
public void setNum(int num) {
this.num = num;
} public void setVersion(String version) {
this.version = version;
} public void setUserDao(UserDao userDao) {
this.userDao = userDao;
} public void save() {
System.out.println("user service running..."+num+" "+version);
userDao.save();
}
} -
在applicationContext.xml中完成set注入的配置
<!--2.将要注入的资源声明为bean-->
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl"/> <bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<!--3.将要注入的引用类型的变量通过property属性进行注入,对应的name是要注入的变量名,使用ref属性声明要注入的bean的id-->
<!--reference 引用类型-->
<!--setUserDao()-->
<property name="userDao" ref="userDao"/>
<!--setNum-->
<property name="num" value="666"/>
<!--setVersion-->
<property name="version" value="itheima"/>
</bean> -
获取userService验证是否注入成功
ApplicationContext ctx =
new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ctx.getBean("userService");
userService.save();
-
易错点:类中必须写set方法
xml中的<property />== 代码中的setXxx()
小结
类中必须写set方法
bean中使用property标签注入属性
name表示注入的属性名
引用类型:使用ref进行注入
其他:使用value进行赋值注入
4.7)构造方法注入(了解)
名称:constructor-arg
类型:标签
归属:bean标签
作用:使用构造方法的形式为bean提供资源,兼容早期遗留系统的升级工作
-
格式:
<bean>
<constructor-arg />
</bean> -
基本属性:
<constructor-arg name="argsName" value="argsValue" />
name:对应bean中的构造方法所携带的参数名
value:设定非引用类型构造方法参数对应的值,不能与ref同时使用
-
其他属性:
-
type :设定构造方法参数的类型,用于按类型匹配参数或进行类型校验
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<!--根据类型进行匹配-->
<constructor-arg type="java.lang.String" value="itcast"/>
<constructor-arg type="int" value="666666"/>
<constructor-arg type="com.itheima.dao.UserDao" ref="userDao"/>
</bean> -
index :设定构造方法参数的位置,用于按位置匹配参数,参数index值从0开始计数
<bean id="userDao" class="com.itheima.dao.impl.UserDaoImpl">
<!--根据index进行匹配-->
<!--argument 参数-->
<constructor-arg index="1" value="123"/>
<constructor-arg index="2" value="com.mysql.jdbc.Driver"/>
<constructor-arg index="0" value="root"/>
</bean>
-
4.8)集合类型数据注入
名称:array,list,set,map,props
类型:标签
归属:property标签 或 constructor-arg标签
作用:注入集合数据类型属性
格式:
(1)集合类型数据注入——list
<!--setAl()-->
<property name="al">
<list>
<value>itheima</value>
<value>66666</value>
</list>
</property>
(2)集合类型数据注入——props
<property name="properties">
<props>
<prop key="name">itheima666</prop>
<prop key="value">666666</prop>
</props>
</property>
(3)集合类型数据注入——array (了解)
<property name="arr">
<array>
<value>123456</value>
<value>66666</value>
</array>
</property>
(4)集合类型数据注入——set(了解)
<property name="hs">
<set>
<value>itheima</value>
<value>66666</value>
</set>
</property>
(5)集合类型数据注入——map(了解)
<property name="hm">
<map>
<entry key="name" value="itheima66666"/>
<entry key="value" value="6666666666"/>
</map>
</property>
扩展知识
将引用类型注入到map当中,使用value-ref属性
<map>
<entry key="name" value="itheima66666"/>
<entry key="value" value="6666666666"/> <entry key="userDao" value-ref="userDao" />
</map>
4.9)使用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">
-
示例:
<bean
id="userService"
class="com.itheima.service.impl.UserServiceImpl"
p:num="888"
p:version="1.1"
p:userDao-ref="userDao"
p:bookDao-ref="bookDao"
/> 不建议使用
4.10)SpEL (了解)
JSP中的EL表达式:${key}
Spring提供了对EL表达式的支持,统一属性注入格式
类型:属性值
归属:value属性值
作用:为bean注入属性值
-
格式:
<property name="name" value="#{value}"/>
注意:所有属性值不区分是否引用类型,统一使用value赋值
-
所有格式统一使用 value=“#{}”
常量 #{10} #{3.14} #{2e5} #{‘itcast’}
引用bean #{beanId}
引用bean属性 #{beanId.propertyName}
引用bean方法 beanId.methodName().method2()
引用静态方法 T(java.lang.Math).PI
运算符支持 #{3 lt 4 == 4 ge 3}
正则表达式支持 #{user.name matches‘[a-z]{6,}’}
集合支持 #{likes[3]}
-
示例:
<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
<property name="userDao" value="#{userDao}"/>
<property name="bookDao" value="#{bookDao}"/>
<property name="num" value="#{666666666}"/>
<property name="version" value="#{'itcast'}"/>
</bean> 不建议使用
4.11)properties文件(重点)
Spring提供了读取外部properties文件的机制,使用读取到的数据为bean的属性赋值
-
操作步骤
1.准备外部data.properties文件,放置在src\main\resources目录下
#系统环境变量中存在username,在cmd中使用echo %username%查看
#username=root
username=root
pwd=root2.开启context命名空间支持
<?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:p="http://www.springframework.org/schema/p"
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">
3.加载指定的properties文件
<context:property-placeholder location="classpath:filename.properties" />
4.使用加载的数据
<property name="propertyName" value="${propertiesName}"/>
注意:如果需要加载多个properties文件,可以使用
*.properties
表示加载所有的properties文件注意:读取数据使用${propertiesName}格式进行,其中propertiesName指properties文件中的属性名
-
注意和系统环境变量的冲突
cmd
echo %JAVA_HOME%
echo %username%扩展知识:使用local-override属性覆盖操作系统中的环境变量
<context:property-placeholder location="classpath:filename.properties"
local-override="true" />
4.12)多配置文件
Spring多配置文件的使用方式:使用import即可。
名称:import
类型:标签
归属:beans标签
作用:在当前配置文件中导入其他配置文件中的项
-
第一种导入方式(常用):使用import标签配置resource:加载的配置文件名
<beans>
<import resource="applicationContext-user.xml"/>
</beans> -
第二种方式Spring容器加载多个配置文件(了解)
new ClassPathXmlApplicationContext("config1.xml","config2.xml");
-
Spring容器中的bean定义冲突问题
同id的bean,后定义的覆盖先定义的
导入配置文件可以理解为将导入的配置文件复制粘贴到对应位置
导入配置文件的顺序与位置不同可能会导致最终程序运行结果不同
4.13)ApplicationContext(了解)
使用BeanFactory
Resource res = new ClassPathResource("applicationContext.xml");
BeanFactory bf = new XmlBeanFactory(res);
//UserService userService = (UserService)bf.getBean("userService");
4.14)第三方资源配置(重点)
-
回顾之前Druid使用方式
//获取配置文件的流对象
InputStream is = DruidTest1.class.getClassLoader().getResourceAsStream("druid.properties"); //1.通过Properties集合,加载配置文件
Properties prop = new Properties();
prop.load(is); //2.通过Druid连接池工厂类获取数据库连接池对象
//driverClassName=com.mysql.jdbc.Driver
//url=jdbc:mysql://192.168.59.129:3306/db14
//username=root
//password=itheima
DataSource dataSource = DruidDataSourceFactory.createDataSource(prop); -
引入Druid坐标
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency> -
使用Spring配置Druid数据源
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring_db"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
5)综合案例(重点)
5.1)案例介绍
使用spring整合mybatis技术,完成账户模块(Account)的基础增删改查功能
-
账户模块对应字段
编号:id
账户名:name
余额:money
5.2)案例分析
下图中标红部分需要咱们来实现
5.3)基础准备工作
-
环境准备:导入Spring坐标,MyBatis坐标,MySQL坐标,Druid坐标
<dependencies> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency> <dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
</dependencies> -
业务类与接口准备
创建数据库表,并制作相应的实体类Account
定义业务层接口与数据层接口
在业务层调用数据层接口,并实现业务方法的调用
-
基础配置文件
jdbc.properties
MyBatis映射配置文件
5.4)整合准备工作
1.spring配置文件,加上context命名空间,用于加载properties文件
<?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">
2.开启加载properties文件
<context:property-placeholder location="classpath:jdbc.properties"/>
3.配置数据源Druid
<!--加载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>
4.定义service层bean,并使用property标签注入dao
<!--配置service作为spring的bean,注入dao-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
5.dao的bean无需定义,MyBatis会自动生成代理对象
5.5)整合工作
1.导入Spring整合MyBatis坐标
<!--Spring整合MyBatis坐标-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.0</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
2.将mybatis配置成spring管理的bean(SqlSessionFactoryBean),并将类型别名交由spring处理
<!--spring整合mybatis后控制的创建连接用的对象-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.itheima.domain"/>
</bean>
3.通过spring加载mybatis的映射配置文件到spring环境中(映射Mapper扫描工作交由spring处理)
<!--加载mybatis映射配置的扫描,将其作为spring的bean进行管理-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.dao"/>
</bean>
4.使用spring环境获取业务层bean,执行操作
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(13);
System.out.println(ac); //保存
Account account = new Account();
account.setName("jack");
account.setMoney(123456.78);
accountService.save(account);
}
}