探索與發現

          研究java技術

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            83 隨筆 :: 0 文章 :: 109 評論 :: 0 Trackbacks

          把spring集成到web框架很簡單,只要在web.xml里面加上
          <context-param>
          ??? <param-name>contextConfigLocation</param-name>
          ??? <param-value>/WEB-INF/applicationContext*.xml</param-value>
          </context-param>
          或者
          <listener>
          ??? <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          </listener>
          如果為下面這種方式的話,那么就是加載默認的文件
          /WEB-INF/applicationContext.xml
          這個在
          org.springframework.web.context.support.XmlWebApplicationContext類里面定義的,,部分代碼:
          /** Default config location for the root context */
          ?public static final String DEFAULT_CONFIG_LOCATION = "/WEB-INF/applicationContext.xml";

          ?/** Default prefix for building a config location for a namespace */
          ?public static final String DEFAULT_CONFIG_LOCATION_PREFIX = "/WEB-INF/";

          ?/** Default suffix for building a config location for a namespace */
          ?public static final String DEFAULT_CONFIG_LOCATION_SUFFIX = ".xml";


          Once the context files are loaded, Spring creates a WebApplicationContext object based on the bean definitions and puts it into the ServletContext.
          這樣我們就可以直接用ServletContext獲取自己想要的參數的,,

          All Java web frameworks are built on top of the Servlet API, so you can use the following code to get the ApplicationContext that Spring created.

          WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);

          通過WebApplicationContext我們就可以通過bean的名字獲取它的實例了
          The WebApplicationContextUtils class is for convenience, so you don't have to remember the name of the ServletContext attribute. Its getWebApplicationContext() method will return null if an object doesn't exist under the WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE key. Rather than risk getting NullPointerExceptions in your application, it's better to use the getRequiredWebApplicationContext() method. This method throws an Exception when the ApplicationContext is missing.

          Once you have a reference to the WebApplicationContext, you can retrieve beans by their name or type. Most developers retrieve beans by name, then cast them to one of their implemented interfaces.

          Fortunately, most of the frameworks in this section have simpler ways of looking up beans. Not only do they make it easy to get beans from the BeanFactory, but they also allow you to use dependency injection on their controllers. Each framework section has more detail on its specific integration strategies.

          posted on 2006-08-02 23:41 蜘蛛 閱讀(632) 評論(0)  編輯  收藏 所屬分類: spring

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 沐川县| 太原市| 库尔勒市| 松江区| 大荔县| 平定县| 什邡市| 吉安市| 金堂县| 林甸县| 瑞丽市| 尚义县| 静宁县| 吴桥县| 襄汾县| 木兰县| 商河县| 当阳市| 祁东县| 微山县| 台江县| 荥经县| 东阿县| 黑河市| 岫岩| 岳阳市| 旌德县| 白山市| 左权县| 金沙县| 伊川县| 巴林左旗| 嘉黎县| 平顺县| 靖远县| 枣庄市| 尼玛县| 舟曲县| 漾濞| 南和县| 泗阳县|