一、spring的概述
1、spring是一个开源的轻量级框架
2、两大核心:IOC容器(反转控制),AOP技术(面向切面编程)
3、可以整合众多的第三方框架mybatis,hibernate, struts,struts2等等。
二、Spring进行解耦的思路:控制反转
具体解决方案:
(1)第一步:使用反射创建对象,避免使用new创建对象
(2)第二步:使用配置文件管理全限类名
三、Spring常用容器对象的区别
(1)顶层的接口:BeanFactory
BeanFactory(用到对象时才会创建): 在全限类名写错的情况下,不会报错,说明在创建容器对象时,不会创建配置文件中的对象,在获取对象时才会创建对象
实现类:XmlBeanFactory(已过时)
(2)常用的接口:ApplicationContext
ApplicationContext(马上创建单例对象): 在全限类名写错的情况下,创建容器对象,会报错,说明创建容器对象时,会创建所有的对象(单例对象)放置到容器中
实现类:ClasspathXmlApplicationContext:读取类路径(classpath)的配置文件
FileSystemXmlApplicationContext:读取的绝对路径(f:/beans.xml)(不用)
AnnotationConfigApplicationContext: 读取注解配置文件
四、bean标签管理对象细节(重点)
(1)bean标签:创建对象
属性:id ,唯一的标识符, 获取对象时使用
class: 全限类名
scope: 对象的范围, 属性值:singleton(单例模式),prototype(原型(多例模式:new))默认的为单例模式
init-method=: 初始化时执行的方法
destroy-method:销毁时执行的方法
(1)实例化bean的三种方法
第一种(推荐):<bean id="userService" class="com.xxx.service.impl.XxxServiceImpl"></bean>
第二种:使用静态工厂模式创建(工厂方法必须是静态的)
<bean id="xxxDao" class="com.xxx.factory.XxxDaoFactory" factory-method="getXxxDao"></bean>
第三种:使用实例工厂模式创建(工厂方法必须是非静态的)
<bean id="instanceFactory" class="com.xxx.factory.XxxDaoInstanceFactory"></bean>
<bean id="xxxDao" factory-bean="instanceFactory" factory-method="getXxxDao"></bean>
五、依赖注入:DI
(1)提供set方法,通过property注入属性值
(2)提供有参数的构造方法,constructor-arg,注入参数值
属性:index:按照索引顺序注入
<constructor-arg index="0" value="2"></constructor-arg>
<constructor-arg index="1" value="名字"></constructor-arg>
<constructor-arg index="2" value="888888.0"></constructor-arg>
<constructor-arg index="3" ref="date"></constructor-arg>
<constructor-arg index="4">
<array>
<value>数组元素1</value>
<value>数组元素2</value>
<value>数组元素3</value>
</array>
</constructor-arg>
type:按照参数类型注入
private String name;
private String address;
private int age;
public Student(String name, String address, int age) {
super();
this.name = name;
this.address = address;
this.age = age;
}
<constructor-arg type="java.lang.String">
<value>jack</value>
</constructor-arg>
<constructor-arg type="java.lang.String">
<value>123456</value>
</constructor-arg>
<constructor-arg type="int">
<value>12</value>
</constructor-arg>
name:按照参数名称注入
<property name="id" value="1"></property>
<property name="name" value="测试"></property>
<property name="money" value="2500.0"></property>
<property name="date" ref="date"></property>
<!--给关联引用类型对象赋值-->
<bean id="date" class="java.util.Date"></bean>
(3)p namespace方式注入
配置namespace:xmlns:p="http://www.springframework.org/schema/p"
<bean id="xxx" class="com.xxx.domain.xxx" scope="prototype" p:属性名="属性值" p:属性名="属性值" p:属性名-ref="关联的beanId">
注意:value: 简单类型 ref:关联引用类型对象
(4)注入集合属性
list,array,set 三个可以通用,map,properties,也可以通用(即结构相同的标签通用)
<bean id="xxx" class="com.xxx.domain.xxx" scope="prototype">
<property name="strs">
<array>
<value>数组元素1</value>
<value>数组元素2</value>
<value>数组元素3</value>
</array>
</property>
<property name="strList">
<list>
<value>集合元素1</value>
<value>集合元素2</value>
<value>集合元素3</value>
</list>
</property>
<property name="strSet">
<set>
<value>set元素1</value>
<value>set元素2</value>
<value>set元素3</value>
</set>
</property>
<property name="map">
<map>
<entry key="1" value="v1"></entry>
<entry key="2" value="v2"></entry>
<entry key="3" value="v3"></entry>
</map>
</property>
</bean>