IOC
IOC:inversion of control 控制反转。作用是降低程序间的耦合(依赖关系)
IOC核心容器:容器存储着创建出来的对象,需要的时候再从容器中取出即可。spring容器是Map结构,由key-value组成
IOC核心容器的两个接口
配置文件bean.xml,在resource目录下
<?xmlversion="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 把对象的创建交给spring来管理-->
<beanid="accountService"class="cn.upeveryday.service.impl.AccountServiceImpl"></bean>
<beanid="accountDao"class="cn.upeveryday.dao.impl.AccountDaoImpl"></bean>
</beans>
BeanFactory
在构建核心容器时,创建对象采取的策略是延迟加载的方式。也就是说,什么时候根据id获取对象,什么时候才真正的创建对象。
延迟加载适用于多例模式
ApplicationContext(常用)
在构建核心容器时,创建对象采取的策略是立即加载的方式。也就是说,只要一读取完配置文件就马上创建配置文件中配置的对象。
立即加载适用于单例对象
ApplicationContext是BeanFactory的子接口,功能比BeanFactory更强大,因此开发中常用ApplicationContext接口。
spring框架很智能,能够根据是单例对象还是多例对象,来自动设置加载方式是立即加载还是延迟加载
获取sring的IOC核心容器
ApplicationContext是容器的接口,有三个常用实现类:
- ClassPathXmlApplicationContext:可以加载类路径下的配置文件来创建容器,要求配置文件必须在类路径下;不在,则无法加载
- FileSystemXmlApplicationContext:可以加载磁盘任意路径下的配置文件来创建容器(要有访问权限)
- AnnotationConfigApplicationContext:读取注解来创建容器
类路径(classpath):在编译打包后的项目中,根目录是
META-INF
和WEB-INF
,WEB-INF/classes
就是类路径,包含src下的所有文件
//1.获取核心容器对象
ApplicationContextac=newClassPathXmlApplicationContext("bean.xml");
//2.根据id获取bean对象
IAccountServiceservice= (IAccountService) ac.getBean("accountService");
IAccountDaodao=ac.getBean("accountDao",IAccountDao.class);
spring对bean的管理
bean
Bean:在计算机英语中,是可重用组件的意思
可重用组件:业务层、持久层都是可重用组件(表现层调用业务层,业务层调用持久层)
javabean:用java语言编写的可重用组件(javabean > 实体类)
IOC核心容器就是用来创建bean对象(service对象和dao对象)的:需要一个配置文件来配置我们的service和dao(spring中的配置文件是xml)
创建bean对象的三种方式
//1.获取核心容器对象
ApplicationContextac=newClassPathXmlApplicationContext("bean.xml");
//2.根据id获取bean对象
IAccountServiceservice= (IAccountService) ac.getBean("accountService");
1. 使用默认构造函数创建对象
读取全限定类名,通过反射创建一个bean对象,保存在spring容器中
<beanid="accountService"class="cn.upeveryday.service.impl.AccountServiceImpl"></bean>
在spring的配置文件中使用bean标签,配以id和class属性后,且没有其他属性和标签时,采用的就是调用默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建
2. 使用工厂中的方法创建对象
使用某个类中的方法创建对象,并存入spring容器
/**
* 模拟一个工厂类(该类可能是存在与jar包中的,我们无法通过修改源码的方式来提供默认构造函数)
*/
publicclassInstanceFactory {
publicIAccountServicegetAccountService(){
returnnewAccountServiceImpl();
}
}
<beanid="instanceFactory"class="cn.upeveryday.factory.InstanceFactory"></bean>
<beanid="accountService"factory-bean="instanceFactory"factory-method="getAccountService"></bean>
这个时候,我们再从容器中获取accountService
对象时,是从factory-bean
中的factory-method
方法获取的
3. 使用工厂中的静态方法创建对象
使用某个类中的静态方法创建对象并存入spring容器中
/**
* 模拟一个工厂类(该类可能是存在与jar包中的,我们无法通过修改源码的方式来提供默认构造函数)
*/
publicclassStaticFactory {
publicstaticIAccountServicegetAccountService(){
returnnewAccountServiceImpl();
}
}
<beanid="accountService"class="cn.upeveryday.factory.StaticFactory"factory-method="getAccountService"></bean>
bean对象的作用范围
使用bean标签的scope属性指定bean对象的作用范围(常用singleton和prototype)
- singleton:单例的(默认值)
- prototype:多例的
- request:作用于web应用的请求范围
- session:作用于web应用的会话范围
- global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,他就是session
bean对象的生命周期
spring框架很智能,能够根据是单例对象还是多例对象,来自动设置加载方式是立即加载还是延迟加载
单例对象
出生:当容器创建时,对象出生(立即加载)
活着:只要容器存在,对象一直活着
死亡:容器销毁,对象死亡
单例对象的生命周期和容器一样
多例对象
出生:当使用对象时,spring框架为我们创建(延迟加载)
活着:对象只要只在使用过程中就一直活着
死亡:当对象长时间不用,且没有别的对象引用时,由java的垃圾回收器回收
spring的依赖注入
如果一个类A 的功能实现需要借助于类B,那么就称类B是类A的依赖,如果在类A的内部去实例化类B,那么两者之间会出现较高的耦合,一旦类B出现了问题, 类A也需要进行改造,如果这样的情况较多,每个类之间都有很多依赖,那么就会出现牵一发而动全身的情况,程序会极难维护,并且很容易出现问题。
要解决这个问题,就要把A类对B类的控制权抽离出来,交给一个第三方去做,把控制权反转给第三方,就称作控制反转(IOC Inversion Of Control)。控制反转是一种思想,是能够解决问题的一种可能的结果,而依赖注入(Dependency Injection)就是其最典型的实现方法。由第三方(我们称作IOC容器)来控制依赖,把他通过构造函数、属性或者工厂模式等方法,注入到类A内,这样就极大程度的对类A和类B进行了解耦。
IOC作用是降低程序间的耦合(依赖关系),依赖关系的管理都交给spring来维护:在当前类中需要用到其他类的对象,只需要在配置文件中说明,由spring为我们提供
依赖注入:我对对象的依赖是注入进来的。本来接收各种参数来构造一个对象,现在只接受一个参数——已经实例化的对象。
依赖关系的维护就称之为:依赖注入DI(Dependency Injection)
能注入的类型
基本类型和String
其他bean类型(在配置文件中或者注解配置过的bean)
复杂类型/集合类型
依赖注入的方式
将本类对象中依赖的其他对象注入
1. 使用构造函数注入
使用的标签:constructor-arg
标签出现的位置:bean标签的内部
标签中的属性:
- type:给构造函数中类型为type的参数赋值
- index:给构造函数中索引为index的参数赋值。index从0开始
- name:给构造函数中名称为name的参数赋值(常用)
--------------------以上三个用于选择给构造函数中哪个参数赋值,以下两个用于给参数赋什么值------------------------ - value:用于提供基本类型和String类型的数据
- ref:用于指定其他的bean类型数据。它指的就是在spring的IOC核心容器中出现过的bean对象
优势
一个类只提供了有参构造而没有无参构造,在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功
弊端
改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供
package cn.upeveryday.service.impl;
public class AccountServiceImpl implements IAccountService {
private String name;
private Integer age;
private Date birthday;
//构造方法
public AccountServiceImpl(String name, Integer age, Date birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
}
<bean id="accountService" class="cn.upeveryday.service.impl.AccountServiceImpl">
<constructor-arg name="name" value="ggbond"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<!-- 配置一个日期对象,调用id即可从容器中获取对象-->
<bean id="now" class="java.util.Date"></bean>
2. 使用set方法注入(常用)
使用的标签:property
标签出现的位置:bean标签的内部
标签中的属性:
- name:用于指定注入时所调用的set方法名称
--------------------以上一个用于选择给构造函数中哪个参数赋值,以下两个用于给参数赋什么值------------------------ - value:用于提供基本类型和String类型的数据
- ref:用于指定其他的bean类型数据。它指的就是在spring的IOC核心容器中出现过的bean对象
优势
创建对象时没有明确的限制,可以直接使用默认构造函数
弊端
如果有某个成员必须有值,则获取对象时有可能set方法没执行
基本类型/String/其他bean类型
package cn.upeveryday.service.impl;
public class AccountServiceImpl implements IAccountService {
private String name;
private Integer age;
private Date birthday;
public void setName(String name) {
this.name = name;
}
public void setAge(Integer age) {
this.age = age;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
<bean id="accountService" class="cn.upeveryday.service.impl.AccountServiceImpl">
<property name="name" value="ggbond"></property>
<property name="age" value="18"></property>
<property name="birthday" ref="now"></property>
</bean>
<!-- 配置一个日期对象-->
<bean id="now" class="java.util.Date"></bean>
复杂类型/集合类型
用于给List结构集合注入的标签:
list array set
用于给Map结构集合注入的标签:
map props
结构相同,标签可以互换
package cn.upeveryday.service.impl;
public class AccountServiceImpl implements IAccountService {
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
private Map<String,String> myMap;
private Properties myProps;
public void setMyStrs(String[] myStrs) {
this.myStrs = myStrs;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
public void setMySet(Set<String> mySet) {
this.mySet = mySet;
}
public void setMyMap(Map<String, String> myMap) {
this.myMap = myMap;
}
public void setMyProps(Properties myProps) {
this.myProps = myProps;
}
}
<!-- 复杂类型/集合类型的注入-->
<bean id="accountService" class="cn.upeveryday.service.impl.AccountServiceImpl">
<property name="myStrs">
<array>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</array>
</property>
<property name="myList">
<list>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</list>
</property>
<property name="mySet">
<set>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</set>
</property>
<property name="myMap">
<map>
<entry key="01" value="aaa"></entry>
<entry key="02" value="bbb"></entry>
<entry key="03" value="ccc"></entry>
</map>
</property>
<property name="myProps">
<props>
<prop key="01">aaa</prop>
<prop key="02">bbb</prop>
<prop key="03">ccc</prop>
</props>
</property>
</bean>
3. 使用注解注入
注解
在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
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
使用context:component-scan
标签中的base-package
属性表明spring在创建容器时要扫描的包
<context:component-scan base-package="cn.upeveryday"></context:component-scan>
1. 用于创建bean对象
和在xml配置文件中使用<bean>
标签作用一样
Component
作用:把当前类对象存入spring容器中
属性value:用于指定bean的id。默认值是当前类名,且首字母改小写
作用在类上面,一般用于不属于三层中任意一层的类
@Component("logger")
public class Logger {...}
Controller(表现层)
一般用在表现层
Service(业务层)
一般用在业务层
@Service("accountService")
public class AccountServiceImpl implements IAccountService {...}
Repository(持久层)
一般用在持久层
以上三个注解的作用和属性与Component一样
是spring框架提供的分别作用于三层架构的不同层级的注解,使我们的三层对象更加清晰
2. 用于注入数据
和在xml配置文件中使用<bean>
标签中写一个<property>
标签作用一样
Autowired
spring容器中的bean对象是以Map键值对的形式存在(key-value形式)
自动按照类型注入时先跟value值(class)匹配,再跟key值(id)匹配
作用:自动按照类型注入。
- 当容器中有唯一的一个bean对象的类型(class值)和要注入的变量类型匹配,可以注入成功
- 当容器中没有bean对象的类型(class值)和要注入的变量类型匹配,则注入失败
- 当容器中有多个bean对象的类型(class值)和要注入的变量类型匹配,则使用要注入变量的名称跟key值(id值)进行比较,若存在相同,则注入成功,否则注入失败
出现位置:可以是变量上,也可以是方法上
细节:使用注解注入时,无需set方法
Qualifier
作用:在按照类注入的基础上再按照名称注入。他给类成员注入时不能单独使用(依托于Autowired注解),但是在给方法参数注入时可以单独使用
属性:
- value:用于指定注入bean的id
//Qualifier给类成员注入时,不能单独使用,要搭配Autowired使用
//spring容器中存在真实对象和代理对象,class一样,id值不一样,在Qualifier中指定id值
@Autowired
@Qualifier("proxyAccountService")
private IAccountService accountService;
Resource
作用:直接按照bean的id注入。可以独立使用
属性:
- name:用于指定bean的id
以上三个注入都只能注入其他bean类型的数据,而基本类型和String类型无法使用上述注解实现;且集合类型的注入只能通过xml来实现
Value
作用:用于注入基本类型和String类型的数据
属性:
- value:用于指定数据的值。它可以使用spring中的SpEL(也就是spring的EL表达式:
${ 表达式 }
)
用来注入yaml文件中的属性
3. 用于改变作用范围
和在xml配置文件中使用<bean>
标签中使用scope属性作用一样
Scope
作用:用于指定bean的作用范围
属性:
- value:指定范围的取值。常用取值:singleton(默认单例)、prototype
4. 和生命周期相关(了解即可)
和在xml配置文件中使用<bean>
标签中使用init-method
和destroy-method
属性作用一样
PreDestroy
作用:用于指定销毁方法
PostConstrut
作用:用于指定初始化方法
5. 用于替代XML配置文件
Configuration
作用:指定当前类是一个配置类
注意:当配置类作为AnnotationConfigApplicationContext对象创建的参数时,该注解可以不写
ApplicationContext ac=new AnnotationConfigApplicationContext(SpringConfiguration.class);
ComponentScan
作用:通过此注解指定spring在创建容器时要扫描的包
属性:
- value:它和basePackages作用一样,都是用于指定创建容器时要扫描的包。和下面xml中的语句作用一样
<context:component-scan base-package="cn.upeveryday"></context:component-scan>
Bean
作用:把当前方法的返回值作为bean对象存入spring的IOC容器中
属性:
- name:用于指定bean的id。默认是当前方法名
注意:当我们使用注解配置方法,如果方法有参数,spring容器会去容器中查找有没有可用的bean对象。查找方式和Autowired注解的作用是一样的
Import
作用:导入其他的配置类
属性:
- value:用于指定其他配置类的字节码。当我们使用Import的注解之后,有Import注解的类就是父配置类,而导入的都是子配置类
PropertySource
作用:用于指定properties文件的位置
属性:
- value:指定文件的名称和路径
关键字classpath:表示类路径下@PropertySource("classpath:jdbcConfig.properties")
6. 用于事务管理
@Transactional
事务管理是应用系统开发中必不可少的一部分。Spring 为事务管理提供了丰富的功能支持。Spring 事务管理分为编程式和声明式的两种方式。
- 编程式事务指的是通过编码方式实现事务
- 声明式事务基于 AOP,将具体业务逻辑与事务处理解耦,声明式事务管理使业务代码逻辑不受污染, 因此在实际使用中声明式事务用的比较多。
声明式事务有两种方式,一种是在配置文件(xml)中做相关的事务规则声明,另一种是基于@Transactional
注解的方式。
需要明确几点:
- 默认配置下 Spring 只会回滚运行时、未检查异常(继承自 RuntimeException 的异常)或者 Error。
@Transactional
注解只能应用到 public 方法才有效
以下的示例使用的是 mybatis,所以 spring boot 会自动配置一个 DataSourceTransactionManager
,我们只需在方法(或者类)加上 @Transactional 注解,就自动纳入 Spring 的事务管理了。
简单的使用方法
只需在方法加上 @Transactional
注解就可以了。
如下有一个保存用户的方法,加入 @Transactional
注解,使用默认配置,抛出异常之后,事务会自动回滚,数据不会插入到数据库。
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserDAO userDAO;
@Override
public void register(User user) {
user.setId(UUID.randomUUID().toString());
userDAO.save(user);
}
}
spring整合Junit
Junit
- 应用程序的入口:main方法
- Junit单元测试中没有main方法也能执行,因为Junit集成了一个main方法,该方法会自动判断当前测试类中有哪些方法有
@Test
注解,Junit就会让有Test注解的方法执行 - Junit测试时不会管我们是否使用了spring框架,也就不会为我们读取配置文件/配置类创建spring核心容器
综上所述,当测试方法执行时,没有IOC容器,就算写了Autowired注解,也无法实现注入
spring整合Junit的配置
- 首先导入spring整合Junit的jar(坐标)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.6</version>
</dependency>
- 使用spring提供的一个注解把原有的main方法替换了
@RunWith(SpringJUnit4ClassRunner.class)
- 告知spring的运行器,spring和IOC创建时基于xml还是注解的,并且说明位置
@ContextConfiguration
location:指定xml文件的位置,加上classpath关键字,表示在类路径下
classes:指定注解类所在的路径
@ContextConfiguration(classes = SpringConfiguration.class)
- 当我们使用spring 5.x版本的时候,要求Junit的jar必须是4.12及以上
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:bean.xml")
public class AccountServiceTest {
@Autowired
private IAccountService as;
}