Spring boot启动时报错

新手上路,请多包涵
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.0.2.RELEASE)

2018-05-30 10:03:51.252  INFO 6448 --- [  restartedMain] com.example.DemoApplication              : Starting DemoApplication on huyh-PC with PID 6448 (D:\work\springbootspace\demo\target\classes started by huyh in D:\work\springbootspace\demo)
2018-05-30 10:03:51.254  INFO 6448 --- [  restartedMain] com.example.DemoApplication              : No active profile set, falling back to default profiles: default
2018-05-30 10:03:51.775  INFO 6448 --- [  restartedMain] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@5d4441ea: startup date [Wed May 30 10:03:51 CST 2018]; root of context hierarchy
2018-05-30 10:03:53.404  WARN 6448 --- [  restartedMain] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.example]' package. Please check your configuration.
2018-05-30 10:03:53.509  INFO 6448 --- [  restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Multiple Spring Data modules found, entering strict repository configuration mode!
2018-05-30 10:03:54.092  INFO 6448 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$5cc6f725] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-05-30 10:03:54.107  INFO 6448 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cache.annotation.ProxyCachingConfiguration' of type [org.springframework.cache.annotation.ProxyCachingConfiguration$$EnhancerBySpringCGLIB$$a5d92d43] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2018-05-30 10:03:54.840  INFO 6448 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2018-05-30 10:03:54.885  INFO 6448 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-05-30 10:03:54.885  INFO 6448 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-05-30 10:03:54.890  INFO 6448 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [D:\work\JDK\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\ProgramData\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\work\JDK\bin;D:\work\SVN\bin;C:\Program Files (x86)\Git\cmd;%TOMCAT_HOME%\bin;D:\work\tomcat7\lib;D:\work\JDK\bin;D:\work\JDK\jre\bin;.]
2018-05-30 10:03:55.634  INFO 6448 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-05-30 10:03:55.634  INFO 6448 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 3864 ms
2018-05-30 10:03:56.250  INFO 6448 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2018-05-30 10:03:56.259  INFO 6448 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-05-30 10:03:56.260  INFO 6448 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-05-30 10:03:56.261  INFO 6448 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-05-30 10:03:56.262  INFO 6448 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
2018-05-30 10:03:56.854  WARN 6448 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisCacheTransfer': Unsatisfied dependency expressed through method 'setJedisConnectionFactory' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.redis.connection.jedis.JedisConnectionFactory' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
2018-05-30 10:03:56.869  INFO 6448 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2018-05-30 10:03:56.885  INFO 6448 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-05-30 10:03:57.077 ERROR 6448 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method setJedisConnectionFactory in com.example.Utils.cache.RedisCacheTransfer required a bean of type 'org.springframework.data.redis.connection.jedis.JedisConnectionFactory' that could not be found.
    - Bean method 'redisConnectionFactory' in 'JedisConnectionConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.data.redis.connection.RedisConnectionFactory; SearchStrategy: all) found beans of type 'org.springframework.data.redis.connection.RedisConnectionFactory' redisConnectionFactory


Action:

Consider revisiting the conditions above or defining a bean of type 'org.springframework.data.redis.connection.jedis.JedisConnectionFactory' in your configuration.


Process finished with exit code 0

刚接触spring boot在用redis做二级缓存的时候,依照往上的例子做的,结果启动的时候就报这个错误!
pom依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

然后下面是application的

#redis
spring.redis.database=1
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.timeout=50000
#数据库配置
mybatis.mapperLocations=classpath:mappers/*.xml
mybatis.type-aliases-package= com.example.entity
mybatis.config-location= classpath:mybatis-config.xml

#全局映射器启用缓存 *主要将此属性设置完成即可
mybatis.cacheEnabled=true
#查询时,关闭关联对象即时加载以提高性能
mybatis.lazyLoadingEnabled=false
#对于未知的SQL查询,允许返回不同的结果集以达到通用的效果
mybatis.multipleResultSetsEnabled=true
#设置关联对象加载的形态,此处为按需加载字段(加载字段由SQL指 定),不会加载关联表的所有字段,以提高性能
mybatis.aggressiveLazyLoading=true

#pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql

spring.datasource.url = jdbc:mysql://localhost:3306/exam
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driverClassName =com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10

报错类


@Component
public class RedisCacheTransfer {

    @Autowired
    public void setJedisConnectionFactory(JedisConnectionFactory jedisConnectionFactory) {
        MybatisRedisCache.setJedisConnectionFactory(jedisConnectionFactory);
    }

}

然后启动的时候就是这个错

***************************
APPLICATION FAILED TO START
***************************

Description:

Parameter 0 of method setJedisConnectionFactory in com.example.Utils.cache.RedisCacheTransfer required a bean of type 'org.springframework.data.redis.connection.jedis.JedisConnectionFactory' that could not be found.
    - Bean method 'redisConnectionFactory' in 'JedisConnectionConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.data.redis.connection.RedisConnectionFactory; SearchStrategy: all) found beans of type 'org.springframework.data.redis.connection.RedisConnectionFactory' redisConnectionFactory


Action:

Consider revisiting the conditions above or defining a bean of type 'org.springframework.data.redis.connection.jedis.JedisConnectionFactory' in your configuration.


Process finished with exit code 0

我也不知道我贴的这些够不够大佬们分析问题,如果想看哪里我可以再贴上来

阅读 35.9k
5 个回答

原因如 @喵了个咪 所说,Spring-boot 2.0 默认使用redis client是lettuce

解决办法

想试试lettuce:

把JedisConnectionFactory依赖替换为org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory或者org.springframework.data.redis.connection.RedisConnectionFactory

推荐后者,它是JedisConnectionFactory和LettuceConnectionFactory都实现的接口,好处是哪天你想换个redis client可以只改项目依赖而不用动Java代码

想换成jedis:

排除lettuce依赖,引入jedis

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>io.lettuce</groupId>
                    <artifactId>lettuce-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>

或者用spring-boot 1.x也可以?


报错分析

- Bean method 'redisConnectionFactory' in 'JedisConnectionConfiguration' not loaded because @ConditionalOnMissingBean (types: org.springframework.data.redis.connection.RedisConnectionFactory; SearchStrategy: all) found beans of type 'org.springframework.data.redis.connection.RedisConnectionFactory' redisConnectionFactory

这里是说因为已经存在一个RedisConnectionFactory的Bean了, JedisConnectionConfiguration这个类里的redisConnectionFactory方法没有加载

根据LettuceConnectionConfiguration的代码

@Configuration
@ConditionalOnClass(RedisClient.class)
class LettuceConnectionConfiguration extends RedisConnectionConfiguration

可知spring发现lettuce的redis客户端类时,配置类LettuceConnectionConfiguration就会生效,因此不想用想要用jedis的话需要把lettuce的依赖去掉

这两个BEAN的配置类分别为org.springframework.boot.autoconfigure.data.redis.JedisConnectionConfiguration和org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration
,有兴趣可以去看看

不知在该类中是否有JedisConnectionFactory变量,其实如果你使用了@Component注解了之后,你可以直接在该类中直接使用@AutoWired直接注入

@Component
public class RedisCacheTransfer {
    @Autowired
    private JedisConnectionFactory jedisConnectionFactory
    
}

这样写不是更加优美?
如果你想将该类注册到MybatisRedisCache中进行设置,则你可以在你的conifg文件中,当SpringBoot启动的时候在Mybatis配置方法中加入方法即可

新手上路,请多包涵

这个问题解决了吗?我也遇到这个问题了--萌新一位

去掉jedis,2.0默认用的是lettuce连接Redis

新手上路,请多包涵

错误提示注入的时候找不着类型为org.springframework.data.redis.connection.jedis.JedisConnectionFactory的bean,
在你@Configuration配置类里面,用@Bean自己先注册一个这样的bean

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Microsoft
子站问答
访问
宣传栏