spring中通過(guò)ApplicationContext getBean獲取注入對(duì)象的方法實(shí)例
用SpringContextUtil實(shí)現(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()); } }
工具類(lèi)
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的配置文件中配置這個(gè)類(lèi),Spring容器會(huì)在加載完Spring容器后把上下文對(duì)象調(diào)用這個(gè)對(duì)象中的setApplicationContext方法
<!--1 自動(dòng)掃描 將標(biāo)注Spring注解的類(lèi)自動(dòng)轉(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項(xiàng)目中的web.xml中配置加載Spring容器的Listener <!-- 初始化Spring容器,讓Spring容器隨Web應(yīng)用的啟動(dòng)而自動(dòng)啟動(dòng) --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
spring配置文件注入Bean類(lèi)
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxIdle" value="300" /> <!-- 最大能夠保持idel狀態(tài)的對(duì)象數(shù) --> <property name="testOnBorrow" value="true" /> <!-- 當(dāng)調(diào)用borrow Object方法時(shí),是否進(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é)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
Springboot解決ajax+自定義headers的跨域請(qǐng)求問(wèn)題
由于瀏覽器同源策略(同源策略,它是由Netscape提出的一個(gè)著名的安全策略,現(xiàn)在所有支持JavaScript 的瀏覽器都會(huì)使用這個(gè)策略。接下來(lái)通過(guò)本文給大家介紹Springboot如何優(yōu)雅的解決ajax+自定義headers的跨域請(qǐng)求 ,需要的朋友可以參考下2019-05-05Java實(shí)現(xiàn)將導(dǎo)出帶格式的Excel數(shù)據(jù)到Word表格
在Word中制作報(bào)表時(shí),我們經(jīng)常需要將Excel中的數(shù)據(jù)復(fù)制粘貼到Word中,這樣則可以直接在Word文檔中查看數(shù)據(jù)而無(wú)需打開(kāi)另一個(gè)Excel文件。本文將通過(guò)Java應(yīng)用程序詳細(xì)介紹如何把帶格式的Excel數(shù)據(jù)導(dǎo)入Word表格。希望這篇文章能對(duì)大家有所幫助2022-11-11基于SpringBoot開(kāi)機(jī)啟動(dòng)與@Order注解
這篇文章主要介紹了SpringBoot開(kāi)機(jī)啟動(dòng)與@Order注解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Java核心庫(kù)實(shí)現(xiàn)AOP過(guò)程
給大家分享一下利用Java核心庫(kù)實(shí)現(xiàn)簡(jiǎn)單的AOP的經(jīng)驗(yàn)分享和教學(xué),需要的讀者們參考下吧。2017-12-12Java實(shí)現(xiàn)帶有權(quán)重隨機(jī)算法的示例詳解
這篇文章主要為大家詳細(xì)介紹了Java如何實(shí)現(xiàn)帶有權(quán)重隨機(jī)算法,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-10-10idea maven pom不自動(dòng)更新的解決方法
這篇文章主要介紹了idea maven pom不自動(dòng)更新的解決方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08idea插件生成jpa實(shí)體類(lèi)的實(shí)現(xiàn)示例
本文主要介紹了idea插件生成jpa實(shí)體類(lèi)的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01JavaWeb簡(jiǎn)單用戶(hù)登錄注冊(cè)實(shí)例代碼(有驗(yàn)證碼)
這篇文章主要介紹了JavaWeb簡(jiǎn)單用戶(hù)登錄注冊(cè)實(shí)例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02java 輸入3個(gè)數(shù)a,b,c,按大小順序輸出的實(shí)例講解
今天小編就為大家分享一篇java 輸入3個(gè)數(shù)a,b,c,按大小順序輸出的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07