当前位置: 首页 > 知识库问答 >
问题:

Spring boot+redis

刘才俊
2023-03-14

我正在使用Redis集成的演示Spring Boot应用程序。

我已经参考了各种站点,但最后我更喜欢以下内容:http://www.baeldung.com/spring-data-redis-tutorial

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'studentController': Unsatisfied dependency expressed through field 'studentRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'studentRepositoryImpl' defined in file [/home/klevu/work/Nimesh/Spring Boot Workspace/bootDemo/target/classes/com/example/demo/redis/repository/StudentRepositoryImpl.class]: Initialization of bean failed; nested exception is org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class [class com.example.demo.redis.repository.StudentRepositoryImpl]: Common causes of this problem include using a final class or a non-visible class; nested exception is java.lang.IllegalArgumentException: No visible constructors in class com.example.demo.redis.repository.StudentRepositoryImpl
@Autowired
private RedisTemplate<String, Student> redisTemplate;
package com.example.demo.redis.repository;

import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import com.example.demo.redis.bean.Student;

@Repository
public class StudentRepositoryImpl implements StudentRepository {

    private static final String KEY = "Student";

    //@Autowired
    private RedisTemplate<String, Student> redisTemplate;

    private HashOperations<String, String, Student> hashOps;

    @Autowired
    private StudentRepositoryImpl(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @PostConstruct
    private void init() {
        hashOps = redisTemplate.opsForHash();
    }

    @Override
    public void saveStudent(Student person) {
        hashOps.put(KEY, person.getId(), person);
    }

    @Override
    public void updateStudent(Student person) {
        hashOps.put(KEY, person.getId(), person);
    }

    @Override
    public Student findStudent(String id) {
        return hashOps.get(KEY, id);
    }

    @Override
    public Map<String, Student> findAllStudents() {
        return hashOps.entries(KEY);
    }

    @Override
    public void deleteStudent(String id) {
        hashOps.delete(KEY, id);
    }
}
package com.example.demo.configuration;

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;

@Configuration
public class RedisConfiguration {

    @Bean
    JedisConnectionFactory jedisConnectionFactory() {
        return new JedisConnectionFactory();
    }

    @Bean
    public RedisTemplate<String, Object> redisTemplate(){
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(jedisConnectionFactory());
        return template;
    }


}

Spring boot主入口点声明如下:

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;

@SpringBootApplication
@EnableMongoRepositories(basePackages = {"com.example.demo.mongo.repository"} )
@EnableRedisRepositories(basePackages = {"com.example.demo.redis.repository"})
public class BootDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootDemoApplication.class, args);
    }
}

测试redis的演示控制器如下:

package com.example.demo.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.redis.bean.Student;
import com.example.demo.redis.repository.StudentRepository;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentRepository studentRepository;

    @GetMapping
    public ResponseEntity<Map<String, Student>> index() {
        Map<String, Student> students = studentRepository.findAllStudents();
        return new ResponseEntity<Map<String, Student>>(students, HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public ResponseEntity<Student> getStudentById(@PathVariable("id") String id) {
        Student student = studentRepository.findStudent(id);
        return new ResponseEntity<Student>(student, HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<Student> saveStudent(@RequestBody Student student) {
        studentRepository.saveStudent(student);
        return new ResponseEntity<Student>(student, HttpStatus.CREATED);
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{id}")
    public ResponseEntity<Student> updateStudent(@RequestBody Student student) {
        studentRepository.updateStudent(student);
        return new ResponseEntity<Student>(student, HttpStatus.OK);
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public ResponseEntity<Student> deleteMessage(@PathVariable("id") String id) {
        studentRepository.deleteStudent(id);
        return new ResponseEntity<Student>(HttpStatus.OK);
    }
}

共有1个答案

常海
2023-03-14

您将构造函数设置为私有...将其更改为public

@Autowired
public StudentRepositoryImpl(RedisTemplate redisTemplate) {
    this.redisTemplate = redisTemplate;
}
 类似资料:
  • 已与目标VM断开连接,地址:“javadeBug”,传输:“共享内存” 进程已完成,退出代码为0 PessoAcontroller:

  • 使用 springboot 改造 jeesite,只保留最简单的系统配置 。 介绍 1、运行主类,登录  admin/admin com.wolfking.jeesite.WolfkingJeesiteDriver 2、砍掉了所有的冗余的东西,只保留系统配置,数据库脚本 wolfking-jeesite.sql 3、使用 springboot 集成,使用 HikariDataSource 数据源

  • WeChat-SpringBoot 是使用 Spring Boot 开发的微信开发后端脚手架

  • 生产制造执行系统,基于 springBoot 开发。 精益生产+ISA-95 标准。 结合 MESA 战略计划方向设计框架。

  • 一个简单便捷的基于springboot+RabbitMQ中间件实现的RPC调用框架 远程调用过程如下 首先:消费者和生产者spring容器初始化的时候,会根据配置的的api在RabbitMQ上建立相应的队列,消费者会监听相关队列 1)生产者(client)调用以本地调用方式调用服务; 2)client 接收到调用后通过Hessian将方法、参数等组装成能够进行网络传输的消息体; 3)client

  • SpringBoot + 前端MVVM 基于Java的微服务全栈快速开发实践。 如今Web开发领域,当有人提到Java时,总会让人觉得臃肿、古老而过时且开发效率没有某些动态语言高效,甚至在此之前还有人高喊“Java 已死!”,但是事实真是如此吗?其实如果你一直关注着Java,那你的感悟会更深,尽管它有很多的缺点和啰嗦,但不可否认,Java依然是工业界中最优秀的语言,而且它一直保持着与时俱进。本项目

  • SpringBoot-Plus 是一个基于SpringBoot 2 的管理后台系统,包含了用户管理,组织机构管理,角色管理,功能点管理,菜单管理,权限分配,数据权限分配,代码生成,子系统生成,文档管理和预览等功能.不同于其他简单的开源后台管理系统,SpringBoot-Plus具备适当的企业应用深度。 系统基于Spring Boot 2技术,前端采用了Layui 2。数据库以MySQL/Oracl

  • �� a simple project for Spring Boot ~ 项目概述 ( �� pause update) �� 一个简单的,基于Spring Boot的好友备忘录小项目,通过本项目可以学习Spring Boot与MyBatis的整合及CURD操作的基本思路,同时也可以帮助你学习Thylemeaf模板引擎使用哟 ! 该项目的代码注释详细,逻辑结构清晰,非常具有参考,学习价值哟 !