spring IOC(2)

Spring IOC配置与应用

1. FAQ:不给提示:

a) window – preferences – myeclipse – xml – xml catalog

b) User Specified Entries – add

i. Location: D:\share\0900_Spring\soft\spring-framework-2.5.6\dist\resources\spring-beans-2.5.xsd

ii. URI:   file:///D:/share/0900_Spring/soft/spring-framework-2.5.6/dist/resources/spring-beans-2.5.xsd

iii. Key Type: Schema Location

iv. Key: http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

2.注入类型

b) setter(重要),另外两种基本没用

c) 构造方法(可以忘记)

d) 接口注入(可以忘记)

3.<bean中的 id vs. name

a) name可以用特殊字符,id里面不能写特殊字符

4. 简单属性的注入(需要在beans.xml中配置)

a) <property name=… value=….>

5. <bean 中的scope属性,常用的取值有singleton和proptotype

a) <bean中的scope属性默认是Sinleton,说明bean默认情况是单例

b) singleton 单例

c) proptotype 每次创建新的对象

6. 集合注入

a) 很少用,不重要!参考程序

7. <bean id="..." class="..." autowire="byName"> 自动装配(默认的为no),常用的自动装配有2种

b) byName :按名字来自动装配

c) byType :按类型来自动装配,但是如果同样的类型有2个的话,就会报错。

区别:byName与byType

8. 生命周期

a) <bean id="..." class="..."  lazy-init="">(不重要)容器初始化时,lazy-init =“true”的bean不用初始化

b) <bean id="..." class="..." init-method="init"  destroy-methd="destroy"> init-method(当容器初始化时调用bean中这个方法),destroy-methd(容器关闭时调用bean中的这个方法,在Java应用程序中并不会调用该方法,只有在web应用程序中才会调用该方法)。 不要和prototype一起用(了解),不会执行destroy方法,因为像这个ClassPathXmlApplicationContext它不会监控protoType的死亡,所以不会执行destroy

9. Annotation第一步:

 总结:如果你想用annotation的话

        1.加入新的命名空间: 

           xmlns:context="http://www.springframework.org/schema/context"

        2.向schemaLocation中添加

           http://www.springframework.org/schema/context和

           http://www.springframework.org/schema/context/spring-context-2.5.xsd

        3.声明:<context:annotation-config />说明你程序中用了annotationl来注入的,如果用xml,则不需声明

a) 修改xml文件以支持annotation,参考文档<context:annotation-config />

10. 1.Xml:autowire=””

      2.Annotation:@Autowired(spring自己定义的,使用并不多,还有更好方法代替)

              a) 默认按名字(根据setters方法的参数名)来注入。如果没找到,会按类型来注入。

              b) 在按类型注入时,如果是同一个类型的bean有2个,就会报错。解决:搭配@Qulifier使用,指定注入2个之中的那个bean

              b) 写在private field(第三种注入形式)(不建议,破坏封装)。我们一般写在setters上面

              c) 搭配@Qulifier使用,指定注入那个bean。@Qulifier既可以写在方法上面,也可以写在参数里面

11. @required:在bean初始化的时候你必须给注入,你要是不给它注入,就不对了。这东西就是容错用的,这东西跟我们以前讲过的注解@override非常类似,@override写不写都没关系,打不了运行时,jvm检查到底有没有重写。但是我们为什么要写@override呢?调试错的时候应该是尽早发现错误,如果你写了@override,那么它就会在编译期间发现错误。写@required时候,编译时就会发现错误

12. @Resource(重要:是j2ee定义的标准,所以要引入J2ee中的annotation)

   @Resource是j2ee中定义的标准,所以要引入j2ee中的annotation

   a) 加入:j2ee/common-annotations.jar

   b) 默认首先按名称,名称找不到时才会按类型。(这个可能根据你使用的spring版本不同会有差别,也可能默认按类型注入)

   c) @Resource默认按参数名称自动匹配注入,如果没找到对应的参数名称再按类型

   d) 可以指定特定名称

   e) 推荐使用

   f) 不足:如果没有源码,就无法运用annotation,只能使用xml

    <context:component-scan base-package="">用了这句话的话,对于配置文件中bean都不用写了,spring自动帮你到base-package=""自定包中找

13. @Component @Service @Controller @Repository

a).@Component必须与<context:component-scan base-package="org.springframework.docs.test" />  

    一起用,用于指定那些包中应用程序加了@Component的

b) 初始化的名字默认为类名首字母小写

c) 可以指定初始化bean的名字

14. @Scope(“singeton”)

15. @PostConstruct = init-method; @PreDestroy = destroy-method;

猜你喜欢

转载自weigang-gao.iteye.com/blog/2160565