Spring学习点滴,《Spring in Action》笔记(一)2011-08-18 unmi.cc 隔叶黄莺第二章:装配 Bean--------------------------------------------------------------------------------1. <ref>标签中的 bean, local, parent 三个属性的区别(P53)2. <list><value...</list>和<set><value...</set>可以换着用都 可以为 List, Set 以及数组属性赋值(P55)3. <map>属性用 spring 进行装匹时 key 值只能是字符串类型,不过一般能满足要求(P55)4. 装配 map 属性要用 <entry key="key1"><value>foo</value></entry>, 而装匹 properties 属性可以写成 <prop key="key1">foo</prop>, 这是因为 properties 的值总是字符串,而 map 中很随意 (P55)5. 用 <property name="foo"><null/></property> 形式设置属性为 null, 区别 为字符串 "null"(P56)6. 通过构造函数注入依赖时,对多参数需要借助于 index 或 type 属性来指定对应哪个参数,index 属性能应付所有情况(P58)7. 可为 bean 设置 autowire为四个值, byName, byType, constructor, autodetect, 四种方式自 动装匹;也可以在 <beans> 中设置 default-autowire 属性。手动和自动可以混合使用,手动优 先。你应该清楚自己在做什么,所以不建议用自动装配(P61)8. BeanPostProcessor的方法 postProcessBeforeInitialization 在 bean 初始化之前调用, postProcessAfterInitialization 是在 bean 初始化之后调用,需要注册到 BeanFactory 上,如 factory.addBeanPostProcessor(new BeanPostProcessor(){...})。内置的 ApplicationContextAwareProcessor 注册在了 AbstractApplicationContext 上了(P64)9. BeanFactoryPostProcessor 是在 Bean 工厂载入所有 Bean 定义后,实例化 Bean 之前作处理。 如果是 AbstractApplicationContext ,那么只需要配置 <bean id="myBeanFactoryPostProcessor" class="com.unmi.MyBeanFactoryPostProcessor"/>, 则会自动注册这个,原有的 BeanFactoryPostProcessor 不可用了,不需要显示式的调用 addBeanFactoryPostProcessor 方法(P67)10. 可用 PropertyPlaceholderConfigurer 载入属性文件,然后在其他引用 value 的地方用 ${database.url} 的方式引用(P70)11. 用 CustomEditorConfigurer 注册自己的 PropertyEditorSupport 关联特定的 bean 属性的处理 ,可了解 Spring 有哪些内置的 PropertyEditorSupport(P72)12. Spring 用 ResourceBundleMessageSource 处理国际化,配置成
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>trainingtext</value>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>trainingtext</value>
</property>
</bean>
会读取 trainingtext.prperties, trainingtext_en_US.properties 等。用 Sring text = context.getMessage("button.submit", new Object[0],locale); 或 <sping:message code="button.submit"/> 读取(P73)