private Object findSpringBean ( String beanName ) { ServletContext servletContext = this . pageContext . getServletContext ( ) ; Enumeration < String > attrNames = servletContext . getAttributeNames ( ) ; while ( attrNames . hasMoreElements ( ) ) { String attrName = attrNames . nextElement ( ) ; if ( attrName . startsWith ( "org.springframework" ) ) { try { WebApplicationContext springContext = WebApplicationContextUtils . getWebApplicationContext ( servletContext , attrName ) ; Object bean = springContext . getBean ( beanName ) ; return bean ; } catch ( Exception e ) { // continue if the bean cannot be found. } } } return null ; }
Find the Spring bean from WebApplicationContext in servlet context .