Springboot (iii) integrated Redis

Source: Internet
Author: User
Tags redis redis server
problems encountered

Springboot Integrated Redis, refer to the blog:

Http://www.tuicool.com/articles/mAriMbI

Problems that exist:
When injecting redistemplate, if you follow the blog's approach, add generics

@Autowired
Private redistemplate<string, user> redistemplate;

A startup error is generated:

2016-10-23 16:32:53.133 ERROR 13404---[main] o.s.boot.springapplication:application startup F ailed org.springframework.beans.factory.UnsatisfiedDependencyException:Error creating Bean with Name ' Hellocontroller ': Unsatisfied dependency expressed through field ' redistemplate ': No qualifying Bean of type [ORG.SPRINGFR Amework.data.redis.core.RedisTemplate] found for dependency [org.springframework.data.redis.core.redistemplate<  Java.lang.String, Com.model.user>]: expected at least 1 beans which qualifies as Autowire candidate for this dependency. Dependency annotations: {@org. springframework.beans.factory.annotation.Autowired (Required=true)}; Nested exception is Org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying bean of type [ Org.springframework.data.redis.core.RedisTemplate] found for dependency [ Org.springframework.data.redis.core.redistemplate<java.lang.string, Com.model.user>]: expected at least 1 bean Which qualifieS as Autowire candidate for this dependency. Dependency annotations: {@org. springframework.beans.factory.annotation.Autowired (Required=true)} at Org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject (autowiredannotationbeanpostprocessor.java:569) ~[spring-beans-4.3.2.release.jar:4.3.2.release] at Org.springframework.beans.factory.annotation.InjectionMetadata.inject (injectionmetadata.java:88) ~[ Spring-beans-4.3.2.release.jar:4.3.2.release] At Org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues ( autowiredannotationbeanpostprocessor.java:349) ~[spring-beans-4.3.2.release.jar:4.3.2.release] at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean ( abstractautowirecapablebeanfactory.java:1214) ~[spring-beans-4.3.2.release.jar:4.3.2.release] at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean (abstractautowirecapablebeanfactory.java:543) ~[spring-beans-4.3.2.release.jar:4.3.2.release] at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean ( abstractautowirecapablebeanfactory.java:482) ~[spring-beans-4.3.2.release.jar:4.3.2.release] at Org.springframework.beans.factory.support.abstractbeanfactory$1.getobject (abstractbeanfactory.java:306) ~[ Spring-beans-4.3.2.release.jar:4.3.2.release] At Org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton ( defaultsingletonbeanregistry.java:230) ~[spring-beans-4.3.2.release.jar:4.3.2.release] at Org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (abstractbeanfactory.java:302) ~[ Spring-beans-4.3.2.release.jar:4.3.2.release] At Org.springframework.beans.factory.support.AbstractBeanFactory.getBean (abstractbeanfactory.java:197) ~[ Spring-beans-4.3.2.release.jar:4.3.2.release] at Org.springframework.beans.factory.support.DefaultListableBEanfactory.preinstantiatesingletons (defaultlistablebeanfactory.java:776) ~[spring-beans-4.3.2.release.jar : 4.3.2.RELEASE] At Org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization ( abstractapplicationcontext.java:861) ~[spring-context-4.3.2.release.jar:4.3.2.release] at Org.springframework.context.support.AbstractApplicationContext.refresh (abstractapplicationcontext.java:541) ~[ Spring-context-4.3.2.release.jar:4.3.2.release] At Org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh ( embeddedwebapplicationcontext.java:122) ~[spring-boot-1.4.0.release.jar:1.4.0.release] at Org.springframework.boot.SpringApplication.refresh (springapplication.java:759) [Spring-boot-1.4.0.release.jar : 1.4.0.RELEASE] at Org.springframework.boot.SpringApplication.refreshContext (springapplication.java:369) [ Spring-boot-1.4.0.release.jar:1.4.0.release] at Org.springframework.boot.SpringApplication.run ( springapplication.java:313) [Spring-boot-1.4.0.release.jar:1.4.0.release] at Org.springframework.boot.SpringApplication.run ( springapplication.java:1185) [Spring-boot-1.4.0.release.jar:1.4.0.release] at Org.springframework.boot.SpringApplication.run (springapplication.java:1174) [Spring-boot-1.4.0.release.jar : 1.4.0.RELEASE] at Com.didispace.Chapter1Application.main (chapter1application.java:18) [Classes/:na] caused by: Org.springframework.beans.factory.NoSuchBeanDefinitionException:No qualifying Bean of type [ Org.springframework.data.redis.core.RedisTemplate] found for dependency [ Org.springframework.data.redis.core.redistemplate<java.lang.string, Com.model.user>]: expected at least 1 bean Which qualifies as Autowire candidate for this dependency. Dependency annotations: {@org. springframework.beans.factory.annotation.Autowired (Required=true)} at Org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound ( defaultlistablebeanfactory.java:1406) ~[spring-beans-4.3.2.release.jar:4.3.2.release] At Org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency ( defaultlistablebeanfactory.java:1057) ~[spring-beans-4.3.2.release.jar:4.3.2.release] at Org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency ( defaultlistablebeanfactory.java:1019) ~[spring-beans-4.3.2.release.jar:4.3.2.release] at Org.springframework.beans.factory.annotation.autowiredannotationbeanpostprocessor$autowiredfieldelement.inject (autowiredannotationbeanpostprocessor.java:566) ~[spring-beans-4.3.2.release.jar:4.3.2.release] ... Common frames omitted

Change to the following is no problem:

@Autowired
private Redistemplate redistemplate;
Integrate detailed procedures

The effect you actually write is as follows: adding MAVEN dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId> Spring-boot-starter-redis</artifactid>
</dependency>
Add a configuration file
Adding Redis server-side configuration to Application.properties
# Redis (redisproperties)
# Redis Database index (default = 0)
spring.redis.database=0  
# Redis server address
Spring.redis.host=localhost  
# Redis Server connection Port
spring.redis.port=6379  
# Redis Server connection password (default is empty)
spring.redis.password=  
# Connection pool Maximum number of connections (using negative values to indicate no limit)
spring.redis.pool.max-active=8  
# Connection pool Maximum blocking wait time (with negative values for No limit)
spring.redis.pool.max-wait=-1  
# The maximum idle connection in the connection pool
spring.redis.pool.max-idle=8  
# Minimum idle connection in connection pool
spring.redis.pool.min-idle=0  
# Connection time-out (ms)
spring.redis.timeout=0  
Test access in Controller
/** * * @version 1.0 * @Description * @Date 2016/10/15 18:30 * @Author NICK * * @RestController public class Hellocon

    Troller {@Autowired private stringredistemplate stringredistemplate;

    @Autowired private Redistemplate redistemplate; @RequestMapping ("/hello") public string Index () {//Save string Stringredistemplate.opsforvalue (). Set ("AAA"
        , "111");
        String string = Stringredistemplate.opsforvalue (). Get ("AAA");

        System.out.println (string);
        Save Object User user = New User ("Superman", 20);

        Redistemplate.opsforvalue (). Set (User.getusername (), user);
        user = new User ("Batman", 30);

        Redistemplate.opsforvalue (). Set (User.getusername (), user);
        user = new User ("Spider-Man", 40);
        Redistemplate.opsforvalue (). Set (User.getusername (), user);
        User User1 = (user) Redistemplate.opsforvalue (). Get ("Superman");
        System.out.println (User1.getage (). Longvalue ()); System.out.println ((User) RedistemplaTe.opsforvalue (). Get ("Batman")). Getage (). Longvalue ());

        System.out.println ((User) Redistemplate.opsforvalue (). Get ("Spider-Man")). Getage (). Longvalue ());
    Return "Hello World"; }
}

Run boot portal, start Redis, start Springboot, access in browser: Http://localhost:8080/hello
Browser display Hello World, background console print
111
20
30
40

Indicates successful integration test

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.