零、Spring搭建
1、导包:4(4个核心包core)+2(logging(日志包)+log4j)
2、准备类,如User
3、书写配置(src/applicationContext.xml)
4、测试
一、IOC和DI
IOC:Inverse of Control :控制反转,将对象的创建权交给了Spring。
DI:Dependence Injection:依赖注入,需要有IOC的环境,Spring创建这个类的过程中,将类的依赖属性设置进去。
二、BeanFactory&ApplicationContext
BeanFactory:Spring原始接口,针对原始接口的实现类功能较为单一。
BeanFactory接口实现类的容器,特点是在每次获取对象时才会创建对象。
ApplicationContext:每次容器启动时就会创建容器中配置的所有对象,并提供更多的功能。
推荐:web开发中,使用ApplicationContext,在资源匮乏时可以使用BeanFactory。
创建ApplicationContext对象:从类路径下加载配置文件。
ApplicationContext ac = new ClassPathXmlApplicationContext(“applicationContext.xml”);
三、Spring配置详解——Bean元素
Bean元素:使用该元素描述Spring容器中管理的对象。
属性:
name:给被管理的对象起个名字,在获取该对象时,可以通过该名字进行获取,可以重复,可以包含特殊字符。
id:功能与name一致,但不可以重复,不能包含特殊字符,不推荐使用。
class:被管理对象的完整类名。
四、Spring配置详解——三种对象的创建方法
1、空参构造方式:通过空参构造函数进行创建(默认创建方法)
2、静态工厂(了解)
3、实例工厂(了解)
五、Spring配置详解——scope属性
1、singleton:默认值,单例对象,被标识为单例的对象在Spring容器中只会存在一个实例。
2、prototype:多例原型,被标识为多例的对象在Spring容器中每次获得才会创建,每次创建都是新的对象。
配置位置:bean标签内。例如:
<bean name="user" class="domain.User" scope="prototype"></bean>
六、Spring配置详解——初始化&销毁方法
<bean name="user" class="domain.user" init-method="init" destroy-method="destroy"></bean>
其中init和destroy方法均在User类中进行实现。
七、Spring配置详解——模块化配置
导入其他Spring配置文件:
<import resource="domain.applicationContext.xml">
八、Spring配置详解——属性注入(set方法)
<bean name="user" class="domain.User">
<!-- 为User对象的name属性注入tom作为值 -->
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
<property name="car" ref="car"></property>
</bean>
<!-- 将car对象配置到容器中 -->
<bean name="car" class="domain.Car">
<property name="name" value="兰博基尼"></property>
<property name="color" value="red"></property>
</bean>
九、Spring配置详解——构造函数
对于拥有有参构造函数的对象
<bean name="user" class="domain.User">
<constructor-arg name="name" value=“20” index="0" type="java.lang.Integer"></constructor>
<contructor-arg name="car" ref="car" index="1"></constructor>
</bean>
十、Spring配置详解——p名称空间&SPEL表达式
p名称空间注入:
1、在applicationContext.xml文件头部的<bean></bean>中加入:
xmlns:xsi="http://www.springframework.org/schema/p"
2、在<bean>标签内进行参数注入
<bean name="user" class=“domain.User” p:name="jack" p:age="18" p:car-ref="car"></bean>
SPEL表达式:
SPEL表达式只能应用于基本类型属性的注入,不支持引用类型属性的注入。
<bean name="user2" class="domain.User">
<property name="name" value="#{user.name}"></property>
<property name="car" ref="car"></property>
</bean>
十一、Spring配置详解——复杂类型注入
1、数组
若数组中只有一个元素,可直接在name后value=“tom”。对于多个元素:
<bean name="user" class="domain.User">
<property name="arr">
<array>
<value>tom</value>
<ref bean="user2" />
</array>
</property>
</bean>
2、list
<bean name="user" class="domain.User">
<property name="list">
<list>
<value>tom</value>
<ref bean="user2" />
</list>
</property>
</bean>
3、map
<property name="map">
<map>
<entry key="url" value="jdbc:mysql:crm"></entry>
<entry key="user3" value-ref="user3"></entry>
<entry key-ref="user2" value-ref="user3"></entry>
</map>
</property>
4、properties
<property>
<props>
<prop key="driverClass">com.jdbc.mysql.Driven</prop>
<prop key="userName">root<prop>
</props>
</property>
十二、管理容器在项目中的生命周期
ApplicationContext的生命周期:与ServletContext一致。
为了保证ApplicationContext在项目中只创建一次,且与ServletContext生命周期一致,可通过Servlet的Listener监听Servlet创建和销毁,随之创建时创建,销毁时销毁,同时将ApplicationContext放入application域中,从而便于获取。
Spring框架帮我们实现了这一功能,我们需要做的是:
1.、导入spring-web-4.2.4.RELEASE.jar
2、在web.xml中配置listener。
<listener>
<listener-class>org.springframework.web.context.ContextLocaderlListener</listener-class>
</listener>
3、指定加载spring配置文件的位置
在web.xml中配置:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
在Action中获取spring容器和对象:从application域中获得。
1、获取ServletContext对象
2、从ServletContext中获取Spring容器
3、从spring容器中获取对象
ServletContext sc = ServletActionContext.getServletContext();
WebApplicationContext ac= WebApplicationContextUtils.getWebApplicationContext(sc);
CustomerService cs = (CustomerService) ac.getBean("customerService");