核心监听器(启动工程时创建IOC)

  1. 在web.xml中

    <!-- 指定spring核心配置文件路径 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <!-- 配置监听器,当工程启动时,创建ioc容器,存放在ServletContext中 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    

    2.servlet获取IOC

    //从ServletContext中获取唯一的IOC容器
        ApplicationContext ac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        //从IOC容器中获取AccountService
        AccountService accountService = (AccountService) ac.getBean("accountService");
    

猜你喜欢

转载自blog.csdn.net/luoxiao2554/article/details/80443383