SpringIOC核心技术:控制反转、依赖注入
把创建对象的权利交给spring管理 、把当前类需要用到其他类的依赖交给spring管理,说白了就是通过工厂反射帮你实例化对象、并且帮你给成员变量赋值的技术。
控制反转原理
我们制作一个工厂:输入Bean的全类名,然后返回它的实例化对象
首先创建一个配置文件写上如下配置
accountService = com.itheima.service.impl.AccountServiceImpl
accountDao = com.itheima.dao.impl.AccountDaoImpl
这个我们后面会讲到,这里先扩展
配置文件的key就相当于spring配置文件中 bean标签里的id(唯一标识符)属性的值 后面的路径就相当于class属性的值,然后spring自己吧class属性的值,通过反射给你实例化一个对象,就相当于比如说你告诉它accountService,然后它帮你new accountService 对象一样。
代码:
/**
* 一个创建Bean对象的工厂
*
* Bean:在计算机英语中,有可重用组件的含义。
* JavaBean:用java语言编写的可重用组件。
* javabean > 实体类
*
* 它就是创建我们的service和dao对象的。
*
* 第一个:需要一个配置文件来配置我们的service和dao
* 配置的内容:唯一标识=全限定类名(key=value)
* 第二个:通过读取配置文件中配置的内容,反射创建对象
*
* 我的配置文件可以是xml也可以是properties
*/
public class BeanFactory {
//定义一个Properties对象
private static Properties props;
//定义一个Map,用于存放我们要创建的对象。我们把它称之为容器 键是Bean全类名 值是Bean对象
private static Map<String,Object> beans;
//使用静态代码块为Properties对象赋值
static {
try {
//实例化对象
props = new Properties();
//获取properties文件的流对象
InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
props.load(in);
//实例化容器
beans = new HashMap<String,Object>();
//取出配置文件中所有的Key
Enumeration keys = props.keys();
//遍历枚举
while (keys.hasMoreElements()){
//取出每个Key
String key = keys.nextElement().toString();
System.out.println("key="+key); //key=accountDao 相当于 id
//根据key获取value
String beanPath = props.getProperty(key);
//反射创建对象
Object value = Class.forName(beanPath).newInstance();
System.out.println("value="+value);// value=com.itheima.dao.impl.AccountDaoImpl@4554617c 相当于 class
//把key和value存入容器中
beans.put(key,value);
}
}catch(Exception e){
throw new ExceptionInInitializerError("初始化properties失败!");
}
}
/**
* 根据bean的名称获取对象
* @param beanName
* @return
*/
public static Object getBean(String beanName){
return beans.get(beanName);
}
}
一、 IOC控制反转(基于在配置文件里配置bean)
以上代码将被Spring 封装好了,我们写一段代码直接进行调用:
1.获取核心容器对象
2.根据id获取Bean对象
public static void main(String[] args) {
//1.获取核心容器对象
//②方式一
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//②方式二
BeanFactory ac = new ClassPathXmlApplicationContext("bean.xml");
//2.根据id获取Bean对象
//获得Bean方式一
IAccountService as = (IAccountService)ac.getBean("accountService");
//获得Bean方式二
IAccountDao adao = ac.getBean("accountDao",IAccountDao.class);(id,id实现类.class)
System.out.println(as);
System.out.println(adao);
as.saveAccount();
//--------------------------------------------------
但是配置文件产生了如下变化:
<!--bean.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">
<!--把对象的创建交给spring来管理-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
<bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"></bean>
</beans>
}
获取核心容器对象的ApplicationContext 细节:
/*获取spring的Ioc核心容器,并根据id获取对象
ApplicationContext的三个常用实现类:
ClassPathXmlApplicationContext:它可以加载类路径下的配置文件,要求配置文件必须在类路径下。不在的话,加载不了。(更常用)
FileSystemXmlApplicationContext:它可以加载磁盘任意路径下的配置文件(必须有访问权限)
AnnotationConfigApplicationContext:它是用于读取注解创建容器的,
核心容器的两个接口引发出的问题:
ApplicationContext: 单例对象适用 一次读取 一次创建
它在构建核心容器时,创建对象采取的策略是采用立即加载的方式。也就是说,只要一读取完配置文件马上就创建配置文件中配置的对象。
BeanFactory: 多例对象使用 啥时需要 啥时创建
它在构建核心容器时,创建对象采取的策略是采用延迟加载的方式。也就是说,什么时候根据id获取对象了,什么时候才真正的创建对象。*/
spring对bean的管理细节(基于xml)
我们知道,实例一个bean 有多种方式比如通过构造器 ,直接把这个类交给IOC就可以了,但是比如在一个类的方式里直接返回bean的实例,那么我们不能把这个类交给IOC管理吧? IOC将实例的是这个类,这并不是我们想要的,接下来是spring对bean的管理细节:
<?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">
<!--把对象的创建交给spring来管理-->
<!--spring对bean的管理细节
1.创建bean的三种方式
2.bean对象的作用范围
3.bean对象的生命周期
-->
<!--创建Bean的三种方式 -->
<!-- 第一种方式:使用默认构造函数创建。
在spring的配置文件中使用bean标签,配以id和class属性之后,且没有其他属性和标签时。
采用的就是默认构造函数创建bean对象,此时如果类中没有默认构造函数,则对象无法创建。
-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>
<!-- 第二种方式: 使用普通工厂中的方法创建对象(使用某个类中的方法创建对象,并存入spring容器)属性:factory-bean 表示工厂类名称 factory-method 哪个方法返回实例就写哪个 -->
<!-- 这是我们要实例的工厂对象代码
public class InstanceFactory {
public IAccountService getAccountService(){
return new AccountServiceImpl();
}
}
-->
<bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>
<!-- 第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器) -->
<!--这是我们要实例的工厂对象代码
public class StaticFactory {
public static IAccountService getAccountService(){
return new AccountServiceImpl();
}
}
-->
<bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>
<!-- bean的作用范围调整
bean标签的scope属性:
作用:用于指定bean的作用范围
取值: 常用的就是单例的和多例的
singleton:单例的(默认值)
prototype:多例的
request:作用于web应用的请求范围
session:作用于web应用的会话范围
global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,它就是session-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="prototype"></bean>
<!-- bean对象的生命周期
单例对象
出生:当容器创建时对象出生
活着:只要容器还在,对象一直活着
死亡:容器销毁,对象消亡
总结:单例对象的生命周期和容器相同
多例对象
出生:当我们使用对象时spring框架为我们创建,就是懒加载。
活着:对象只要是在使用过程中就一直活着。
死亡:当对象长时间不用,且没有别的对象引用时,由Java的垃圾回收器回收
-->
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
scope="prototype" init-method="init" destroy-method="destroy"></bean>
</beans>
依赖注入(DI)
好了,历经了千辛万苦我们终于拿到bean了,那么我们该如何给bean的成员变量赋值等一些操作呢?接下来将讲到依赖注入:
在当前类需要用到其他类的对象,由spring为我们提供,我们只需要在配置文件中说明
依赖关系的维护:
就称之为依赖注入。
依赖注入:
能注入的数据:有三类
①基本类型和String
②其他bean类型(在配置文件中或者注解配置过的bean)
③复杂类型/集合类型
注入的方式:有三种
第一种:使用构造函数提供
第二种:使用set方法提供
第三种:使用注解提供(明天的内容)
构造函数注入
接下来说的是 第一种:使用构造函数提供
<?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">
<!--构造函数注入:
使用的标签:constructor-arg
标签出现的位置:bean标签的内部
标签中的属性:
type:用于指定要注入的数据的数据类型,该数据类型也是构造函数中某个或某些参数的类型
index:用于指定要注入的数据给构造函数中指定索引位置的参数赋值。索引的位置是从0开始
name:用于指定给构造函数中指定名称的参数赋值 常用的
=============以上三个用于指定给构造函数中哪个参数赋值===============================
value:用于提供基本类型和String类型的数据
ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象
优势:
在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。
弊端:
改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供。
-->
<!--这是我们将要实例化的Bean的java代码 -->
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;
}
public void saveAccount(){
System.out.println("service中的saveAccount方法执行了。。。"+name+","+age+","+birthday);
}
}
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
<constructor-arg name="name" value="泰斯特"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
//成员变量用到了其它类比如说是date类 无法直接给value赋值 2019-7-15 ,需要使用ref属性引入其他类
<constructor-arg name="birthday" ref="now"></constructor-arg>
</bean>
<!-- 配置一个日期对象 -->
<bean id="now" class="java.util.Date"></bean>
set方法注入( 更常用的方式)
<!--我们要注入的bean 提供了set方法-->
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;
}
public void saveAccount(){
System.out.println("service中的saveAccount方法执行了。。。"+name+","+age+","+birthday);
}
}
<!-- set方法注入
涉及的标签:property
出现的位置:bean标签的内部
标签的属性
name:用于指定注入时所调用的set方法名称
value:用于提供基本类型和String类型的数据
ref:用于指定其他的bean类型数据。它指的就是在spring的Ioc核心容器中出现过的bean对象
优势:
创建对象时没有明确的限制,可以直接使用默认构造函数
弊端:
如果有某个成员必须有值,则获取对象是有可能set方法没有执行。
-->
<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>
其他一些复杂类型的注入,也是上述两种方式选其中一种,由于篇幅有限,我们另外开一篇帖子介绍。
总结:基于xml的形式创建bean,知道各个属性的用法即可。
<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"
scope="" init-method="" destroy-method="">
<property name="" value="" | ref=""></property>
</bean>
二、 基于注解形式创建bean
以上的依赖注入和控制反转都是基于xml的形式来编写的,接下来我们将介绍基于注解的形式
要使用基于注解的形式 ,首先要在spring.xml中添加扫描包
<context:component-scan base-package="com.itheima"></context:component-scan>
控制反转的一些注解:相当于:<bean id="" class="">
①@Component @Target({ElementType.TYPE})
在bean 的类名上写上注解@Component("")
" " 填写的是唯一标识符id,如果不写的话,默认值是当前类名首字母小写作为id ,属性class的话 spring帮你填写不用管了。
②@Controller @Service @Repository @Target({ElementType.TYPE})
他们三个注解都是@Component的衍生注解,他们的作用及属性都是一模一样的。
他们只不过是提供了更加明确的语义化。
@Controller:一般用于表现层的注解。
@Service:一般用于业务层的注解。
@Repository:一般用于持久层的注解。
依赖注入的一些注解
相当于:<property name="" ref=""> <property name="" value="">
①@Autowired @Target({ ElementType.METHOD, ElementType.PARAMETER})
作用:自动按照类型注入。只要容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功
如果ioc容器中没有任何bean的类型和要注入的变量类型匹配,则报错。
如果Ioc容器中有多个类型匹配时:
比如
@Repository("accountDao1")
public class AccountDaoImpl implements IAccountDao
{
public void saveAccount(){
System.out.println("保存了账户1111111111111");
}
}
//--------------------------------------------------
@Repository("accountDao2")
public class AccountDaoImpl2 implements IAccountDao {
public void saveAccount(){
System.out.println("保存了账户2222222222222");
}
}
@Autowired
private IAccountDao accountDao;
这时候使用注解@Autowired注入, IOC去查找IAccountDao 的实现类,发现两个实现类,这时候再用accountDao去 id中查找,如果有就对应没有则报错。
②@Qualifier @Target({ ElementType.METHOD, ElementType.PARAMETER})
和@Autowired配合使用
@Autowired
@Qualifier("accountDao1")
private IAccountDao accountDao;
表示直接去找IAccountDao 的实现类中id名为accountDao1的实现类
③@Resource @Target({ ElementType.METHOD, ElementType.PARAMETER})
上面两个注解的合并写法
属性:
name:用于指定bean的id。
以上三个注入都只能注入其他bean类型的数据,而基本类型和String类型无法使用上述注解实现。
另外,集合类型的注入只能通过XML来实现。
@Value
作用:
注入基本数据类型和 String 类型数据的
属性:
value:用于指定值
范围: 自己查看
用于改变作用范围的(了解部分)
①@ Scope @Target(METHOD)
作用:用于指定bean的作用范围
属性:
value:指定范围的取值。常用取值:singleton prototype
接下来是和生命周期相关的:
他们的作用就和在bean标签中使用init-method和destroy-methode的作用是一样的
②PreDestroy@Target(METHOD)
作用:用于指定销毁方法
③PostConstruct@Target(METHOD)
作用:用于指定初始化方法
总结:
- 配置文件的形式先是用文件在bean标签里配置id和class,然后spring帮你实例化这个class交给你(通过getBean方法),这是IOC控制反转,DI是给IOC提供的bean依赖注入它所需要的对象,注入基本数据类型的依赖只能使用配置文件的形式,注入对象的初始化可以通过注解的形式;
- 注解的形式就是在类名头上添加注解@Component(可根据三层架构细分),然后类名的首字母小写默认作为id,class就是类名的全路径类名,然后spring就自动把当前类初始化的权利收取了,在别的类里想要初始化这个类,只需要在变量名的头上加一个注解@Source (可填写id表示指向对应id,细节看文档)