1)Spring简介
1.1)什么是框架
-
源自于建筑学,隶属土木工程,后发展到软件工程领域
-
软件工程框架:经过验证的,具有一定功能的,半成品软件
-
经过验证
-
具有一定功能
-
半成品
-
1.2)框架的作用
1.3)Spring是什么
Spring是分层的JavaSE/EE应用full-stack轻量级开源框架
1.4)Spring的体系结构
1.5)Spring的发展历史
1.6)Spring优势
Spring的优势 | |
---|---|
方便解耦,简化开发 | 第一天 |
方便集成各种优秀框架 | 第一天 |
方便程序的测试 | 第二天 |
AOP编程的支持 | 第三天 |
声明式事务的支持 | 第四天 |
降低JavaEE API的使用难度 | 第四天 |
Java源码是经典学习范例 | 长期学习 |
2)IoC简介
2.1)优质程序代码的制作原则
2.2)耦合与内聚
-
耦合(Coupling):代码书写过程中所使用技术的结合紧密度,用于衡量软件中各个模块之间的互联程度
-
内聚(Cohesion):代码书写过程中单个模块内部各组成部分间的联系,用于衡量软件中各个功能模块内部的功能联系
程序书写的目标:高内聚,低耦合 -
就是同一个模块内的各个元素之间要高度紧密,但是各个模块之间的相互依存度却不要那么紧密
2.3)工厂模式发展史
2.4)Spring发展历程
2.5)IoC
-
IoC(Inversion Of Control)控制反转,Spring反向控制应用程序所需要使用的外部资源
-
Spring控制的资源全部放置在Spring容器中,该容器称为IoC容器
3)入门案例
3.1)案例环境说明
模拟三层架构中表现层调用业务层功能
-
表现层:UserApp模拟UserServlet(使用main方法模拟)
-
业务层:UserService
3.2)IoC入门案例制作步骤
1.导入spring坐标(5.1.9.release)
2.编写业务层与表现层(模拟)接口与实现类
3.建立spring配置文件
4.配置所需资源(Service)为spring控制的资源
5.表现层(App)通过spring获取资源(Service实例)
3.2.1)IoC入门案例制作步骤-1
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
3.2.2)IoC入门案例制作步骤-2
public interface UserService {
//业务方法
void save();
}
3.2.3)IoC入门案例制作步骤-3
public class UserServiceImpl implements UserService {
public void save() {
System.out.println("user service running...");
}
}
3.2.4)IoC入门案例制作步骤-4
<?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>
3.2.5)IoC入门案例制作步骤-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控制
-
格式:
<beans> <bean /> </beans>
-
基本属性:
<bean id="beanId" name="beanName1,beanName2" class="ClassName"></bean>
id:bean的名称,通过id值获取bean
class:bean的类型
name:bean的名称,可以通过name值获取bean,用于多人配合时给bean起别名
4.2)bean属性scope
-
名称:scope
-
类型:属性
-
归属:bean标签
-
作用:定义bean的作用范围
-
格式:
<bean scope="singleton"></bean>
-
取值:
- singleton:设定创建出的对象保存在spring容器中,是一个单例的对象
- prototype:设定创建出的对象保存在spring容器中,是一个非单例的对象
- request、session、application、 websocket :设定创建出的对象放置在web容器对应的位置
4.3)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方法将不会被执行
-
4.4)bean对象创建方式(了解)
(1)factory-bean
-
名称:factory-bean
-
类型:属性
-
归属:bean标签
-
作用:定义bean对象创建方式,使用静态工厂的形式创建bean,兼容早期遗留系统的升级工作
-
格式:
<bean class="FactoryClassName" factory-method="factoryMethodName"></bean>
-
取值:工厂bean中用于获取对象的静态方法名
-
注意事项:
-
class属性必须配置成静态工厂的类名
-
使用实例工厂创建bean首先需要将实例工厂配置bean,交由spring进行管理
-
factory-bean是实例工厂的beanId
-
框架反射执行方法,静态非静态没有区别
静态:封装好的一些方法,方便创建工具类的实例对象
- 对象单例,构造方法私有,new不了,提供getXXX方法
- 创建时需要加载一些配置文件,new需要加很多东西,把加载过程封装好了,直接都加载好了,直接返回了
第三方工具构造方法不能控制,调用某个类调用某个方法拿对象、
4.5)DI
-
IoC(Inversion Of Control)控制翻转,Spring反向控制应用程序所需要使用的外部资源
-
DI(Dependency Injection)依赖注入,应用程序运行依赖的资源由Spring为其提供,资源进入应用程序的方式称为注入
其实就是设置对象的属性!
4.6)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标签
4.7)构造器注入(了解)
-
名称:constructor-arg
-
类型:标签
-
归属:bean标签
-
作用:使用构造方法的形式为bean提供资源,兼容早期遗留系统的升级工作
-
格式:
<bean> <constructor-arg /> </bean>
-
基本属性:
<constructor-arg name="argsName" value="argsValue />
name:对应bean中的构造方法所携带的参数名
value:设定非引用类型构造方法参数对应的值,不能与ref同时使用
其他属性:
<constructor-arg index="arg-index" type="arg-type" ref="beanId"/>
ref:设定引用类型构造方法参数对应bean的id ,不能与value同时使用
type :设定构造方法参数的类型,用于按类型匹配参数或进行类型校验
index :设定构造方法参数的位置,用于按位置匹配参数,参数index值从0开始计数
- 注意:一个bean可以有多个constructor-arg标签
-
4.8)集合类型数据注入
-
名称:array,list,set,map,props
-
类型:标签
-
归属:property标签 或 constructor-arg标签
-
作用:注入集合数据类型属性
-
格式:
<property> <list></list> </property>
(1)集合类型数据注入——list
<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>
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:userDao-ref="userDao" p:bookDao-ref="bookDao" />
4.10)SpEL (了解)
-
Spring提供了对EL表达式的支持,统一属性注入格式
-
类型:属性值
-
归属:value属性值
-
作用:为bean注入属性值
-
格式:
<property value="EL"></bean>
-
注意:所有属性值不区分是否引用类型,统一使用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.准备外部properties文件
2.开启context命名空间支持
xmlns:context="http://www.springframework.org/schema/context"
3.加载指定的properties文件
<context:property-placeholder location="classpath:filename.properties">
4.使用加载的数据
<property name="propertyName" value="${propertiesName}"/>
-
注意:如果需要加载所有的properties文件,可以使用
*.properties
表示加载所有的properties文件 -
注意:读取数据使用**${propertiesName}格式进行,其中propertiesName**指properties文件中的属性名
4.12)团队开发
-
名称:import
-
类型:标签
-
归属:beans标签
-
作用:在当前配置文件中导入其他配置文件中的项
-
格式:
<beans> <import /> </beans>
-
基本属性:
<import resource=“config.xml"/>
resource:加载的配置文件名
-
Spring容器加载多个配置文件
new ClassPathXmlApplicationContext("config1.xml","config2.xml");
-
Spring容器中的bean定义冲突问题
-
同id的bean,后定义的覆盖先定义的
-
导入配置文件可以理解为将导入的配置文件复制粘贴到对应位置
-
导入配置文件的顺序与位置不同可能会导致最终程序运行结果不同
-
4.13)ApplicationContext
1.ApplicationContext是一个接口,提供了访问spring容器的API
2.ClassPathXmlApplicationContext是一个类,实现了上述功能
3.ApplicationContext的顶层接口是BeanFactory
4.BeanFactory定义了bean相关的最基本操作
5.ApplicationContext在BeanFactory基础上追加了若干新功能
对比BeanFactory
1.BeanFactory创建的bean采用延迟加载形式,使用才创建
2.ApplicationContext创建的bean默认采用立即加载形式
FileSystemXmlApplicationContext
可以加载文件系统中任意位置的配置文件,而ClassPathXmlApplicationContext只能加载类路径下的配置文件
BeanFactory
Resource res = new ClassPathResource("applicationContext.xml");
BeanFactory bf = new XmlBeanFactory(res);
UserService userService = (UserService)bf.getBean("userService");
4.14)第三方资源配置
-
阿里数据源方案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>
5)加载容器的简单代码和注意事项
优化
会扫描interface和xml文件。
配置后调用dao的层的mysql语句时,原Dao层的接口文件会默认生成首字母小写的接口名,比如StudentDao,加载后默认生成studentDao,用的时候读取studentDao就成了
6)综合案例 druid
包结构
pox.xml的依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.26</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://192.168.23.129/db3
jdbc.username=root
jdbc.password=root
SpringConfig.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:*.properties"/>
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSoure">
<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>
<bean class="service.Impl.StudentServiceImpl" id="studentService">
<property name="studentDao" ref="studentDao"/>
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSoure"/>
<property name="typeAliasesPackage" value="domain"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="dao"/>
</bean>
</beans>
public class UserServiceImpl implements UserService {
private Integer id;
private String name;
private Date birthday;
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void say() {
System.out.println("我是瓜皮");
}
@Override
public String toString() {
System.out.println(id+" "+name+" "+birthday);
return null;
}
}
public class UserApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("factory.xml");
UserService fact1 = (UserService)classPathXmlApplicationContext.getBean("haha");
fact1.say();
}
}
总结
spring入门第一天,主要学习思想(IoC AOP)和如何使用(配置文件 注释 API)
Ioc :Inverse of Control 控制反转
控制:控制的是Bean对象
反转:本来是new出来的,现在通过去spring容器创建后去要
就好比找女朋友,本来是new,自己去找,各种都亲力亲为,现在是通过婚姻介绍所,去匹配符合条件的对象,拿到后调用
属性:
- id唯一标记Bean对象
- class:告诉spring要管理的对象的全限定类名 com.ybb.类名
- scoper :单例还是多例 ,默认单例 singleton和prototype 由于单例应用中只有一个该对象,在加载配置文件时就创建了 。多例的情况,spring不知道创建多少,所以在用到的时候才会创建
- name:起别名
实例化
无参构造方法: 默认使用无参构造方法创建
工厂静态方法 :先创建工厂,再调用静态方法创建(一个标签中)
工厂非静态方法:先创建工厂,再实例化对象中调用factory-bean 绑定对应的工程,再调用方法创建
注入方式
set方式(属性提供setter方法)
构造方法
数组 list,[],set因为结构相同,可以归为一类,标签都可以互换使用
标签里套
map和props结构相同也可以互换
套value