账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    Spring的注入和SpringMVC的注入有什么不同?
    23
    0

    各位前辈,我现在在初学redis,用了RedisTemplate。配置如下:

    @Bean
    public static  RedisConnectionFactory redisConnectionFactory(){
        JedisConnectionFactory factory = new JedisConnectionFactory();
        factory.setPort(6379);
        factory.setHostName("127.0.0.1");
        factory.afterPropertiesSet();
        return factory;
    }
    @Bean
    public static RedisTemplate redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String, Object> template = new RedisTemplate();
        template.setConnectionFactory(factory);
        template.afterPropertiesSet();
        return template;
    }

    在新的项目springboot环境中的启动类里面有main方法。由于main是静态方法,我只能这样注入redisTemplate:

    private static RedisTemplate template;
    @Autowired
    public TestapiApplication(RedisTemplate redisTemplate) {
        this.template = redisTemplate;
    }
    public static void main(String[] args) {
        SpringApplication.run(TestapiApplication.class, args);
        //....
    }

    redisTemplate可以运行成功。
    我就在自己的Web项目中写了一个配置类RedisConfid.java以及一个工具类RedisUtil.java。其中工具类的静态方法test方法就是上述main方法的内容,在某个controller里面直接使用工具类的静态方法,运行的时候报redisTemplate空指针的错误。
    我想问问各位前辈,一样的环境为什么Web下会出现空指针?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部