• <sub id="h4knl"><ol id="h4knl"></ol></sub>
    <sup id="h4knl"></sup>
      <sub id="h4knl"></sub>

      <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
      1. <strong id="h4knl"></strong>

      2. Java認(rèn)證考試知識(shí)點(diǎn):Spring獲取Bean四種方法

        時(shí)間:2024-09-10 16:43:20 JAVA認(rèn)證 我要投稿
        • 相關(guān)推薦

        Java認(rèn)證考試知識(shí)點(diǎn):Spring獲取Bean四種方法

          下面YJBYS小編為大家整理了Spring獲取Bean四種方法,希望對(duì)你有所幫助。更多Java認(rèn)證考試信息,盡在應(yīng)屆畢業(yè)生培訓(xùn)網(wǎng)!

        Java認(rèn)證考試知識(shí)點(diǎn):Spring獲取Bean四種方法

          方法一:通過(guò)Spring提供的工具類獲取ApplicationContext對(duì)象

          代碼:

          import org.springframework.web.context.support.WebApplicationContextUtils;

          ApplicationContext ac1 = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc)

          ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc)

          ac1.getBean(“beanId”);

          ac2.getBean(“beanId”);

          說(shuō)明:

          這種方式適合于采用Spring框架的B/S系統(tǒng),通過(guò)ServletContext對(duì)象獲取ApplicationContext對(duì)象,然后在通過(guò)它獲取需要的類實(shí)例。上面兩個(gè)工具方式的區(qū)別是,前者在獲取失敗時(shí)拋出異常,后者返回null.

          其中 servletContext sc 可以具體 換成 servlet.getServletContext()或者 this.getServletContext() 或者 request.getSession()。getServletContext();

          另外,由于spring是注入的對(duì)象放在ServletContext中的,所以可以直接在ServletContext取出WebApplicationContext 對(duì)象:

          WebApplicationContext webApplicationContext = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

          方法二:在初始化時(shí)保存ApplicationContext對(duì)象

          代碼:

          ApplicationContext ac = new FileSystemXmlApplicationContex(“applicationContext.xml”);

          ac.getBean(“beanId”);

          說(shuō)明:

          這種方式適用于采用Spring框架的獨(dú)立應(yīng)用程序,需要程序通過(guò)配置文件手工初始化Spring的情況。

          方法三:繼承自抽象類ApplicationObjectSupport

          說(shuō)明:

          抽象類ApplicationObjectSupport提供getApplicationContext()方法,可以方便的獲取到ApplicationContext.Spring初始化時(shí),會(huì)通過(guò)該抽象類的setApplicationContext(ApplicationContext context)方法將ApplicationContext 對(duì)象注入。

          方法四:繼承自抽象類WebApplicationObjectSupport

          說(shuō)明:

          類似上面方法,調(diào)用getWebApplicationContext()獲取WebApplicationContext

          方法五:實(shí)現(xiàn)接口ApplicationContextAware

          說(shuō)明:

          實(shí)現(xiàn)該接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 對(duì)象。Spring初始化時(shí),會(huì)通過(guò)該方法將ApplicationContext 對(duì)象注入。

          以上方法適合不同的情況,請(qǐng)根據(jù)具體情況選用相應(yīng)的方法。

          這里值得提一點(diǎn)的是,系統(tǒng)中用到上述方法的類實(shí)際上就于Spring框架緊密耦合在一起了,因?yàn)檫@些類是知道它們是運(yùn)行在Spring框架上的,因此,系統(tǒng)中,應(yīng)該盡量的減少這類應(yīng)用,使系統(tǒng)盡可能的獨(dú)立于當(dāng)前運(yùn)行環(huán)境,盡量通過(guò)DI的方式獲取需要的服務(wù)提供者。

          然后在Action中編寫如下代碼得到Context,(我是覆蓋了Struts Action的setServlet方法,也許還有更好的方法)。

          public void setServlet(ActionServlet servlet){

          super.setServlet(servlet);

          ServletContext servletContext = servlet.getServletContext();

          WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);

          // get yours beans

          }

          我需要在spring的bean中直接獲取,這下可和我們常規(guī)的操作有些不同,因?yàn)閟pring的bean都是pojo的。根本見(jiàn)不著servletconfig和servletcontext的影子。

          這里我需要指出spring給我們提供了兩個(gè)接口:org.springframework.web.context.ServletContextAware和

          org.springframework.web.context.ServletConfigAware.我們可以讓我們的bean實(shí)現(xiàn)上邊的任何一個(gè)接口就能獲取到servletContext了 .

          代碼如下:

          public class DicBean implements ServletContextAware{

          private ServletContext servletContext;

          public void setServletContext(ServletContext sc) {

          this.servletContext=sc;

          System.out.println(“項(xiàng)目的絕對(duì)路徑為:”+servletContext.getRealPath(“/”));

          }

          }

        【Java認(rèn)證考試知識(shí)點(diǎn):Spring獲取Bean四種方法】相關(guān)文章:

        JAVA認(rèn)證基礎(chǔ)知識(shí):Java獲取當(dāng)前的系統(tǒng)時(shí)間06-08

        Java獲取當(dāng)前的系統(tǒng)時(shí)間的方法05-28

        JAVA認(rèn)證考試細(xì)則06-10

        java認(rèn)證考試介紹07-31

        sun java認(rèn)證考試介紹10-23

        JAVA認(rèn)證考試費(fèi)用06-25

        java認(rèn)證考試細(xì)則介紹08-19

        Sun java認(rèn)證考試答案11-06

        JAVA考試認(rèn)證經(jīng)驗(yàn)分享08-22

        Sun Java認(rèn)證考試科目08-30

        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码
      3. <sub id="h4knl"><ol id="h4knl"></ol></sub>
        <sup id="h4knl"></sup>
          <sub id="h4knl"></sub>

          <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
          1. <strong id="h4knl"></strong>

          2. 亚洲精品嫩草影院久久 | 色婷婷色综合缴 | 中国国产不卡视频在线观看 | 亚洲欧美丝袜精品久久中文字幕 | 在线观看精品国产大片 | 中文字幕精品视频在线观 |