一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - spring中通過ApplicationContext getBean獲取注入對象的方法實例

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

2021-07-27 11:28helentang1987 Java教程

今天小編就為大家分享一篇關于spring中通過ApplicationContext getBean獲取注入對象的方法實例,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

用springcontextutil實現applicationcontextaware

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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());
 }
}

工具類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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容器后把上下文對象調用這個對象中的setapplicationcontext方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<!--1 自動掃描 將標注spring注解的類自動轉化bean-->
 <context:component-scan base-package="com.first,com.util" />
 <!--2 加載數據資源屬性文件 -->
 <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應用的啟動而自動啟動 -->
<listener>
 <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
</listener>

spring配置文件注入bean類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<bean id="jedispoolconfig" class="redis.clients.jedis.jedispoolconfig">
   <property name="maxidle" value="300" /> <!-- 最大能夠保持idel狀態的對象數 -->
   <property name="testonborrow" value="true" /> <!-- 當調用borrow object方法時,是否進行有效性檢查 -->
   <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>

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對服務器之家的支持。如果你想了解更多相關內容請查看下面相關鏈接

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 99在线视频免费 | 蜜桃影像传媒破解版 | 日本无卡码一区二区三区 | 99精品国产成人一区二区 | 特黄视频免费看 | 亚洲福利精品电影在线观看 | 欧美又黄又激烈真实床戏 | 国产一卡2卡3卡四卡高清 | 亚洲国产精品综合一区在线 | 免费欧美一级 | 99av麻豆| 日本高清在线观看天码888 | 精品九九视频 | 四虎影业 | 肉车各种play文r | 亚洲国产精品综合福利专区 | 日韩免费在线观看 | 免费看全黄特黄毛片 | 好湿好紧太硬了我好爽 | 国产精品视频一区二区三区经 | 99综合视频 | 日韩精品久久不卡中文字幕 | 麻豆婷婷 | 掰开逼操 | 欧美成人中文字幕在线看 | 亚洲天堂视频在线观看免费 | 草莓在线 | 欧美大片一区二区三区 | 亚洲精品国产精麻豆久久99 | 毛片视频网站在线观看 | 国产裸露片段精华合集链接 | 好大用力深一点视频 | 99热视| 性欧美高清理论片 | 亚洲狼人香蕉香蕉在线28 | 国产小视频在线 | 国产在线激情视频 | 2019天天干天天操 | 久久精品国产只有精品 | 高跟丝袜麻麻求我调教 | 欧美 亚洲 一区 |