欢迎访问我的个人网站==》 jiashubing.cn

springboot注入一个jar包里的bean

 1 import com.yonyou.iuap.context.ContextHolder;
 2 import com.yonyou.yht.cache.CacheManager;
 3 import com.yonyou.yht.cache.redis.RedisPoolFactory;
 4 import org.springframework.beans.factory.annotation.Value;
 5 import org.springframework.context.annotation.Bean;
 6 import org.springframework.context.annotation.Configuration;
 7 import org.springframework.context.annotation.Scope;
 8 import org.springside.modules.nosql.redis.JedisTemplate;
 9 import org.springside.modules.nosql.redis.pool.JedisPool;
10 
11 /**
12  * @author jiashubing
13  * @since 2019/12/23
14  */
15 @Configuration
16 public class BeanConfig {
17 
18     @Value("${redis.url}")
19     private String redisUrl;
20 
21     @Value("${sessionTimeout}")
22     private int sessionTimeout;
23 
24     @Bean("redisPool")
25     @Scope("prototype")
26     public JedisPool redisPoolFactory() {
27         return RedisPoolFactory.createJedisPool(redisUrl);
28     }
29 
30     @Bean("jedisTemplate")
31     public JedisTemplate jedisTemplate() {
32         JedisPool redisPool = ContextHolder.getContext().getBean(JedisPool.class);
33         return new SwitchableJedisTemplate(redisPool);
34     }
35 
36     @Bean("cacheManager")
37     public CacheManager cacheManager() {
38         JedisTemplate jedisTemplate = ContextHolder.getContext().getBean(JedisTemplate.class);
39         CacheManager cacheManager = new CacheManager();
40         cacheManager.setJedisTemplate(jedisTemplate);
41         cacheManager.setSessionTimeout(sessionTimeout);
42         int reConnectionTime = 50;
43         cacheManager.setReConnectionTime(reConnectionTime);
44         return cacheManager;
45     }
46 }

 

posted @ 2021-07-30 21:05  贾树丙  阅读(1538)  评论(0编辑  收藏  举报