最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring中通過ApplicationContext getBean獲取注入對象的方法實例

 更新時間:2019年03月30日 11:05:20   作者:helentang1987  
今天小編就為大家分享一篇關(guān)于spring中通過ApplicationContext getBean獲取注入對象的方法實例,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

用SpringContextUtil實現(xiàn)ApplicationContextAware

package util;
import java.util.Locale;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
public class SpringContextUtil
 implements ApplicationContextAware
{
 private static ApplicationContext context;
 @Override
 public void setApplicationContext(ApplicationContext contex)
  throws BeansException
 {
  System.out.println("--------------------contex---------"+contex);
  SpringContextUtil.context = contex;
 }
 public static ApplicationContext getApplicationContext() { 
   return context; 
 } 
 public static Object getBean(String beanName) {
  return context.getBean(beanName);
 }
 public static String getMessage(String key) {
  return context.getMessage(key, null, Locale.getDefault());
 }
}

工具類

package redis;
import redis.clients.jedis.JedisPool;
import util.SpringContextUtil;
public class RedisUtil {
 private static JedisPool jedisPool;
 static{
  jedisPool = (JedisPool)SpringContextUtil.getBean("jedisPool"); 
 }
  public static JedisPool getJedisPool(){
  if(jedisPool == null){
   jedisPool = (JedisPool)SpringContextUtil.getBean("jedisPool"); 
  }
  return jedisPool;
  }
  public void flusDB(){
  jedisPool.getResource().flushDB();
  }
  public static String set(String key,String value){
  return jedisPool.getResource().set(key, value);
  }
  public static String get(String key){
  return jedisPool.getResource().get(key);
  }
  public static Long del(String key){
  return jedisPool.getResource().del(key);
  }
}

在Spring的配置文件中配置這個類,Spring容器會在加載完Spring容器后把上下文對象調(diào)用這個對象中的setApplicationContext方法

<!--1 自動掃描 將標(biāo)注Spring注解的類自動轉(zhuǎn)化Bean--> 
 <context:component-scan base-package="com.first,com.util" /> 
 <!--2 加載數(shù)據(jù)資源屬性文件 --> 
 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
  <property name="locations">
   <list>
   <value>classpath:jdbc.properties</value>
   <value>classpath:redis.properties</value>
   </list>
  </property>
 </bean> 
 <bean id="springContextUtil" class="util.SpringContextUtil"></bean>
 <import resource="redis-config.xml"/>
在web項目中的web.xml中配置加載Spring容器的Listener
<!-- 初始化Spring容器,讓Spring容器隨Web應(yīng)用的啟動而自動啟動 --> 
<listener> 
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener>

spring配置文件注入Bean類

 <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
    <property name="maxIdle" value="300" /> <!-- 最大能夠保持idel狀態(tài)的對象數(shù) -->
    <property name="testOnBorrow" value="true" /> <!-- 當(dāng)調(diào)用borrow Object方法時,是否進(jìn)行有效性檢查 -->
    <property name="maxActive" value="200" /> 
    <property name="minIdle" value="10"/> 
     <property name="maxWait" value="300" /> 
     <property name="testOnReturn" value="true" /> 
     <property name="testWhileIdle" value="true" /> 
  </bean>
  <bean id="jedisPool" class="redis.clients.jedis.JedisPool">
    <constructor-arg name="poolConfig" ref="jedisPoolConfig" />
    <constructor-arg name="host" value="${redis_addr}" />
    <constructor-arg name="port" value="${redis_port}" type="int" />
    <constructor-arg name="timeout" value="${redis_timeout}" type="int" />
    <constructor-arg name="password" value="#{'${redis_password}'!=''?'${redis_password}':null}" />
    <constructor-arg name="database" value="${redis_db_index}" type="int" />
  </bean>

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

  • Java數(shù)組擴(kuò)容實例代碼

    Java數(shù)組擴(kuò)容實例代碼

    這篇文章主要介紹了Java數(shù)組擴(kuò)容實例代碼,具有一定借鑒價值,需要的朋友可以參考下
    2017-11-11
  • Java正則表達(dá)式之分組和替換方式

    Java正則表達(dá)式之分組和替換方式

    這篇文章主要介紹了Java正則表達(dá)式之分組和替換方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Java pdu短信解碼全面解析

    Java pdu短信解碼全面解析

    本文是根據(jù)python的方法改寫的pdu短信解碼,非常不錯,代碼簡單易懂具有參考借鑒價值,感興趣的朋友一起看看吧
    2016-10-10
  • Java多線程實現(xiàn)Callable接口

    Java多線程實現(xiàn)Callable接口

    本文給大家分享的是使用Java多線程來實現(xiàn)callable接口的方法,以及使用方法,另外還有一個網(wǎng)友的實例,希望能夠?qū)Υ蠹艺莆認(rèn)ava多線程有所幫助。
    2016-06-06
  • 玩轉(zhuǎn)spring boot 結(jié)合jQuery和AngularJs(3)

    玩轉(zhuǎn)spring boot 結(jié)合jQuery和AngularJs(3)

    玩轉(zhuǎn)spring boot,這篇文章主要介紹了結(jié)合jQuery和AngularJs,玩轉(zhuǎn)spring boot,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • 關(guān)于ResponseEntity類和HttpEntity及跨平臺路徑問題

    關(guān)于ResponseEntity類和HttpEntity及跨平臺路徑問題

    這篇文章主要介紹了關(guān)于ResponseEntity類和HttpEntity及跨平臺路徑問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Spring動態(tài)配置計時器觸發(fā)時間的實例代碼

    Spring動態(tài)配置計時器觸發(fā)時間的實例代碼

    這篇文章主要介紹了Spring動態(tài)配置計時器觸發(fā)時間的實例代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-06-06
  • Java日常練習(xí)題,每天進(jìn)步一點點(58)

    Java日常練習(xí)題,每天進(jìn)步一點點(58)

    下面小編就為大家?guī)硪黄狫ava基礎(chǔ)的幾道練習(xí)題(分享)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望可以幫到你
    2021-08-08
  • Java設(shè)計模式之代理模式詳解

    Java設(shè)計模式之代理模式詳解

    這篇文章主要介紹了Java設(shè)計模式之代理模式詳解,文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好的幫助,需要的朋友可以參考下
    2021-05-05
  • Java對象比較之equals與hashCode詳解

    Java對象比較之equals與hashCode詳解

    這篇文章主要介紹了Java對象比較之equals與hashCode詳解,equals?方法和?hashCode?方法是?Object?類中的兩個基礎(chǔ)方法,它們共同協(xié)作來判斷兩個對象是否相等,需要的朋友可以參考下
    2023-12-12

最新評論

贺兰县| 大丰市| 盐边县| 呼伦贝尔市| 莆田市| 乳山市| 南康市| 桓仁| 鄄城县| 中宁县| 盘山县| 滁州市| 安康市| 赞皇县| 琼海市| 宜宾县| 梁平县| 于都县| 鸡西市| 榆社县| 鄂伦春自治旗| 京山县| 新干县| 巴楚县| 牙克石市| 上饶市| 调兵山市| 诏安县| 唐海县| 镇巴县| 耒阳市| 都安| 尼玛县| 含山县| 台东县| 珠海市| 万全县| 永新县| 仙居县| 英德市| 阿勒泰市|