redisx-spring集成

Java框架

浏览数:75

2020-6-8

redisx项目地址:http://git.oschina.net/websterlu/redisx

pom.xml中我们引入spring,junit4 和 redisx的依赖。

       
      <dependencies>
       <dependency>
        <groupId>cn.skynethome</groupId>
        <artifactId>redisx-core</artifactId>
        <version>1.0.3-SNAPSHOT</version>
      </dependency>
   
        <!-- springframe start -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.2.4.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.2.4.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-oxm</artifactId>
			<version>4.2.4.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.2.4.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.2.4.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.2.4.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.2.4.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context-support</artifactId>
			<version>4.2.4.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>4.2.4.RELEASE</version>
		</dependency>

		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.2.4.RELEASE</version>
		</dependency>
		
		<!-- springframe end -->
     
     		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
			<scope>test</scope>
		</dependency>
     
  </dependencies>
  
<repositories>  
  <repository>  
    <id>central</id>  
    <name>Central Repository</name>  
    <url>https://oss.sonatype.org/content/groups/public</url>  
    <layout>default</layout>
    <releases>    
        <enabled>true</enabled>    
      </releases>   
    <snapshots>  
      <enabled>true</enabled>  
    </snapshots>  
  </repository>  
</repositories>   

编写spring配置文件,对于spring的集成我们提供两种方式,第一种是通过spring的配置文件直接配置redisx线程池,第二种是通过指定一个配置文件配置redisx线程池。

我们还是用主备作为示例,其他示例请参考码云中redisx中的redisx-demo工程,配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 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.xsd">
    
    <!-- 直接使用redisx线程池 -->
	<bean id="redisXMsterSlave"
		class="cn.skynethome.redisx.spring.RedisXMasterSlave">
		<!-- <property name="configFlag" value="false"></property> --> <!-- 可不配置或false  -->
		<property name="master" value="10.0.1.38:6379"></property>
		<property name="slave" value="10.0.1.38:6380"></property><!-- <property name="slave" value="10.0.1.38:6380,10.0.1.38:6381"></property> -->
		<property name="AUTH" value="123^xdxd_qew"></property><!-- 没有密码可配置为空 -->
		<property name="TEST_ON_BORROW" value="true"></property>
		<property name="MAX_ACTIVE" value="300"></property>
		<property name="MAX_IDLE" value="10"></property>
		<property name="MAX_WAIT" value="3000"></property>
		<property name="TIMEOUT" value="10000"></property>
	</bean>
    
    
    <!-- 指定配置文件配置redisx线程池 -->
	<bean id="redisXMsterSlaveOfConfig"
		class="cn.skynethome.redisx.spring.RedisXMasterSlave">
		<property name="configFlag" value="true"></property>
		<property name="configPath" value="properties/redisx_master_slave_spring.properties"></property>
	</bean> 
	
</beans>

因为是spring,我们采用spring-test引入xml,通过注解方式使用,我们用xml配置对象存,配置文件产生的对象删,这样就可以测试到两种方法。

@RunWith(SpringJUnit4ClassRunner.class) // 整合 
@ContextConfiguration(locations="classpath:applicationContext_masterslave.xml") // 加载配置
public class SpringRedisXMasterSlaveTest 
{
    
	@Autowired
	private RedisXMasterSlave redisXMsterSlave;
	
	@Autowired
	private RedisXMasterSlave redisXMsterSlaveOfConfig;
	
	@Test
	public void TestRedisX()
	{
		String key = "key:_redisx_01_masterslave";
		
		//添加对象
		String r_ = redisXMsterSlave.setObject(key, "12356465");
		System.out.println("存入返回:"+r_);
		//获取对象
		String s = redisXMsterSlave.getObject(key, String.class);
		System.out.println("缓存取数据:"+ s);
		
		//删除对象
		long d_ = redisXMsterSlaveOfConfig.del(key);
		System.out.println("删除返回:"+ d_);
	}
}

运行结果:

存入返回:OK
缓存取数据:12356465
删除返回:1

好了,至此,我们spring集成示例也已经好了,大家自己试试吧。

作者:websterlu