书单推荐:成为Java顶级程序员架构师 ,这20来本(高薪)必看点击获取
·½·¨ÈçÏÂ
- ·½·¨Ò»£ºÍ¨¹ý¶ÁÈ¡XMLÎļþ·´ÉäÉú³É¶ÔÏó
- ·½·¨¶þ£ºÍ¨¹ýSpringÌṩµÄutilsÀà»ñÈ¡ApplicationContext¶ÔÏó
- ·½·¨Èý£º¼Ì³Ð×Ô³éÏóÀàApplicationObjectSupport
- ·½·¨ËÄ£º¼Ì³Ð×Ô³éÏóÀàWebApplicationObjectSupport
- ·½·¨Î壺ʵÏÖ½Ó¿ÚApplicationContextAware
- ·½·¨Áù£ºÍ¨¹ýSpringÌṩµÄContextLoader
»ñÈ¡springÖÐbeanµÄ·½Ê½×ܽ᣺
·½·¨Ò»£ºÍ¨¹ý¶ÁÈ¡XMLÎļþ·´ÉäÉú³É¶ÔÏó
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ac.getBean("userService");//±ÈÈ磺<bean id="userService" class="com.cloud.service.impl.UserServiceImpl"></bean>
˵Ã÷£ºÕâÑùµÄ·½Ê½ÊÊÓÃÓÚ’ñÓÃSpring¿ò¼ÜµÄ¶ÀÁ¢Ó¦ÓóÌÐò£¬ÐëÒª³ÌÐòͨ¹ýÅäÖÃÎļþÊÖ¹¤³õʼ»¯SpringµÄÇé¿ö¡£
·½·¨¶þ£ºÍ¨¹ýSpringÌṩµÄ¹¤¾ßÀà»ñÈ¡ApplicationContext¶ÔÏó
ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc); ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); ac1.getBean("beanId"); ac2.getBean("beanId");
˵Ã÷£ºÕâÑùµÄ·½Ê½ÊʺÏÓÚSpring¿ò¼ÜµÄB/Sϵͳ£¬Í¨¹ýServletContext¶ÔÏó»ñÈ¡ApplicationContext¶ÔÏó¡£È»ºóÔÚͨ¹ýËü»ñÈ¡ÐëÒªµÄÀàʵÀý¡£ÉÏÃæÁ½¸ö¹¤¾ß·½Ê½µÄ²î±ðÊÇ£¬Ç°ÕßÔÚ»ñȡʧ°ÜʱÅ׳öÒì³£¡£ºóÕß·µ»Ønull¡£
·½·¨Èý£º¼Ì³Ð×Ô³éÏóÀàApplicationObjectSupport
˵Ã÷£º³éÏóÀàApplicationObjectSupportÌṩgetApplicationContext()·½·¨¡£Äܹ»·½±ãµÄ»ñÈ¡ApplicationContext¡£
Spring³õʼ»¯Ê±¡£»áͨ¹ý¸Ã³éÏóÀàµÄsetApplicationContext(ApplicationContext context)·½·¨½«ApplicationContext ¶ÔÏó×¢Èë¡£
·½·¨ËÄ£º¼Ì³Ð×Ô³éÏóÀàWebApplicationObjectSupport
˵Ã÷£ºÀàËÆÉÏÃæ·½·¨¡£µ÷ÓÃgetWebApplicationContext()»ñÈ¡WebApplicationContext
·½·¨Î壺ʵÏÖ½Ó¿ÚApplicationContextAware
˵Ã÷£ºÊµÏָýӿڵÄsetApplicationContext(ApplicationContext context)·½·¨£¬²¢±£´æApplicationContext ¶ÔÏó¡£Spring³õʼ»¯Ê±£¬»áͨ¹ý¸Ã·½·¨½«ApplicationContext¶ÔÏó×¢Èë¡£
ÏÂÃæÊÇʵÏÖApplicationContextAware½Ó¿Ú·½Ê½µÄ´úÂ룬ǰÃæÁ½ÖÖ·½·¨ÀàËÆ£º
public class SpringContextUtil implements ApplicationContextAware { // SpringÓ¦ÓÃÉÏÏÂÎÄ»·¾³ private static ApplicationContext applicationContext; /** * ʵÏÖApplicationContextAware½Ó¿ÚµÄ»Øµ÷·½·¨¡£ÉèÖÃÉÏÏÂÎÄ»·¾³ * * @param applicationContext */ public void setApplicationContext(ApplicationContext applicationContext) { SpringContextUtil.applicationContext = applicationContext; } /** * @return ApplicationContext */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * »ñÈ¡¶ÔÏó * * @param name * @return Object * @throws BeansException */ public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } }
¾¡¹Ü£¬springÌṩµÄºóÈýÖÖ·½·¨Äܹ»ÊµÈç½ñÆÕͨµÄÀàÖм̳лòʵÏÖ¶ÔÓ¦µÄÀà»ò½Ó¿ÚÀ´»ñÈ¡spring µÄApplicationContext¶ÔÏ󣬿ÉÊÇÔÚʹÓÃÊÇÒ»¶¨Òª×¢ÒâʵÏÖÁËÕâЩÀà»ò½Ó¿ÚµÄÆÕͨjavaÀàÒ»¶¨ÒªÔÚSpring µÄÅäÖÃÎļþapplicationContext.xmlÎļþÀï½øÐÐÅäÖ᣷ñÔò»ñÈ¡µÄApplicationContext¶ÔÏó½«Îªnull¡£
·½·¨Áù£ºÍ¨¹ýSpringÌṩµÄContextLoader
WebApplicationContext wac = ContextLoader.getCurrentWebApplicationContext(); wac.getBean(beanID);
×îºóÌṩһÖÖ²»ÒÀÀµÓÚservlet,²»ÐëҪעÈëµÄ·½Ê½¡£¿ÉÊÇÐëҪעÒâÒ»µã£¬ÔÚserverÆô¶¯Ê±¡£SpringÈÝÆ÷³õʼ»¯Ê±£¬²»ÄÜͨ¹ýÏÂÃæ·½·¨»ñÈ¡Spring ÈÝÆ÷£¬Ï¸½ÚÄܹ»²é¿´springÔ´´úÂëorg.springframework.web.context.ContextLoader¡£
JUNIT²âÊÔʱContextLoader.getCurrentWebApplicationContext()=NULL
ÔÚJUNIT²âÊÔ·½·¨ÖмÓÈëÒÔÏ´úÂ룬Õýʽ»·¾³²»ÓÃ
MockServletContext sc = new MockServletContext(""); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "/applicationContext.xml"); ServletContextListener listener = new ContextLoaderListener(); ServletContextEvent event = new ServletContextEvent(sc); listener.contextInitialized(event);
ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íû¶Ô´ó¼ÒµÄѧϰÓÐËù°ïÖú£¬Ò²Ï£Íû´ó¼Ò¶à¶àÖ§³Ö½Å±¾Ö®¼Ò¡£
转载请注明:谷谷点程序 » Spring在代码中获取bean的几种方式详解