用springcontextutil实现applicationcontextawarepackage 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容器后把上下文对象调用这个对象中的setapplicationcontext方法<!--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类<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> 总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对CodeAE代码之家的支持。如果你想了解更多相关内容请查看下面相关链接
|