反向控制: 协调依赖的对象之间合作的的责任从对象自身中转移出来。这样可以减轻耦合程度;
Bean装配
spring有两种容器
(1)Bean工厂 org.springframework.beans.factory.BeanFactory:提供了基本的依赖注入支持,
BeanFactory负责实例化对象并创建对象间的关联; 参与对象的生命周期管理,调用用户定义的初始化和销毁方法;
最常用的BeanFactory实现 XmlBeanFactory;
(2)ApplicationContext应用上下文 :org.springframework.context.ApplicationContext:提供了系统构架服务;
提供了文本信息解析工具,包括对国际化的支持;提供了载入文件资源的通用方法,如载入图片;为注册为监听器的Bean发送事件;
(1)和(2)的另外一个区别:单实例Bean如何被载入;
BeanFactory延迟载入所有的Bean,知道getBean()被调用时才创建Bean;
ApplicationContext预先加载所有单实例的Bean,
Bean生命周期:
BeanFactory在加载了Bean之后,Bean的生命周期就开始了;
BeanFactory在Bean可以使用之前还做了很多工作:
(1)容器寻找Bean的定义,并实例化
(2)使用依赖注入,Spring按照Bean定义信息配置其属性;
(3)如果Bean实现了BeanNameAware接口,调用setBeanName()方法,传递Bean的id;
(4)如果Bean实现了BeanFactoryAware接口,调用setBeanFactory()方法,传递BeanFactory自身;
(5)如果有BeanPostProcessor和Bean关联,调用postProcessBeforeInitialization()
(6)如果Bean指定了初始化的方法 init-method ,调用初始化方法;
(7)如果有BeanPostProcessor和Bean关联,调用postProcessAfterInitialization()
Set方式注入依赖
基本配置:
<bean id="id" class="***.**.className"/>
<bean id="id" class="***.**.className" singleton ="false "/>
singleton 设置为false表明是原型,每次返回不同的实例;否则默认返回是同一个实例;
<bean id="id" class="***.**.className" init-method ="" destory-method =""/>
init-method,实例化后立即会被调用 destory-method在Bean从容器中删除之前调用;
例子:
(1)基础类型或string类型的值
<bean id="id" class="***.**.className">
<property name="">
<value>**</value>
</property>
</bean>
(2)引用其他Bean
<bean id="id" class="***.**.className">
<property name="">
<ref bean="bean_id"></ref>
</property>
</bean>
<bean id="bean_id" class="***.**.className"/>
(3)集合
a. List 和 数组
<bean id="id" class="***.**.className">
<property name="">
<list>
<value>**</value>
<ref bean="bean_id"></ref>
</list>
</property>
</bean>
例子: spring使用hibernate时的配置
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>*/*/*/*/*1.hbm.xml</value>
<value>*/*/*/*/*2.hbm.xml</value>
</list>
</property>
</bean>
b. Set
<property id="*" class="*">
<set>
<value> value1 </value>
<ref bean="value2">
</set>
</property>
c. Map
<property id="*" class="*">
<map>
<entry key="key1">
<value>value1</value>
</entry>
<entry key="key2">
<ref bean="value2"/>
</entry>
</map>
</property>
注意:key的值只能是String类型的
;
d. Properties
<property name="*">
<props>
<prop key="">value1</prop>
<prop key="">value2</prop>
</props>
</property>
例子: Spring配置MVC的url映射时:
<property name="mapping">
<props>
<prop key="/viewCourseDetails.htm">viewCourseController</prop>
</props>
</property>
e.null
<property><null/>
</property>
构造函数注入依赖
好处,初始化之后就可以使用; 不可以修改;
与Set注入区别,使用的是<constructor-arg>来设置,且没有name属性;
多个参数时,通过序号和类型进行区分;
(1)<constructor-arg>有一个可选属性index
,可以用来指定构造函数的顺序.(index从0开始)
例如:
<bean id="" class="">
<constructor-arg index="1"><value>****</value></constructor-arg>
<constructor-arg index="0"><value>****</value></constructor-arg>
</bean>
(2)<constructor-arg>还有一个type
属性;
例如
<bean id="" class="">
<constructor-arg type="java.lang.String"><value>****</value></constructor-arg>
<constructor-arg index="java.net.URL"><value>****</value></constructor-arg>
</bean>
当参数出现同类型的参数时,只能是用index属性区分;
PropertyPlaceholderConfigurer
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:datasource.properties</value>
</property>
</bean>
<!--datasource config-->
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="url">
<value>${jdbc.url}</value>
</property>
<property name="username">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
<property name="maxActive">
<value>${conn.maxActive}</value>
</property>
<property name="maxWait">
<value>${conn.maxWait}</value>
</property>
<property name="maxIdle">
<value>${conn.maxIdle}</value>
</property>
<property name="initialSize">
<value>${conn.pool.size}</value>
</property>
</bean>
自定义编辑器 配置:
<bean id="**" class="org.springframework.beans.factory.config.CustomEditorConfigurer">
<property name="customEditors">
<map>
<entry key="***.***.***.customClass">
<bean id="" class="***.***.***.customEditor" />
</entry>
</map>
</property>
</bean>
国际化message
ResourceBundleMessageSource
messageSource这个名字不可以变
添加的配置:
<bean id="messageSource " class="org.springframwork.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>property文件名</value>
</property>
</bean>