首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring2.5官方手册备忘录(2)

2012-10-07 
spring2.5官方手册备忘录(二)13.尽量用ApplicationContext接口而不是BeanFactory14.想要使用@Autowired注

spring2.5官方手册备忘录(二)
13.尽量用ApplicationContext接口而不是BeanFactory

14.想要使用@Autowired注解,则要注册
<bean class="org.springframework.beans.factory.annotation.
        AutowiredAnnotationBeanPostProcessor"/>

装配默认行为是byType,要求有且只有1个类型相符的bean满足注入条件,若可能1个bean也没有,则可以通过@Autowired(required = false)来避免BeanCreationException 异常,不过现实中情况比较少见,若多于1个类型相符的bean,可以通过追加一个@Qualifier("beanId")注解来变成byName,明确的指明要注入那个id的bean

15.也可以用JSR-250规范中@Resource来替代@Autowired注解,但是它的默认行为是byName,要想使用JSR-250的注解,则要注册
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>

16.通过自动装配注解,也就不需要提供相应的setter方法了,直接通过反射可以注入相应的private域

17.context命名空间的<context:annotation-config/>可以同时至少注册4个BeanPostProcessor:
AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor

18.开启组件自动扫描如<context:component-scan base-package="org.example"/>,
默认也注册了
AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor

(资源部分)

19.加了特定前缀classpath:,强制返回ClassPathResource,无论ApplicationContext是什么实现

20.FileSystemXmlApplicationContext不存在绝对路径,开头加不加"/",都会变成不加

热点排行