Spring学习总结IOC

来源:互联网 发布:pdf expert mac破解版 编辑:程序博客网 时间:2024/06/13 23:29

IOC:控制权的反转,应用程序不负责依赖对象的创建和维护,而是由外部容器负责创建和维护。

DI:依赖注入是一种IOC的实现方式,目的是创建对象并组装对象之间的关系。


Bean容器初始化

基础包:springframework.bean;springframework.context;beanFactory提供配置结构和基本功能,加载并初始化bean;ApplicationContext保存了Bean对象并在Spring中被广泛使用。

方式:ApplicationContext:

本地文件:

FileSystemXmlApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:/code/applicationContext.xml");

classpath:

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
MyDAO test = (MyDAO)applicationContext.getBean("MyDAO");

servlet或者listener:

<listener>

        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<servlet>

    <servlet-name>context</servlet-name>

    <servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>

</servlet>


spring常用注入方式

1、设值注入

2、构造注入


详细见视频:http://www.imooc.com/learn/196