Spring IOC与Bean容器

什么是IOC

IOC:控制反转,控制权的转移,应用程序自己不负责依赖对象的建立和维护,而是由外部容器负责建立和维护
DI(依赖注入):一种实现方式
目的:建立对象而且组装对象之间的关系web

Bean容器初始化

基础包:spring

  • org.springframework.beans
  • org.springframework.context
  • BeanFactory提供配置结构和基本功能,加载并初始化Bean
  • ApplicationContext保存了Bean对象并在Spring中被普遍使用

ApplicationContext方式app

  • 本地文件spa

    //绝对路径
    FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext("F:/workspace/appcontext.xml");
  • Classpathcode

    //相对路径
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring-context.xml");
  • Web应用中依赖servlet或Listenerxml

    <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>
        <load-on-startup>1</load-on-startup>
    </servlet>
相关文章
相关标签/搜索