SpringMVC学习4 - JavaWeb - 获取Spring容器中的某个对象

// param: Spring容器所管理的Bean名
public <T> T getBean( String name ) {
    
    
    return <T>WebApplicationContextUtils.getWebApplicationContext(servletContext).getBean( name );
}

Spring容器层级图

springApplicationContext:管理Bean
ServletContext等价于Tomcat的ApplicationContext:管理Tomcat的多个Web项目
servlet:每个Web项目含有多个Servlet进行请求的处理

WebApplicationContextUtils作用:

方便通过已有的ServletContext对象进行查找该应用项目的Spring容器

getWebApplicationContext: 获得Spring容器

WebApplicationContext.getBean( Str ): 获得被Spring容器管理的某个对象
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-us5cvLfP-1587882846575)(en-resource://database/16662:1)]

Guess you like

Origin blog.csdn.net/weixin_39651356/article/details/105768582