{"id":5158,"date":"2021-02-04T17:51:59","date_gmt":"2021-02-04T09:51:59","guid":{"rendered":"https:\/\/www.xttblog.com\/?p=5158"},"modified":"2021-02-04T17:51:59","modified_gmt":"2021-02-04T09:51:59","slug":"rediscachemanager-nested-exception-is-java-lang-illegalargumentexception-ttl-duration-must-not-be-null","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=5158","title":{"rendered":"redisCacheManager nested exception is java.lang.IllegalArgumentException: TTL duration must not be null!"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u6700\u8fd1\u5728 SpringBoot 1.x \u5347\u7ea7\u5230 2.x \u4e0a\u9047\u5230\u4e86\u4e0d\u5c11\u5f02\u5e38\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9879\u76ee\u8d8a\u5927\uff0c\u9879\u76ee\u8d8a\u8001\uff0c\u5347\u7ea7 SpringBoot \u5e26\u6765\u7684\u98ce\u9669\u4e5f\u8d8a\u5927\u3002\u6211\u5728\u6574\u4e2a\u5347\u7ea7\u7684\u8fc7\u7a0b\u4e2d\u5df2\u7ecf\u9047\u5230\u4e86\u5341\u51e0\u4e2a\u51b2\u7a81\uff0c\u62a5\u9519\uff0c\u7f16\u8bd1\u5f02\u5e38\u7b49\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u552f\u72ec\uff0cnested exception is java.lang.IllegalArgumentException: TTL duration must not be null! \u8fd9\u4e2a\u95ee\u9898\uff0c\u6ca1\u767e\u5ea6\u5230\u7b54\u6848\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u5f97\u5df2\uff0c\u53ea\u80fd\u4f7f\u7528\u6211\u7684\u8c37\u6b4c\uff0c\u6709\u51e0\u7bc7\u82f1\u6587\u8bb0\u5f55\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u8fd9\u91cc\u6574\u7406\u8bb0\u5f55\u4e00\u4e0b\uff0c\u65b9\u4fbf\u5176\u4ed6\u7f51\u53cb\u6392\u9519\uff01<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">CacheManager \u5728 SpringBoot 1.x \u4e2d\u7684\u7528\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Bean\npublic CacheManager redisCacheManager(RedisTemplate&lt;?, ?&gt; redisTemplate) {\n    CacheManager cacheManager = new RedisCacheManager(redisTemplate);\n    return cacheManager;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u7528\u6cd5\u5728 SpringBoot 2.x \u4e2d\u5df2\u7ecf\u65e0\u6cd5\u4f7f\u7528\u4e86\u3002SpringBoot 2.x \u7528\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Bean\npublic CacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {\n    RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()\n            .entryTtl(Duration.ofSeconds(null));\n    return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))\n            .cacheDefaults(redisCacheConfiguration).build();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u95ee\u9898\u5c31\u4ea7\u751f\u5728 entryTtl(null) \u4e0a\u3002\u5b8c\u6574\u7684\u5f02\u5e38\u4fe1\u606f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'redisCacheManager' defined in class path resource [com\/xttblog\/config\/RedisCacheConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.cache.CacheManager]: Factory method 'redisCacheManager' threw exception; nested exception is java.lang.IllegalArgumentException: TTL duration must not be null!<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u89e3\u51b3\u529e\u6cd5\u5c31\u662f\u8bbe\u7f6e entryTtl\uff0centryTtl() \u65b9\u6cd5\u4e2d\u6709\u4e00\u4e2a\u5224\u65ad\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Assert.notNull(ttl, \"TTL duration must not be null!\"); <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0b\u9762\u7ed9\u51fa\u4e00\u4e2a\u6b63\u786e\u7528\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@Bean\n    public CacheManager redisCacheManager(RedisConnectionFactory redisConnectionFactory) {\n        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()\n                .entryTtl(Duration.ofSeconds(3600));\n        return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))\n                .cacheDefaults(redisCacheConfiguration).build();\n    }<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6700\u7ec8\u5b8c\u7f8e\u89e3\u51b3\uff01<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=5158\">redisCacheManager nested exception is java.lang.IllegalArgumentException: TTL duration must not be null!<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728 SpringBoot 1.x \u5347\u7ea7\u5230 2.x \u4e0a\u9047\u5230\u4e86\u4e0d\u5c11\u5f02\u5e38\u3002 \u9879\u76ee\u8d8a\u5927\uff0c\u9879\u76ee\u8d8a\u8001\uff0c\u5347\u7ea7 Sprin [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[645,266,414,946,906],"class_list":["post-5158","post","type-post","status-publish","format-standard","hentry","category-java","tag-gitbook","tag-java","tag-spring","tag-946","tag-906"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5158"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5158\/revisions"}],"predecessor-version":[{"id":5159,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/5158\/revisions\/5159"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}