当一个父类拥有子类时,通过@Autowired注入父类对象时会报错,无法区分注入的对象是父对象还是子对象。
public class RedisTemplate implements IRedisTemplate {
private Log log = LogFactory.getLog(IRedisTemplate.class);
private ShardedJedisPool shardedJedisPool;
private static boolean isEnabled = true;
/**
* @param shardedJedisPool the shardedJedisPool to set
*/
public void setShardedJedisPool(ShardedJedisPool shardedJedisPool) {
this.shardedJedisPool = shardedJedisPool;
}
@PostConstruct
private void checkConnect() {
try {
log.info("系统启动,检查Redis服务是否启动!");
ShardedJedis jedis = shardedJedisPool.getResource();
isEnabled = jedis!=null?true:false;
log.info("Redis服务已启动!");
}catch (Exception e) {
log.error("redis 连接失败:"+e.getMessage());
isEnabled = false;
}
}
public static boolean isEnable() {
return isEnabled;
}
public boolean valiEnable() {
//如果启用状态为false,主动尝试一次连接,更新状态
if(!isEnabled) {
try {
log.info("系统启动,检查Redis服务是否启动!");
ShardedJedis jedis = shardedJedisPool.getResource();
isEnabled = (jedis!=null?true:false);
log.info("Redis服务已启动!");
}catch (Exception e) {
log.error("redis 连接失败:"+e.getMessage());
isEnabled = false;
}
}
return isEnabled;
}
/* (non-Javadoc)
* @see com.sf.redis.client.IRedisTemplate#set(java.lang.String, java.lang.String)
*/
public void set(String key, String value) {
if(!valiEnable()) {
return;
}
try {
ShardedJedis jedis = shardedJedisPool.getResource();
jedis.set(SerializationUtils.serialize(key), SerializationUtils.serialize(value));
jedis.close();
} catch (Exception e) {
e.printStackTrace();
isEnabled = false;
log.error("redis 连接失败:"+e.getMessage());
}
}
@Component
public class RedisUtil extends RedisTemplate {
private Log log = LogFactory.getLog(RedisUtil.class);
@Autowired
private ShardedJedisPool shardedJedisPool;
private static boolean isEnabled = true;
public Long setnx(String key, int seconds, String value) {
if(!valiEnable()) {
return null;
}
try {
ShardedJedis jedis = shardedJedisPool.getResource();
Long result = jedis.setnx(SerializationUtils.serialize(key),SerializationUtils.serialize(value));
jedis.expire(SerializationUtils.serialize(key),seconds);
jedis.close();
return result;
} catch (Exception e) {
e.printStackTrace();
isEnabled = false;
log.error("redis 连接失败:"+e.getMessage());
}
return null;
}
}
@Autowired
private IRedisTemplate redisService;
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.sf.redis.client.IRedisTemplate] is defined: expected single matching bean but found 2: redisUtil,redisTemplate
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1126)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:508)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:486)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:615)
at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:308)
... 189 more
@Resource(name = "redisService",type = IRedisTemplate.class)
private IRedisTemplate redisService;
@Autowired
@Qualifier("redisService")
private IRedisTemplate redisService;
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。