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