您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)spring中通過ApplicationContext getBean獲取注入對(duì)象的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
用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()); } }
工具類
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è)類,Spring容器會(huì)在加載完Spring容器后把上下文對(duì)象調(diào)用這個(gè)對(duì)象中的setApplicationContext方法
<!--1 自動(dòng)掃描 將標(biāo)注Spring注解的類自動(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類
<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>
關(guān)于“spring中通過ApplicationContext getBean獲取注入對(duì)象的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。