文章目录
- IoC的概念和作用
- Spring中的IoC
- 1.导入依赖
- 2.创建配置文件&导入约束&配置bean
- 3.取出容器&获取对象
- tip: ApplicationContext的继承关系
- tip: ApplicationContext和BeanFactory的区别
- bean的细节
- 创建bean对象的三种方式
- 方式一:使用默认构造函数创建
- 方式二:使用普通工厂中的方法创建对象
- 方式三:使用工厂中的静态方法创建对象
- 指定bean对象的作用范围:bean标签的scope属性
- bean对象的生命周期
- Spring中的依赖注入(DI)
- 构造函数注入
- set方法注入
- 复杂类型注入
- 基于注解的IoC配置
- 用于创建对象的注解
- 用于注入数据的注解
- 用于改变作用范围的注解
- 生命周期相关的注解
IoC的概念和作用
上次工厂模式中创建对象有2种方式
// 方式1
IAccountService as = new AccountServiceImpl();
// 方式2
IAccountService as = (IAccountService) BeanFactory.getBean("accountService");
方式1是主动的;方式2是被动的(根据配置创建),控制权转移给了工厂
IoC (Inversion of Control 控制反转)把创建对象的权利交给框架
其作用是降低程序的耦合
但只能是降低,而不能完全消除。如果两个类之间任何关系都没有,那有一个类一定是多余的。
自己写时用工厂模式实现,Spring中的Ioc如何实现?
Spring中的IoC
控制反转 Inversion of Control, IoC
解决的问题:降低程序耦合(减少依赖关系)
解决的方法:使用配置的方式
1.导入依赖
导入Spring依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
在查看引入的依赖
包含了常用的核心组件
Core Container简单的说就是一个Map,封装了要用的对象
2.创建配置文件&导入约束&配置bean
然后创建bean.xml(没有固定要求,习惯这样命名,因为是管beans的)
在spring-framework-5.0.2.RELEASE-docs/spring-framework-reference文件夹中找到index.html
点core,搜索xmlns(导入xml schema约束的关键字)
把它复制粘贴到配置文件中
<?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
http://www.springframework.org/schema/beans/spring-beans.xsd">
</beans>
配置bean,把对象的创建交给Spring来管理
同工厂模式,需要唯一标志和对象的全限定类名(包名+类名)
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"></bean>
剩下的解析配置文件的事就由Spring做了
3.取出容器&获取对象
// Client.java
public class Client {
public static void main(String[] args) {
// 1.获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// 2.根据id获取Bean对象
// 方法一:拿到一个Object类,自己强转
IAccountService as = (IAccountService)ac.getBean("accountService");
// 方法二:给一个类型,直接得到该类型对象
IAccountDao adao = ac.getBean("accountDao",IAccountDao.class);
}
}
tip: ApplicationContext的继承关系
Idea中,在类上右键->Diagrams->Show Diagram
在接口上右键->Show Implementations
可以看到,ApplicationContext继承自BeanFactory,ClassPathXmlApplicationContext是它的实现类
ApplicationContext的三个常用实现类:
* ClassPathXmlApplicationContext:它可以加载类路径下的配置文件(配置文件必须在类路径下,不在的加载不了)(更常用)
* FileSystemXmlApplicationContext:它可以加载磁盘任意路径下的配置文件(必须有访问权限)
* AnnotationConfigApplicationContext:它是用于读取注解创建容器的(后面讲)
tip: ApplicationContext和BeanFactory的区别
ApplicationContext
- 构建核心容器时,创建对象采取的策略是采用立即加载的方式。即,一读取完配置文件马上就创建配置文件中配置的对象
- 单例对象适用
BeanFactory
- 构建核心容器时,创建对象采取的策略是采用延迟加载的方式。即,什么时候根据id获取对象了,什么时候才真正的创建对象
- 多例对象使用
bean的细节
创建bean对象的三种方式
方式一:使用默认构造函数创建
在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
方式二:使用普通工厂中的方法创建对象
实际开发中,可能要用到别人写好的jar包中的类。(不知道有没有默认构造函数,也无法通过修改源码来提供默认构造函数)。例如:
// 模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数)
public class InstanceFactory {
public IAccountService getAccountService(){
return new AccountServiceImpl();
}
}
有这么一个InstanceFactory类,需要拿到AccountServiceImpl
如果像方法一那样创建对象,即
<bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
得到的并不是AccountServiceImpl对象,而是InstanceFactory对象
此时应该修改创建对象的方式
使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)
<bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
先创建工厂,再通过工厂的方法创建所需对象
方式三:使用工厂中的静态方法创建对象
模拟一个工厂类,它有一个返回对象的静态方法
// 模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式来提供默认构造函数)
public class StaticFactory {
public static IAccountService getAccountService(){
return new AccountServiceImpl();
}
}
使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器)
<bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>
这里的factory-method是一个静态方法
Q:这两个工厂中不都new对象了吗?
A:把这两个对象看作jar包中的类(此处只是模拟它),jar包中不是.java文件而是.class,都是无法修改的。实际开发中,有些对象就得用方法二或方法三来创建
指定bean对象的作用范围:bean标签的scope属性
取值:
singleton:单例的(默认值)(常用)
prototype:多例的(常用)
request:作用于web应用的请求范围
session:作用于web应用的会话范围
global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session
例子:
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="prototype"></bean>
global-session的含义
bean对象的生命周期
单例对象
- 出生:当容器创建时对象出生
- 活着:只要容器还在,对象一直活着
- 死亡:容器销毁,对象消亡
总结:单例对象的生命周期和容器相同
多例对象
- 出生:当我们使用对象时spring框架为我们创建
- 活着:对象只要是在使用过程中就一直活着。
- 死亡:当对象长时间不用,且没有别的对象引用时,由Java的垃圾回收器回收
可以通过bean标签指定在特定生命周期执行的方法
例子:
// beans.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
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
scope="singleton" init-method="init" destroy-method="destroy"></bean>
</beans>
// Client.java
public class Client {
public static void main(String[] args) {
// 1.获取核心容器对象
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); // 执行完这句,单例对象就创建了
// 2.根据id获取Bean对象
IAccountService as = (IAccountService)ac.getBean("accountService"); // 执行完这句,多例对象才创建
as.saveAccount();
}
}
// AccountServiceImpl.java
public class AccountServiceImpl implements IAccountService {
public AccountServiceImpl(){
System.out.println("对象创建了...");
}
public void saveAccount(){
System.out.println("service中的saveAccount方法执行了...");
}
public void init(){
System.out.println("对象初始化了...");
}
public void destroy(){
System.out.println("对象销毁了...");
}
}
执行输出
对象创建了…
对象初始化了…
service中的saveAccount方法执行了…
Q:为什么没有执行销毁方法?
A:main()结束之后,当前进程占用的内存全部释放(包括容器),此时并没有调用销毁方法就已经把内存释放了。这里要想调用销毁方法,可以手动关闭容器
在main()中手动关闭容器
为什么没有呢?
这里ApplicationContext是个接口,调不到子类的方法
// 1.获取核心容器对象
// ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
// 2.根据id获取Bean对象
IAccountService as = (IAccountService)ac.getBean("accountService");
as.saveAccount();
// 3.手动关闭容器
ac.close();
这样就可以了
对象创建了…
对象初始化了…
service中的saveAccount方法执行了…
对象销毁了…
但是,此时如果把对象改为多例的,其它都不变,不会执行销毁方法
Spring中的依赖注入(DI)
依赖注入 Dependency Injection, DI
能注入的三类数据
- 基本类型和String
- 其他bean类型(在配置文件中或者注解配置过的bean)
- 复杂类型/集合类型
注入的三种方式
- 使用构造函数提供
- 使用set方法提供
- 使用注解提供
构造函数注入
使用的标签:constructor-arg
标签出现的位置:bean标签的内部
标签中的属性(指定参数):
- type:指定数据类型
- index:指定构造函数中的索引位置
- name:指定名称(常用)
标签中的属性(提供赋值):
- value:用于提供基本类型和String类型的数据
- ref:用于指定其他的bean类型数据(在spring的Ioc核心容器中出现过的bean对象)
优势:
在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功(必须某些数据时,不会被忽略)
弊端:
改变了bean对象的实例化方式,创建对象时,即使以后用不到这些数据,也必须提供
例:
AccountServiceImpl类有3个变量,没有无参构造函数
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.xml对应的bean配置
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<constructor-arg name="name" value="test"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<!-- 配置一个日期对象 -->
<bean id="now" class="java.util.Date"></bean>
配置中的value都是字符串,对于String、Integer类型,Spring可以自动转换
但Date无法直接转换,需另外配置
set方法注入
比构造函数常用
使用的标签:property
出现的位置:bean标签的内部
标签的属性:
- name:用于指定注入时所调用的set方法名称(不管变量名)
- value:用于提供基本类型和String类型的数据
- ref:用于指定其他的bean类型数据
优势:
创建对象时没有明确的限制,可以直接使用默认构造函数
弊端:
如果有某个成员必须有值,但没有注入
例:
public class AccountServiceImpl2 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="accountService2" class="com.itheima.service.impl.AccountServiceImpl2">
<property name="name" value="TEST" ></property>
<property name="age" value="21"></property>
<property name="birthday" ref="now"></property>
</bean>
复杂类型注入
用于给List结构集合注入的标签:
list array set
用于个Map结构集合注入的标签:
map props
! 结构相同,标签可以互换
例:
public class AccountServiceImpl3 implements IAccountService {
// list结构
private String[] myStrs;
private List<String> myList;
private Set<String> mySet;
// map结构
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="accountService3" class="com.itheima.service.impl.AccountServiceImpl3">
<!--用array、list、set都行-->
<property name="myStrs">
<array>
<value>AAA</value>
<value>BBB</value>
<value>CCC</value>
</array>
</property>
<!--用map、prop都行-->
<property name="myMap">
<map>
<entry key="testA" value="aaa"></entry>
<entry key="testB">
<value>BBB</value>
</entry>
</map>
</property>
</bean>
基于注解的IoC配置
注解配置和xml配置要实现的功能都是一样的:降低程序耦合
曾经XML的配置:
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
scope="" init-method="" destroy-method="">
<property name="" value="" | ref=""></property>
</bean>
使用注解时,不用上面的,但需要告诉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"
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">
<!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为
context名称空间和约束中-->
<context:component-scan base-package="com.itheima"></context:component-scan>
</beans>
其中context名称空间和前面一样,在文档中搜索找
用于创建对象的注解
@Component
作用:
用于把当前类对象存入spring容器中
属性:
value:用于指定bean的id(不写时,默认值是当前类名,且首字母小写)
例:
@Component(value = "accountService")
public class AccountServiceImpl implements IAccountService {
//...
}
细节:
当只给一个value属性赋值时,value可以不写
@Component("accountService")
public class AccountServiceImpl implements IAccountService {
//...
}
@Controller:一般用在表现层
@Service:一般用在业务层
@Repository:一般用在持久层
以上三个注解的作用和属性与@Component一模一样
是Spring框架提供的明确的三层使用的注解,使三层对象更加清晰
用于注入数据的注解
@Autowired
作用:
自动按照类型注入。只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功。如果ioc容器中没有任何bean的类型和要注入的变量类型匹配,则报错
(当有多个bean类型匹配时,用变量名和bean的id匹配,如果恰好有一个匹配,也可以成功,否则报错)
出现位置:
可以是变量上,也可以是方法上
例:
@Autowired
private IAccountDao accountDao = null;
细节:
在使用注解注入时,set方法就不是必须的了
@Qualifier
作用:
在按照类中注入的基础之上再按照名称注入。在给类成员注入时不能单独使用,必须和@Autowired一起。但是在给方法参数注入时可以单独使用
属性:
value:用于指定注入bean的id
例:
@Autowired
@Qualifier("accountDao1")
private IAccountDao accountDao = null;
@Resource
作用:
直接按照bean的id注入,可以独立使用
属性:
name:用于指定bean的id
(注意:这里是name,不是value)
例:
@Resource(name = "accountDao1")
private IAccountDao accountDao = null;
以上三个注解都只能注入其他bean类型的数据,而基本类型和String类型无法注入
另外,集合类型的注入只能通过XML来实现
@Value
作用:
用于注入基本类型和String类型的数据
属性:
value:用于指定数据的值
可以使用Spring中SpEL(即Spring的el表达式)写法:${表达式}
用于改变作用范围的注解
@Scope
作用:
用于指定bean的作用范围
属性:
value:指定范围的取值(常用:singleton prototype)
例:
@Service("accountService")
@Scope("prototype")
public class AccountServiceImpl implements IAccountService {
//...
}
生命周期相关的注解
@PreDestroy
作用:用于指定销毁方法
@PostConstruct
作用:用于指定初始化方法
和bean标签中使用init-method和destroy-methode一样
例:
@Service("accountService")
@Scope("singleton")
public class AccountServiceImpl implements IAccountService {
@Resource(name = "accountDao1")
private IAccountDao accountDao = null;
@PostConstruct
public void init(){
System.out.println("初始化方法执行了");
}
@PreDestroy
public void destroy(){
System.out.println("销毁方法执行了");
}
public void saveAccount(){
accountDao.saveAccount();
}
}
同样,如果类是多例的,可能不会看到销毁方法执行的输出,因为它的销毁是JVM管的