0

我有一个带有 redis 的 java 应用程序,它会引发异常。

这里有课程。主类:

public class App {
    public static void main( String[] args ) {
        ManipulatingData manData = new ManipulatingData();
        manData.addData();
    }
}

应用配置:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.springredisdatabook;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
    import org.springframework.data.redis.core.RedisTemplate;
    import org.springframework.data.redis.core.StringRedisTemplate;
    import org.springframework.data.redis.serializer.StringRedisSerializer;
    @Configuration
    public class ApplicationConfig {
        @Bean
        public JedisConnectionFactory connectionFactory() {
            JedisConnectionFactory connectionFactory = new JedisConnectionFactory(); 
            connectionFactory.setHostName("localhost"); 
            connectionFactory.setPort(6379);
            return connectionFactory;
        }
        @Bean 
        public StringRedisTemplate redisTemplate() {
            StringRedisTemplate redisTemplate = new StringRedisTemplate(); 
            redisTemplate.setConnectionFactory(connectionFactory()); 
            return redisTemplate;    
        }
        @Bean
        public RedisTemplate<String, Long> longTemplate() {
            StringRedisSerializer STRING_SERIALIZER = new StringRedisSerializer();
            RedisTemplate<String, Long> redisTemplate = new RedisTemplate<String, Long>();
                redisTemplate.setConnectionFactory(connectionFactory());
                redisTemplate.setKeySerializer(STRING_SERIALIZER);
                redisTemplate.setValueSerializer(LongSerializer.INSTANCE);
            return redisTemplate;
        }
    }

操作数据:

package com.mycompany.springredisdatabook;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

public class ManipulatingData {

    public ManipulatingData() {}

    @Autowired
    StringRedisTemplate redisTemplate;
    public void addData() {
        double start = System.currentTimeMillis(); 
        for (int i=1; i<=1000; i++) {
            redisTemplate.opsForSet().add("k" + i, "v" + i);
        }
        double end = System.currentTimeMillis();
        System.out.println("Add data time: " + (end-start));
    }
    public String getData (String key) {
        return redisTemplate.opsForValue().get(key);
    }
    public void deleteData(String key) {
        redisTemplate.opsForValue().getOperations().delete(key);
    }
}

例外:

Exception in thread "main" java.lang.NullPointerException
    at com.mycompany.springredisdatabook.ManipulatingData.addData(ManipulatingData.java:25)
    at com.mycompany.springredisdatabook.App.main(App.java:11)
Java Result: 1

那么,它是什么?我不知道。顺便说一句,我正在使用弹簧

4

1 回答 1

0

您的redisTemplate实例为空。检查你的 spring.xml 的配置redisTemplate和导致它为空的可能性

于 2013-02-06T06:38:39.557 回答