Spring简介
1.Spring是一个一站式的分成结构 轻量级的开发框架
特点: 开源 轻量级(体积小) 分层架构(按需添加)
一站式(对目前流行的框架支持非常高) IOC DI
可以把Spring理解为对象的管家 帮我们管理项目中用到的对象
使用Spring的好处
降低复杂性
松耦合
高性能
易测试 可以使用junit测试
声明式事物
IOC(Spring的核心思想):inverse of control 反转控制
就是把对象的创建反转(交给)给Spring来管理
之前是我们手动new对象 现在是问Spring拿对象
实现原理:通过反射 + 配置文件来达到
DI(技术):dependency injection (依赖注入)
依赖注入最终的目的就是提高程序的扩展性 尽可能不去修改源代码
bean的创建和注入
1.bean的创建方式
1.构造函数创建 (默认的)
2.静态工厂创建 (调用指定的静态方法获取bean)
3.实例工厂创建 (调用实例方法获取bean)
依赖注入的方式
1.set注入(就是通过set方法 所以必须有set方法)
2.构造函数注入
3.<:p 命名空间
4.SPEL
示例:创建符合javabean规范的Person类,Car类
<!-- 构造函数创建bean set注入-->
<bean class="com.xxx.Person" name="person1">
<property name="name" value="张三"></property>
<property name="age" value="20"></property>
<property name="car" ref="car"></property>
</bean>
<bean class="com.lanou.Car" name="car">
<property name="name" value="劳斯莱斯"></property>
<property name="color" value="彩虹色"></property>
</bean>
<bean name="person2" class="com.lanou.PersonFactory"
factory-method="getPerson">
<property name="name" value="李四"></property>
</bean>
<bean class="com.lanou.PersonFactory" name="personF"></bean>
<bean name="person3" class="com.lanou.PersonFactory"
factory-method="getPerson" factory-bean="personF">
<property name="name" value="王五"></property>
</bean>
<bean name="person4" class="com.lanou.Person">
<constructor-arg name="age" value="18"></constructor-arg>
<constructor-arg name="name" value="10086" type="int"></constructor-arg>
<constructor-arg name="car" ref="car" index="0"></constructor-arg>
</bean>
<bean name="person5" class="com.lanou.Person" p:name="尔康" p:age="30" p:car-ref="car"></bean>
<bean name="person6" class="com.lanou.Person">
<property name="name" value="#{person.name}"></property>
<property name="age" value="#{person5.age}"></property>
<property name="car" ref="car"></property>
</bean>
bean的获取
1.ApplicationContext接口有两个实现类 都可以加载配置文件
ApplicationContext ac1 = new
ClassPathXmlApplicationContext("beans.xml");
ApplicationContext ac2 = new
FileSystemXmlApplicationContext("file:配置文件的真实路径");
ac1.getBean("person")
ac2.getBean("person")
2.Spring提供了两种容器
Resource resource = new FileSystemResource("配置文件的真实路径");
BeanFactory factory = new XmlBeanFactory(resource);
ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
bean元素的属性(生命周期)
1. scope属性
// 在配置文件中除了可以用name设置对象的名字外 还可以用id
<bean id="person1" scope="singleton" class="com.
xxx.Person"></bean>
2.bean的声明周期方法
<bean id="person2" class="com.xxx.Person" init-method="init"
destroy-method="destory"></bean>
复杂数据类型的注入
1.以数组(array) 集合(list map properties)为例
创建javabean实体类 类中以下列元素为成员变量
private Object[] array;
private List list;
private Map map;
private Properties pop;
// 配置文件中创建该对象
<bean id="cbean" class="com.xxx.CollectionBean">
// 元素的注入方式
array 使用array子标签
例如 <property name="array" >
<array>
<value>123</value>
<value>456</value>
</array>
</property>
list 使用list子标签 同array
map 使用entry子标签
例如: <property name="map" >
<map>
<entry key="xx" vlue=""></entry>
<entry key-ref="引用类型" vlue=""></entry>
</map>
</property>
properties 使用props子标签
例如:<property name="pop" >
<props>
<prop key="xx">value</prop>
</props>
</property>
配置文件的模块化
1.当一个配置文件内容太多时 可以将其按其功能模块划分
(1.在创建容器时 传入多个字符串对象(配置文件名)
ApplicationContext ac = new
ClassPathXmlApplicationContext("配置文件1","配置文件2");
(2.在配置文件中使用import标签导入
<import resource="classpath:配置文件名"/>
Spring的生命周期
1.用下面这种方式获取ApplicationContext 会每次请求都创建一个新的Spring
ApplicationContext ac1 = new
ClassPathXmlApplicationContext("beans.xml");
Person p = (Person) ac1.getBean("person");
2.Spring作为框架 当然不应该每次请求都创建一个新的
我们希望Spring能够与项目的启动一并启动 跟随项目的停止一并销毁
实现步骤
1.在web.xml中配置监听器 使得项目启动时Spring也能够一起启动
类名org.springframework.web.context/ContextLoaderListener
当监听到应用启动时会自动创建Spring容器并放到Context域中
2.WebApplicationContextUtils 工具用来从WebApplicationContext中取出Spring容器;
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
// 获取ApplicationContext
// 因为key容易写错 所以Spring提供了一个工具用来获取容器
// 先获取ServletContext 再获取Spring容器
ServletContext sc = ServletActionContext.getServletContext();
WebApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(sc);
Person p = (Person) ac2.getBean("person");