@Autowired RedisTemplate<String,String> redisTemplate; redisTemplate.execute(new SessionCallback<Object>(){ @Override public <K, V> Object execute(RedisOperations<K, V> operations) throws DataAccessException { //開啟事務 operations.multi(); for(int i = 1; i <= 5; i++) { redisTemplate.convertAndSend("channel:test", String.format("我是消息{%d}號: %tT", i, new Date())); redisTemplate.opsForValue().set("key" + i,"key" + i); } //執行事務 operations.exec(); return null; } });