{"id":342,"date":"2025-09-03T18:31:22","date_gmt":"2025-09-03T10:31:22","guid":{"rendered":"https:\/\/heyingnian.com\/?p=342"},"modified":"2025-09-05T11:26:01","modified_gmt":"2025-09-05T03:26:01","slug":"spring-cache","status":"publish","type":"post","link":"https:\/\/heyingnian.com\/index.php\/2025\/09\/03\/spring-cache\/","title":{"rendered":"Spring \u4e3a\u4ec0\u4e48\u9700\u8981\u4e09\u7ea7\u7f13\u5b58"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">\ud83d\udd0d Spring \u4e3a\u4ec0\u4e48\u9700\u8981\u4e09\u7ea7\u7f13\u5b58\uff1f<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u2014\u2014 \u4e00\u6587\u8bb2\u6e05\u5faa\u73af\u4f9d\u8d56\u4e0e AOP \u7684\u4e00\u81f4\u6027\u96be\u9898<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u4f5c\u8005\uff1a<\/strong> \u8d6b\u82f1\u5e74<br><strong>\u5173\u952e\u8bcd\uff1a<\/strong> Spring\u3001\u4e09\u7ea7\u7f13\u5b58\u3001\u5faa\u73af\u4f9d\u8d56\u3001AOP\u3001\u4ee3\u7406\u3001\u5bf9\u8c61\u4e00\u81f4\u6027<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf30 \u4e00\u3001\u95ee\u9898\u573a\u666f<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>@Service\npublic class UserService {\n    @Autowired\n    private OrderService orderService;\n\n    @Transactional \/\/ \u9700\u8981\u4e8b\u52a1\u63a7\u5236 \u2192 \u5fc5\u987b\u901a\u8fc7\u4ee3\u7406\u6267\u884c\n    public void createUserOrder() {\n        orderService.createOrder();\n    }\n}\n\n@Service\npublic class OrderService {\n    @Autowired\n    private UserService userService; \/\/ \u5faa\u73af\u4f9d\u8d56\n}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b58\u5728\u5faa\u73af\u4f9d\u8d56 \u2705<\/li>\n\n\n\n<li><code>UserService<\/code>&nbsp;\u6807\u6709&nbsp;<code>@Transactional<\/code>&nbsp;\u2705<\/li>\n\n\n\n<li>Spring \u6b63\u5e38\u542f\u52a8\uff0c\u4f46\u4e8b\u52a1\u53ef\u80fd<strong>\u4e0d\u751f\u6548<\/strong><\/li>\n<\/ul>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83e\uddf1 \u4e8c\u3001\u4e09\u7ea7\u7f13\u5b58\u662f\u54ea\u4e09\u7ea7\uff1f<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7f13\u5b58<\/th><th>\u4f5c\u7528<\/th><\/tr><\/thead><tbody><tr><td>\u4e00\u7ea7\uff1a<code>singletonObjects<\/code><\/td><td>\u5b58<strong>\u6700\u7ec8\u6210\u54c1 Bean<\/strong><\/td><\/tr><tr><td>\u4e8c\u7ea7\uff1a<code>earlySingletonObjects<\/code><\/td><td>\u5b58<strong>\u65e9\u671f\u5f15\u7528<\/strong>\uff08\u539f\u59cb\u6216\u4ee3\u7406\uff09<\/td><\/tr><tr><td>\u4e09\u7ea7\uff1a<code>singletonFactories<\/code><\/td><td>\u5b58&nbsp;<code>ObjectFactory<\/code>\uff0c<strong>\u5ef6\u8fdf\u521b\u5efa<\/strong>\u65e9\u671f\u5f15\u7528<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u2705 \u4e09\u7ea7\u7f13\u5b58\u5b58\u7684\u4e0d\u662f\u5bf9\u8c61\uff0c\u800c\u662f\u4e00\u4e2a\u5de5\u5382\uff1a<br><code>() -&gt; getEarlyBeanReference(bean)<\/code><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udeab \u4e09\u3001\u5982\u679c\u53ea\u6709\u4e8c\u7ea7\u7f13\u5b58\uff08\u5b58\u539f\u59cb\u5bf9\u8c61\uff09<\/h2>\n\n\n\n<p>\u5047\u8bbe\u6d41\u7a0b\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u521b\u5efa&nbsp;<code>UserService<\/code>&nbsp;\u2192 \u5b9e\u4f8b\u5316 \u2192 \u653e\u5165<strong>\u4e8c\u7ea7\u7f13\u5b58<\/strong>\uff08\u539f\u59cb\u5bf9\u8c61\uff09<\/li>\n\n\n\n<li><code>OrderService<\/code>&nbsp;\u4f9d\u8d56&nbsp;<code>UserService<\/code>&nbsp;\u2192 \u4ece\u4e8c\u7ea7\u7f13\u5b58\u62ff\u5230<strong>\u539f\u59cb\u5bf9\u8c61<\/strong><\/li>\n\n\n\n<li><code>OrderService<\/code>&nbsp;\u6301\u6709\u539f\u59cb&nbsp;<code>UserService<\/code><\/li>\n\n\n\n<li><code>UserService<\/code>&nbsp;\u7ee7\u7eed\u521d\u59cb\u5316 \u2192&nbsp;<code>BeanPostProcessor<\/code>&nbsp;\u521b\u5efa<strong>\u4ee3\u7406\u5bf9\u8c61<\/strong><\/li>\n\n\n\n<li>\u4ee3\u7406\u5bf9\u8c61\u653e\u5165<strong>\u4e00\u7ea7\u7f13\u5b58<\/strong><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\ud83d\udca1 \u4ec0\u4e48\u662f\u201c\u4ee3\u7406\u5bf9\u8c61\u201d\uff1f\u5b83\u548c\u201c\u539f\u59cb\u5bf9\u8c61\u201d\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u539f\u59cb\u5bf9\u8c61<\/strong>\uff1a\u5c31\u662f&nbsp;<code>new UserService()<\/code>&nbsp;\u5f97\u5230\u7684\u5b9e\u4f8b\uff0c<strong>\u6ca1\u6709\u4efb\u4f55\u589e\u5f3a<\/strong>\u3002<\/li>\n\n\n\n<li><strong>\u4ee3\u7406\u5bf9\u8c61<\/strong>\uff1aSpring \u901a\u8fc7 AOP\uff08\u5982 CGLIB \u6216 JDK \u52a8\u6001\u4ee3\u7406\uff09\u751f\u6210\u7684\u201c\u5305\u88c5\u5bf9\u8c61\u201d\uff0c\u5b83\uff1a\n<ul class=\"wp-block-list\">\n<li>\u6301\u6709\u539f\u59cb\u5bf9\u8c61<\/li>\n\n\n\n<li>\u5728\u8c03\u7528\u65b9\u6cd5\u524d\/\u540e<strong>\u81ea\u52a8\u7ec7\u5165\u903b\u8f91<\/strong>\uff0c\u6bd4\u5982\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5f00\u542f\u4e8b\u52a1\uff08<code>@Transactional<\/code>\uff09<\/li>\n\n\n\n<li>\u7f13\u5b58\u8bfb\u5199\uff08<code>@Cacheable<\/code>\uff09<\/li>\n\n\n\n<li>\u65e5\u5fd7\u8bb0\u5f55\u3001\u6743\u9650\u6821\u9a8c\u7b49<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udd11 <strong>\u5173\u952e\uff1a\u53ea\u6709\u901a\u8fc7\u4ee3\u7406\u5bf9\u8c61\u8c03\u7528\u65b9\u6cd5\uff0c\u8fd9\u4e9b\u589e\u5f3a\u903b\u8f91\u624d\u4f1a\u751f\u6548\uff01<\/strong><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u26a0\ufe0f \u95ee\u9898\u7206\u53d1\uff1a\u5bf9\u8c61\u201c\u5206\u88c2\u201d\uff01<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7ec4\u4ef6<\/th><th>\u6301\u6709\u5b9e\u4f8b<\/th><th>\u7c7b\u578b<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>OrderService<\/code><\/td><td>\u539f\u59cb&nbsp;<code>UserService<\/code><\/td><td>\u274c \u65e0\u4ee3\u7406<\/td><td>\u8c03\u7528\u65b9\u6cd5 \u2192&nbsp;<strong>\u76f4\u63a5\u6267\u884c\uff0c\u4e0d\u8d70\u4ee3\u7406<\/strong><\/td><\/tr><tr><td>\u5176\u4ed6\u7ec4\u4ef6<\/td><td>\u4ee3\u7406&nbsp;<code>UserService<\/code><\/td><td>\u2705 \u6709\u4e8b\u52a1<\/td><td>\u8c03\u7528\u65b9\u6cd5 \u2192&nbsp;<strong>\u5148\u5f00\u542f\u4e8b\u52a1\uff0c\u518d\u6267\u884c<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83d\udd34 <strong>\u540e\u679c\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>OrderService<\/code>&nbsp;\u8c03\u7528&nbsp;<code>userService.createUserOrder()<\/code><br>\u2192 \u8c03\u7684\u662f<strong>\u539f\u59cb\u5bf9\u8c61\u7684\u65b9\u6cd5<\/strong><br>\u2192&nbsp;<strong>\u4e0d\u5f00\u542f\u4e8b\u52a1<\/strong>&nbsp;\u2192 \u5373\u4f7f\u629b\u5f02\u5e38\uff0c\u6570\u636e\u5e93\u64cd\u4f5c\u4e5f\u4e0d\u4f1a\u56de\u6eda\uff01<\/li>\n\n\n\n<li>\u540c\u4e00\u4e2a Bean \u6709\u4e24\u4e2a\u7248\u672c \u2192&nbsp;<strong>\u8fdd\u53cd\u5355\u4f8b\u539f\u5219\uff0c\u7cfb\u7edf\u884c\u4e3a\u4e0d\u4e00\u81f4<\/strong><\/li>\n<\/ul>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 \u56db\u3001Spring \u7684\u6b63\u786e\u89e3\uff1a\u4e09\u7ea7\u7f13\u5b58\u5ef6\u8fdf\u51b3\u7b56<\/h2>\n\n\n\n<p>Spring \u7684\u7cbe\u5999\u8bbe\u8ba1\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u521b\u5efa&nbsp;<code>UserService<\/code>&nbsp;\u2192 \u5b9e\u4f8b\u5316<\/li>\n\n\n\n<li>\u5c06\u00a0<code>ObjectFactory<\/code>\u00a0\u653e\u5165<strong>\u4e09\u7ea7\u7f13\u5b58<\/strong>\uff1a<code>() -> getEarlyBeanReference(userService)<\/code><\/li>\n\n\n\n<li><code>OrderService<\/code>&nbsp;\u9700\u8981&nbsp;<code>UserService<\/code>&nbsp;\u2192 \u8c03\u7528\u5de5\u5382\u7684&nbsp;<code>getObject()<\/code><\/li>\n\n\n\n<li>\u6b64\u65f6\u6267\u884c&nbsp;<code>getEarlyBeanReference()<\/code>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5224\u65ad\u662f\u5426\u6709&nbsp;<code>@Transactional<\/code><\/li>\n\n\n\n<li><strong>\u9700\u8981 \u2192 \u521b\u5efa\u4ee3\u7406<\/strong><\/li>\n\n\n\n<li>\u653e\u5165<strong>\u4e8c\u7ea7\u7f13\u5b58<\/strong><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><code>OrderService<\/code>&nbsp;\u6301\u6709\u7684\u662f<strong>\u4ee3\u7406\u5bf9\u8c61<\/strong><\/li>\n\n\n\n<li><code>UserService<\/code>&nbsp;\u540e\u7eed\u521d\u59cb\u5316\u5b8c\u6210 \u2192 \u76f4\u63a5\u4f7f\u7528\u5df2\u6709\u4ee3\u7406 \u2192 \u653e\u5165\u4e00\u7ea7\u7f13\u5b58<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u2705 \u6700\u7ec8\u72b6\u6001\uff1a\u5bf9\u8c61\u552f\u4e00\uff0c\u4ee3\u7406\u4e00\u81f4<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7ec4\u4ef6<\/th><th>\u6301\u6709\u5b9e\u4f8b<\/th><th>\u7c7b\u578b<\/th><\/tr><\/thead><tbody><tr><td><code>OrderService<\/code><\/td><td>\u4ee3\u7406&nbsp;<code>UserService<\/code><\/td><td>\u2705<\/td><\/tr><tr><td>\u5176\u4ed6\u7ec4\u4ef6<\/td><td>\u4ee3\u7406&nbsp;<code>UserService<\/code><\/td><td>\u2705<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\ud83c\udfaf <strong>\u65e0\u8bba\u8c01\u8c03\u7528\uff0c\u90fd\u8d70\u4ee3\u7406 \u2192 \u4e8b\u52a1\u751f\u6548<\/strong><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83c\udf1f \u4e94\u3001\u6838\u5fc3\u7ed3\u8bba<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u95ee\u9898<\/th><th>\u7b54\u6848<\/th><\/tr><\/thead><tbody><tr><td><strong>\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u53ea\u7528\u4e8c\u7ea7\u7f13\u5b58\uff1f<\/strong><\/td><td>\u4f1a\u5b58\u539f\u59cb\u5bf9\u8c61\uff0c\u5bfc\u81f4 AOP \u5931\u6548<\/td><\/tr><tr><td><strong>\u4e3a\u4ec0\u4e48\u9700\u8981\u4e09\u7ea7\u7f13\u5b58\uff1f<\/strong><\/td><td>\u901a\u8fc7&nbsp;<code>ObjectFactory<\/code>&nbsp;<strong>\u5ef6\u8fdf\u51b3\u7b56<\/strong>\uff0c\u6309\u9700\u521b\u5efa\u4ee3\u7406<\/td><\/tr><tr><td><strong>\u4e09\u7ea7\u7f13\u5b58\u7684\u672c\u8d28\uff1f<\/strong><\/td><td>\u89e3\u51b3\u201c\u63d0\u524d\u66b4\u9732\u201d\u4e0e\u201cAOP\u4e00\u81f4\u6027\u201d\u7684\u77db\u76fe<\/td><\/tr><tr><td><strong>\u6700\u7ec8\u76ee\u6807\uff1f<\/strong><\/td><td><strong>\u4fdd\u8bc1\u6574\u4e2a\u5bb9\u5668\u4e2d\u53ea\u6709\u4e00\u4e2a Bean \u5b9e\u4f8b\uff08\u4e14\u6b63\u786e\uff09<\/strong><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udca1 \u516d\u3001\u4e00\u53e5\u8bdd\u603b\u7ed3<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p><strong>\u5982\u679c\u6ca1\u6709\u4e09\u7ea7\u7f13\u5b58\u7684\u201c\u5ef6\u8fdf\u4ee3\u7406\u201d\u673a\u5236\uff0c\u4e8c\u7ea7\u7f13\u5b58\u53ea\u80fd\u5b58\u539f\u59cb\u5bf9\u8c61\uff0c\u5c31\u4f1a\u5bfc\u81f4\u201c\u4e00\u4e2a Bean \u6709\u4e24\u4e2a\u7248\u672c\uff08\u539f\u59cb + \u4ee3\u7406\uff09\u201d\uff0c\u9020\u6210 AOP \u5931\u6548\u3002\u4e09\u7ea7\u7f13\u5b58\u901a\u8fc7 ObjectFactory \u5ef6\u8fdf\u51b3\u7b56\uff0c\u786e\u4fdd\u4e86\u4ee3\u7406\u7684\u6b63\u786e\u6027\u548c\u5bf9\u8c61\u7684\u552f\u4e00\u6027\u3002<\/strong><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcda \u53c2\u8003\u6e90\u7801<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ SmartInstantiationAwareBeanPostProcessor.java\ndefault Object getEarlyBeanReference(Object bean, String beanName) {\n    return bean;\n}<\/code><\/pre>\n\n\n\n<p>AOP \u6a21\u5757\uff08\u5982 <code>AnnotationAwareAspectJAutoProxyCreator<\/code>\uff09\u4f1a\u91cd\u5199\u6b64\u65b9\u6cd5\uff0c\u5728\u9700\u8981\u65f6\u8fd4\u56de\u4ee3\u7406\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u2705 <strong>\u9002\u5408\u573a\u666f\uff1a<\/strong> \u6280\u672f\u77ed\u5206\u4eab\u3001\u56e2\u961f\u5468\u4f1a\u3001\u9762\u8bd5\u590d\u76d8<br>\u2705 <strong>\u7279\u70b9\uff1a<\/strong> \u91cd\u70b9\u7a81\u51fa\u3001\u903b\u8f91\u6e05\u6670\u3001<strong>\u6982\u5ff5\u660e\u786e<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udd0d Spring \u4e3a\u4ec0\u4e48\u9700\u8981\u4e09\u7ea7\u7f13\u5b58\uff1f \u2014\u2014 \u4e00\u6587\u8bb2\u6e05\u5faa\u73af\u4f9d\u8d56\u4e0e AOP \u7684\u4e00\u81f4\u6027\u96be\u9898 \u4f5c\u8005\uff1a \u8d6b\u82f1\u5e74\u5173\u952e\u8bcd\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[52,53],"class_list":["post-342","post","type-post","status-publish","format-standard","hentry","category-daily","tag-spring","tag-daily"],"_links":{"self":[{"href":"https:\/\/heyingnian.com\/index.php\/wp-json\/wp\/v2\/posts\/342","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/heyingnian.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/heyingnian.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/heyingnian.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/heyingnian.com\/index.php\/wp-json\/wp\/v2\/comments?post=342"}],"version-history":[{"count":2,"href":"https:\/\/heyingnian.com\/index.php\/wp-json\/wp\/v2\/posts\/342\/revisions"}],"predecessor-version":[{"id":368,"href":"https:\/\/heyingnian.com\/index.php\/wp-json\/wp\/v2\/posts\/342\/revisions\/368"}],"wp:attachment":[{"href":"https:\/\/heyingnian.com\/index.php\/wp-json\/wp\/v2\/media?parent=342"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/heyingnian.com\/index.php\/wp-json\/wp\/v2\/categories?post=342"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/heyingnian.com\/index.php\/wp-json\/wp\/v2\/tags?post=342"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}