简介
前面我们会用BeanFactory工厂模式进行解耦,降低程序的耦合性。本次使用spring的进行解耦,它提高了我们的效率,还有很多其他的功能。
- Spring是分层的 Java SE/EE应用 full-stack 轻量级开源框架,以 IoC(Inverse Of Control: 反转控制)和 AOP(Aspect Oriented Programming:面向切面编程)为内核,提供了展现层 Spring MVC 和持久层 Spring JDBC 以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多 著名的第三方框架和类库,逐渐成为使用最多的Java EE 企业应用开源框架
- 方便解耦,简化开发 通过 Spring提供的 IoC容器,可以将对象间的依赖关系交由 Spring进行控制,避免硬编码所造 成的过度程序耦合。用户也不必再为单例模式类、属性文件解析等这些很底层的需求编写代码,可 以更专注于上层的应用。
- 通过 AOP 轻松应付。 声明式事务的支持 可以将我们从单调烦闷的事务管理代码中解脱出来,通过声明式方式灵活的进行事务的管理, 提高开发效率和质量。
spring创建对象的三种方式
- 第一种方式:使用默认的构造函数创建,在spring的配置文件中使用bean标签,配以id和class属性之后且没有其他事项的标签时,采用的就是默认构造函数创建的bean对象,此时如果类中没有默认构造函数,则对象无法创建
- 第二种方式:使用普通工厂的方法创建对象(使用某个类中的方法创建对象并存入spring容器),一般这个类可能存在于jar包中,我们无法通过修改源码的方式提供默认的构造函数。
- 第三种方式:使用工厂中的静态方法创建对象(使用某个类中的静态方法创建对象,并存入spring容器),一般这个类可能存在于jar包中,我们无法通过修改源码的方式提供默认的构造函数。
这两个类,xml配置文件,会用他们去创建AccountServiceImpl对象。
/**
* 模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式提供默认的构造函数)
* 模拟jar包中的类
*/
public class InstanceFactory {
//使用某个类中的方法创建对象
public IAccountService getAccountServiceImpl(){
return new AccountServiceImpl();
}
}
/**
* 模拟一个工厂类(该类可能是存在于jar包中的,我们无法通过修改源码的方式提供默认的构造函数)
* 模拟jar包中的类
*/
public class StaticFactory {
//使用静态方法创建对象
public static IAccountService getAccountServiceImpl(){
return new AccountServiceImpl();
}
}
<?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的三种方式-->
<!--第一种方式-->
<bean id="accountService" class="cn.itcast.service.impl.AccountServiceImpl"></bean>
<!--第二种方式-->
<bean id="instanceFactory" class="cn.itcast.factory.InstanceFactory"></bean>
<bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountServiceImpl" ></bean>
<!--第三种方式-->
<bean id="accountService" class="cn.itcast.factory.StaticFactory" factory-method="getAccountServiceImpl"></bean>
<!--bean的作用范围-->
<bean scope="singleton" id="accountService" class="cn.itcast.service.impl.AccountServiceImpl"></bean>
<bean scope="singleton" id="accountService" class="cn.itcast.service.impl.AccountServiceImpl"></bean>
<bean id="accountDao" class="cn.itcast.dao.impl.AccountDaoImpl"></bean>
</beans>
spring的依赖注入
IOC的作用:降低程序间的耦合,也就是依赖关系。
依赖关系的管理:交给spring来维护,在当前类需要用到其他类的对象,由spring为我们提供,我们只需要在配置文件中说明就可以了
依赖注入(Dependency Injection):依赖关系的维护就称之为依赖注入
- 要注入的数据类型:
基本类型、String、其他bean类型、复杂类型、集合等
注入方式有三种
构造函数注入
- 使用标签:constructor-arg
- 标签出现的位置:bean标签的内部
- 标签的属性
name:用于指定给构造函数中指定名称的参数赋值(常用)
type:用于指定要注入的数据的数据类型,同时该数据类型也是构造函数的某个或某些参数类型
index:用于指定要注入的数据给构造函数中指定索引位置参数赋值,索引的位置是从0开始
value:用于提供基本类型和String类型的数据
ref:用于其他的bean类型,指的是在spring的ioc核心容器中出现的bean对象
- 优势:在获取bean对象时,注入数据是必须的操作,否则对象无法创建成功。
弊端:改变了bean对象的实例化方式,使我们在创建对象时,如果用不到这些数据,也必须提供
set方法注入
- 使用标签:property
- 出现的位置:bean标签的内部
- 标签的属性:
name:用于指定给构造函数中指定名称的参数赋值(常用)
value:用于提供基本类型和String类型的数据
ref:用于其他的bean类型,指的是在spring的ioc核心容器中出现的bean对象
- 优势:创建对象时没有明确的限制,可以直接使用默认构造函数
弊端:如果有某个成员必须有值,则获取对象是有可能set方法没有执行
复杂类型注入
- 用于给list结构集合注入标签:list、 array 、set(数组、集合)
- 用于给map结构集合注入的标签:map、prop(键值对格式)
- 不管是什么复杂数据类型,只要他们的存储结构相同,那么他们的标签是可以互换的,例如:List集合、数组、和Set集合的存储结构是相同的,那么list、array、set三个标签是可以互换的,map和prop(对应properties)存储结构是键值对,那么map标签和prop标签也是可以互换。
<?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="cn.itcast.service.impl.AccountServiceImpl">
<constructor-arg name="name" value="惠杰猪蹄子"></constructor-arg>
<constructor-arg name="age" value="18"></constructor-arg>
<!--使用ref属性引用定义的bean-->
<constructor-arg name="brithday" ref="now"></constructor-arg>
</bean>
<!--配置一个日期对象-->
<bean id="now" class="java.util.Date"></bean>
<!--set方法注入(更常用)-->
<bean id="accountService2" class="cn.itcast.service.impl.AccountServiceImpl2">
<property name="name" value="张惠杰"></property>
<property name="age" value="18"></property>
<property name="brithday" ref="now"></property>
</bean>
<!--复杂类型的注入/集合类型的注入-->
<bean id="accountService3" class="cn.itcast.service.impl.AccountServiceImpl3">
<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="1" value="aaa"></entry>
<entry key="2" value="bbb"></entry>
<entry key="3">
<value>ccc</value>
</entry>
</map>
</property>
<property name="myProps">
<props>
<prop key="1">aaa</prop>
<prop key="2">bbb</prop>
<prop key="3">ccc</prop>
</props>
</property>
</bean>
</beans>
获取spring的ioc核心对象,并根据id获取对象
- applicationcontext的三个常用实现类
ClassPathXmlApplicationContext:他可以加载类路径下的配置文件,要求配置文件必须在类路径下
FileSystemXmlApplicationContext:他可以加载磁盘任意路径下的配置文件(位置必须有访问权限)
AnnotationConfigApplicationContext:读取注解创建容器的
- 核心容器的两个接口引发的问题:
ApplicationContext: 单例模式适用,他在构建核心容器时,创建对象采取的策略是采用立即加载的方式,只要一读取配置文件就创建配置文件中的对象
BeanFactory:多例模式适用,他在构建核心容器时,创建对象采取的策略是延迟加载的方式,什么时候根据id获取对象什么时候才真正创建对象
public class Test{
public static void main(String[] args) {
//获取核心容器对象
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
//根据id获取bean对象,2种方式
//IAccountService accountService = (IAccountService) ac.getBean("accountService");
IAccountService accountService =ac.getBean("accountService",IAccountService.class);
IAccountDao accountDao=ac.getBean("accountDao", IAccountDao.class);
System.out.println(accountService);
System.out.println(accountDao);
System.out.println("--------------");
//BeanFactory是一个顶层接口
Resource resource=new ClassPathResource("bean.xml");
BeanFactory factory=new XmlBeanFactory(resource);
IAccountService as= (IAccountService) factory.getBean("accountService");
System.out.println(as);
}
}
结果:
spring中bean对象的作用范围调控和生命周期
- bean的作用范围调控
bean标签的scope属性用于指定bean的作用范围取值:
singleton:单例的(默认值)(常用)
prototype:多例的(常用)
request:作用于应用的请求范围
session:作用于应用的会话范围
global-session:作用于集群环境的会话范围(全局会话范围),当不是集群环境时,他就是session
bean的生命周期
单例对象:
出生:当前容器创建时对象就出生
活着:只要容器还在,对象一直存在
死亡:单例对象的生命周期和容器相同
总结:单例对象的生命周期和容器相同
多例对象:
出生:当我们使用对象时spring框架为我们创建
活着:对象只要是在使用中就一直存在
死亡:当对象长时间不用,且没有别的对象引用时,有gc回收