<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          如何手動(dòng)獲取 Spring 容器中的 bean?

          共 1624字,需瀏覽 4分鐘

           ·

          2020-12-16 23:09

          Java技術(shù)棧

          www.javastack.cn

          關(guān)注閱讀更多優(yōu)質(zhì)文章


          ApplicationContextAware 接口的作用:

          先來看下 Spring API 中對(duì)于 ApplicationContextAware 這個(gè)接口的描述:

          即是說,當(dāng)一個(gè)類實(shí)現(xiàn)了這個(gè)接口之后,這個(gè)類就可以方便地獲得 ApplicationContext 中的所有bean。

          換句話說,就是這個(gè)類可以直接獲取Spring配置文件中,所有有引用到的bean對(duì)象。

          如何使用 ApplicationContextAware 接口?

          如何使用該接口?很簡(jiǎn)單。

          1、定義一個(gè)工具類,實(shí)現(xiàn) ApplicationContextAware,實(shí)現(xiàn) setApplicationContext方法

          public?class?SpringContextUtils?implements?ApplicationContextAware?{?
          ????private?static?ApplicationContext?context;

          ????@Override
          ????public?void?setApplicationContext(ApplicationContext?context)
          ????????????throws?BeansException?{
          ????????SpringContextUtils.context?=?context;
          ????}

          ????public?static?ApplicationContext?getContext(){
          ????????return?context;
          ????}

          }

          如此一來,我們就可以通過該工具類,來獲得 ApplicationContext,進(jìn)而使用其getBean方法來獲取我們需要的bean。Spring Aware容器感知技術(shù),這篇推薦看下。

          2、在Spring配置文件中注冊(cè)該工具類

          之所以我們能如此方便地使用該工具類來獲取,正是因?yàn)镾pring能夠?yàn)槲覀冏詣?dòng)地執(zhí)行 setApplicationContext 方法,顯然,這也是因?yàn)镮OC的緣故,所以必然這個(gè)工具類也是需要在Spring的配置文件中進(jìn)行配置的。

          "springContextUtils"?class="com.zker.common.util.SpringContextUtils"?/>

          3、編寫方法進(jìn)行使用

          一切就緒,我們就可以在需要使用的地方調(diào)用該方法來獲取bean了。

          ????public?String?ajaxRegister()?throws?IOException?{
          ????????UserDao?userDao?=?(UserDao)SpringContextUtils.getContext().getBean("userDao");
          ????????if?(userDao.findAdminByLoginName(loginName)?!=?null
          ????????????????||?userDao.findUserByLoginName(loginName)?!=?null)?{
          ????????????message.setMsg("用戶名已存在");
          ????????????message.setStatus(false);
          ????????}?else?{
          ????????????message.setMsg("用戶名可以注冊(cè)");
          ????????????message.setStatus(true);
          ????????}

          ????????return?"register";
          ????}

          作者:Dulk
          來源:https://www.cnblogs.com/deng-cc/p/6373670.html






          關(guān)注Java技術(shù)棧看更多干貨



          戳原文,獲取精選面試題!
          瀏覽 44
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  上床视频免费网站 | 少妇愉情理伦片BD在线播放 | 91成人免费在线 | 亚日韩综合 | 五月激情婷婷影音娱乐 |