实践周老师教ssm,想要在其中整合redis做缓存,但是之前都是使用的springBoot,所以在这里记录一下整合的过程

1. 首先在pom.xml中添加相关的依赖:

    <!-- redis-->
    <dependency>
      <groupId>redis.clients</groupId>
      <artifactId>jedis</artifactId>
      <version>2.10.1</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.data</groupId>
      <artifactId>spring-data-redis</artifactId>
      <version>2.1.8.RELEASE</version>
    </dependency>

2. 然后在resources目录下创建redis.properties文件

#redis.host=192.168.2.200
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
redis.maxIdle=300
redis.minIdle=10
redis.maxWaitMillis=1000
redis.maxTotal=500
redis.testOnBorrow=true
redis.testOnReturn=true
redis.testWhileIdle=true
redis.blockWhenExhausted=false
redis.numTestsPerEvictionRun=1024
redis.timeBetweenEvictionRunsMillis=30000
redis.minEvictableIdleTimeMillis=1800000

上述各种配置除了前四个为必需之外,其他的参数可以根据需要设置。

3. application-redis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
 
<context:property-placeholder location="classpath:redis.properties" ignore-unresolvable="true"/>
    <!--设置数据池-->
    <bean id="poolConfig"  class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}"></property>
        <property name="minIdle" value="${redis.minIdle}"></property>
        <property name="maxTotal" value="${redis.maxTotal}"></property>
        <property name="maxWaitMillis" value="${redis.maxWaitMillis}"></property>
        <property name="testOnBorrow" value="${redis.testOnBorrow}"></property>
    </bean>
    <!--链接redis-->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
          p:hostName="${redis.host}"
          p:port="${redis.port}"
          p:password="${redis.password}"
          p:pool-config-ref="poolConfig"
          p:timeout="100000"/>
 
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" p:connection-factory-ref="connectionFactory" >
        <!--针对各种数据选择序列化方式-->
        <property name="keySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="valueSerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
        <property name="hashKeySerializer">
            <bean class="org.springframework.data.redis.serializer.StringRedisSerializer" />
        </property>
    </bean>
</beans>

在主配置文件applicationContext.xml中引入redis的配置文件,即:

<import resource="classpath:application-redis.xml"/>

插入到以下位置:


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
    <import resource="classpath:application-redis.xml"/>

</beans>

4. 测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class redisTest {
 
    @Autowired
    private RedisTemplate redisTemplate;
 
 
    @Test
    public void test() {
        redisTemplate.opsForValue().set("name", "zhangsan");
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println(name);
    }
}

5. 特别的如果想在配置类中加上xml配置文件

我们加入这个注解:

@ImportResource("classpath:applicationContext.xml")

比如在我这里就是:

@Configuration
@ComponentScan({"com.tedustore.service"})
@PropertySource("classpath:mysql.properties")
@Import({JdbcConfig.class, MyBatisConfig.class})
@ImportResource("classpath:applicationContext.xml")
@EnableTransactionManagement //开启Spring事务管理
public class SpringConfig {

}

测试类就不是导入xml配置了,直接导入SpringConfig配置类即可。

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = SpringConfig.class)
public class redisTest {
 
    @Autowired
    private RedisTemplate redisTemplate;


    @Test
    public void test() {
        redisTemplate.opsForValue().set("name3", "zhangsan");
        Object name = redisTemplate.opsForValue().get("name");
        System.out.println(name);
    }
}
最后修改:2022 年 11 月 10 日
如果觉得我的文章对你有用,请随意赞赏