1、视频参考黑马32期宜立方商城第6课

redis对于的代码

我们先变向一个redis客户端的接口文件

package com.test;
public interface JedisClient { String set(String key, String value);
String get(String key);
Boolean exists(String key);
Long expire(String key, int seconds);
Long ttl(String key);
Long incr(String key);
Long hset(String key, String field, String value);
String hget(String key, String field);
Long hdel(String key, String... field);
}

单机版:

package com.test;

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool; public class JedisClientPool implements JedisClient { public JedisPool getJedisPool() {
return jedisPool;
} public void setJedisPool(JedisPool jedisPool) {
this.jedisPool = jedisPool;
} private JedisPool jedisPool; @Override
public String set(String key, String value) {
Jedis jedis = jedisPool.getResource();
String result = jedis.set(key, value);
jedis.close();
return result;
} @Override
public String get(String key) {
Jedis jedis = jedisPool.getResource();
String result = jedis.get(key);
jedis.close();
return result;
} @Override
public Boolean exists(String key) {
Jedis jedis = jedisPool.getResource();
Boolean result = jedis.exists(key);
jedis.close();
return result;
} @Override
public Long expire(String key, int seconds) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.expire(key, seconds);
jedis.close();
return result;
} @Override
public Long ttl(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.ttl(key);
jedis.close();
return result;
} @Override
public Long incr(String key) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.incr(key);
jedis.close();
return result;
} @Override
public Long hset(String key, String field, String value) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hset(key, field, value);
jedis.close();
return result;
} @Override
public String hget(String key, String field) {
Jedis jedis = jedisPool.getResource();
String result = jedis.hget(key, field);
jedis.close();
return result;
} @Override
public Long hdel(String key, String... field) {
Jedis jedis = jedisPool.getResource();
Long result = jedis.hdel(key, field);
jedis.close();
return result;
} }

集群版:

package com.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import redis.clients.jedis.JedisCluster; public class JedisClientCluster implements JedisClient { @Autowired
private JedisCluster jedisCluster; @Override
public String set(String key, String value) {
return jedisCluster.set(key, value);
} @Override
public String get(String key) {
return jedisCluster.get(key);
} @Override
public Boolean exists(String key) {
return jedisCluster.exists(key);
} @Override
public Long expire(String key, int seconds) {
return jedisCluster.expire(key, seconds);
} @Override
public Long ttl(String key) {
return jedisCluster.ttl(key);
} @Override
public Long incr(String key) {
return jedisCluster.incr(key);
} @Override
public Long hset(String key, String field, String value) {
return jedisCluster.hset(key, field, value);
} @Override
public String hget(String key, String field) {
return jedisCluster.hget(key, field);
} @Override
public Long hdel(String key, String... field) {
return jedisCluster.hdel(key, field);
} }

我们编写一个applicationContext-redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springfamework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <bean class="com.test.JedisClientCluster"></bean> <bean id="jedisCluster" class="redis.clients.jedis.JedisCluster">
<constructor-arg>
<set>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.111"></constructor-arg>
<constructor-arg name="port" value="7001"></constructor-arg>
</bean> <bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.111"></constructor-arg>
<constructor-arg name="port" value="7002"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.111"></constructor-arg>
<constructor-arg name="port" value="7003"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.111"></constructor-arg>
<constructor-arg name="port" value="7004"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.111"></constructor-arg>
<constructor-arg name="port" value="7005"></constructor-arg>
</bean>
<bean class="redis.clients.jedis.HostAndPort">
<constructor-arg name="host" value="192.168.25.111"></constructor-arg>
<constructor-arg name="port" value="7006"></constructor-arg>
</bean> </set>
</constructor-arg>
</bean>
</beans>

我们编写一个测试代码来进行测试

package com.e3mall.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.test.JedisClientCluster; public class TestRedis { @Test
public void testRedis(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-redis.xml");
JedisClientCluster clientCluster =applicationContext.getBean(JedisClientCluster.class);
clientCluster.hset("jjj", "sdjj", "111");
String hget = clientCluster.hget("jjj", "sdjj");
System.out.println(hget);
}
}

运行的时候出现下面的情况

出现了jedisCluster这个对象为null,

说明

    @Autowired
private JedisCluster jedisCluster;
这个注入没有起到效果
为啥出现这个情况了
https://blog.csdn.net/bluetjs/article/details/45368769

在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册

AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、

PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。

注册这4个 BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。

例如:

如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下:


  1. <bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/>

如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor

如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。

如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。同样,传统的声明方式如下:


  1. <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

一般来说,这些注解我们还是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供<context:annotation-config/>的简化配置方式,自动帮你完成声明。

不过,呵呵,我们使用注解一般都会配置扫描包路径选项


  1. <context:component-scan base-package=”XX.XX”/>

该配置项其实也包含了自动注入上述processor的功能,因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。

本文出自 “苦逼coder” 博客,请务必保留此出处http://mushiqianmeng.blog.51cto.com/3970029/723880

为了解决上面的问题:

我们需要在xml文件中配置:

方法一:<context:annotation-config/>

方法2:添加包扫描:

  1. <context:component-scan base-package=”XX.XX”/>

spring 整合redis集群中使用@autowire无效问题的解决办法的更多相关文章

  1. Spring 整合Redis 出现 afterPropertiesSet signature: ()V) Incompatible argument to function 解决办法

    正在做SpringMVC+Redis整合的练习 使用的是 spring-data-redis 和 Jedis 配置好之后出现了以下错误: Caused by: java.lang.VerifyErro ...

  2. SpringBoot整合Redis集群

    一.环境搭建 Redis集群环境搭建:https://www.cnblogs.com/zwcry/p/9174233.html 二.Spring整合Redis集群 1.pom.xml <proj ...

  3. Spring集成Redis集群(含spring集成redis代码)

    代码地址如下:http://www.demodashi.com/demo/11458.html 一.准备工作 安装 Redis 集群 安装参考: http://blog.csdn.net/zk6738 ...

  4. (七)整合 Redis集群 ,实现消息队列场景

    整合 Redis集群 ,实现消息队列场景 1.Redis集群简介 1.1 RedisCluster概念 2.SpringBoot整合Redis集群 2.1 核心依赖 2.2 核心配置 2.3 参数渲染 ...

  5. Redis设置认证密码 Redis使用认证密码登录 在Redis集群中使用认证密码

    Redis默认配置是不需要密码认证的,也就是说只要连接的Redis服务器的host和port正确,就可以连接使用.这在安全性上会有一定的问题,所以需要启用Redis的认证密码,增加Redis服务器的安 ...

  6. 在Redis集群中使用pipeline批量插入

    在Redis集群中使用pipeline批量插入 由于项目中需要使用批量插入功能, 所以在网上查找到了Redis 批量插入可以使用pipeline来高效的插入, 示例代码如下: Pipeline p = ...

  7. 转:Redis设置认证密码 Redis使用认证密码登录 在Redis集群中使用认证密码

    Redis默认配置是不需要密码认证的,也就是说只要连接的Redis服务器的host和port正确,就可以连接使用.这在安全性上会有一定的问题,所以需要启用Redis的认证密码,增加Redis服务器的安 ...

  8. 通过扩展redis-cli来实现在redis集群中移动槽位

    下面的扩展代码基于redis 5.0.2进行扩展, 对于其他的redis版本, 我没有进行相关的测试.考虑到redis集群的修改频率,这段代码应该同时适用于其他的redis版本. 下面为修改的代码: ...

  9. Spring Boot2.0之 整合Redis集群

    项目目录结构: pom: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:// ...

随机推荐

  1. Rocket - debug - TLDebugModuleInner - Abstract Command State Machine

    https://mp.weixin.qq.com/s/RcXI8uEHvZHGCvX3DoVR4Q 简单介绍TLDebugModuleInner中处理抽象命令时的状态机. 1. CtrlState 定 ...

  2. Java-接口概念辨析

    https://mp.weixin.qq.com/s/HQZhlS-ffgEMqhB2rHax1w 1. 类        是属性成员和方法成员的集合:2. 父类      是子类相同属性成员和方法成 ...

  3. Rocket - diplomacy - BaseNode

    https://mp.weixin.qq.com/s/eOgNLi_MJ8HJOpepGaaW8Q   简单介绍BaseNode的实现.   ​​   1. You cannot create a n ...

  4. 分享按钮(QQ,微信,微博等)移入动画效果

    ps:最近写的很多博客都是在以前在项目里写过的,之所以现在写出来,最大的目的就是希望自己以后用到的时候比较容易找,而且现在再写一遍,有助于加深印象! 很简单的效果,说先实现方式: 1.图标来自 阿里巴 ...

  5. 使用Mac的Remote Desktop Manager连接ubuntu16.04 & Win10的远程桌面

    疫情严重,公司实行远程办公.自己只有mac电脑,苦于3个系统间跨平台建立远程桌面. 今天,终于尝试成功!特来记录,以防别人踩坑! Mac远程软件安装 Remote Desktop Manager软件非 ...

  6. Java实现 LeetCode 461 汉明距离

    461. 汉明距离 两个整数之间的汉明距离指的是这两个数字对应二进制位不同的位置的数目. 给出两个整数 x 和 y,计算它们之间的汉明距离. 注意: 0 ≤ x, y < 231. 示例: 输入 ...

  7. Java实现 LeetCode 322 零钱兑换

    322. 零钱兑换 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成总金额,返回 -1. 示例 1: 输 ...

  8. java实现第六届蓝桥杯胡同门牌号

    胡同门牌号 小明家住在一条胡同里.胡同里的门牌号都是连续的正整数,由于历史原因,最小的号码并不是从1开始排的. 有一天小明突然发现了有趣的事情: 如果除去小明家不算,胡同里的其它门牌号加起来,刚好是1 ...

  9. 数据的存储结构浅析LSM-Tree和B-tree

    目录 顺序存储与哈希索引 SSTable和LSM tree B-Tree 存储结构的比对 小结 本篇主要讨论的是不同存储结构(主要是LSM-tree和B-tree),它们应对的不同场景,所采用的底层存 ...

  10. 通过SecureCRT向远程Linux主机上传和下载文件

    有时候直接在Linux服务器上通过 wget 或 curl 工具下截比较大的网络文件时会比较慢,这时我们通常会改用在Windows平台通过迅雷等更加现代化的下载功具下好目标文件(迅雷开会员才能更高速的 ...