TA的每日心情 | 郁闷 2017-12-5 01:44 |
---|
签到天数: 5 天 [LV.2]偶尔看看I
注册会员

- 积分
- 185
|
spring-data-redis怎么注解事务?
我在jUnit中无法@Transactional,一启动就报“无法确定是哪个PlatformTransactionManager”。
我的项目配有hibernateTransactionManager、myBatisTransactionManager,
Redis的TransactionManager是怎么配置的?@Transactional(“这里填什么???”)只有写明是哪个TransactionManager才不会报错,以及交由Spring事务管理,并在jUnit里测试完Rollback一下。
代码如下:
import java.util.Date;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:applicationContext.xml"})
@WebAppConfiguration("src/main/webapp")
//@Transactional
@Rollback(true)
public class RedisForValueDaoTest {
@Autowired
private RedisForValueDao<String, Object> redisForValueDao;
private String key = "say";
@Test
public void test() {
get();
set();
}
public void get() {
System.out.println("[get][" + key + "][" + redisForValueDao.get(key) + "]");
System.out.println("[size][" + redisForValueDao.size(key) + "]");
System.out.println("[hasKey][" + redisForValueDao.hasKey(key) + "]");
}
public void set() {
redisForValueDao.set(key, "Hello World");
System.out.println("[set后][" + key + "][" + redisForValueDao.get(key) + "]");
System.out.println("[set后][有效期(秒)][" + redisForValueDao.getExpire(key) + "]");
redisForValueDao.set(key, "Happy Life", 60, TimeUnit.MINUTES);
System.out.println("[set后][" + key + "][" + redisForValueDao.get(key) + "]");
System.out.println("[set后][有效期(分)][" + redisForValueDao.getExpire(key, TimeUnit.MINUTES) + "]");
Date expire = new Date(System.currentTimeMillis() + (7200 * 1000));
System.out.println("[expire前][" + expire.getTime() + "]");
System.out.println("[expire中][" + redisForValueDao.expire(key, expire) + "]");
System.out.println("[expire后][有效期(秒)][" + redisForValueDao.getExpire(key) + "]");
}
}
异常错误:
java.lang.IllegalStateException: Failed to retrieve PlatformTransactionManager for @Transactional test for test context [DefaultTestContext@4358cfda testClass = RedisForValueDaoTest, testInstance = com.platform.base.common.redis.RedisForValueDaoTest@6c381cae, testMethod = test@RedisForValueDaoTest, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@7bb613c0 testClass = RedisForValueDaoTest, locations = '{classpath:applicationContext.xml}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[empty]], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]]. |
|