{"id":100223,"date":"2026-09-04T07:34:04","date_gmt":"2026-09-03T23:34:04","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/100223.html"},"modified":"2026-09-04T07:34:04","modified_gmt":"2026-09-03T23:34:04","slug":"%e5%a4%a7%e5%ad%a6%e7%94%9f%e5%b0%8f%e8%82%a0%e5%ad%a6%e4%b9%a0%ef%bc%9aspring%e7%af%87%ef%bc%88iocbeanaop%e4%ba%8b%e5%8a%a1%ef%bc%89%e5%86%85%e7%bd%ae%e5%b8%b8%e7%94%a8%e9%9d%a2%e8%af%95%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/100223.html","title":{"rendered":"\u5927\u5b66\u751f\u5c0f\u80a0\u5b66\u4e60\uff1aSpring\u7bc7\uff08IOC+bean+AOP+\u4e8b\u52a1\uff09\u5185\u7f6e\u5e38\u7528\u9762\u8bd5\u9898\u6574\u7406"},"content":{"rendered":"<p>\u00a0 \u4eca\u5929\u5b66\u4e60\u4e86Spring\u7684\u76f8\u5173\u77e5\u8bc6&#xff0c;IOC&#xff0c;bean&#xff0c;AOP&#xff0c;\u4e8b\u52a1&#xff0c;\u63a5\u4e0b\u6765\u6211\u5c06\u5c06\u6211\u7684\u5b66\u4e60\u8fc7\u7a0b\u8bb0\u5f55\u4e0b\u6765&#xff0c;\u5206\u4eab\u5230CSDN&#xff0c;\u505a\u4e00\u4e2a\u77e5\u8bc6\u70b9\u6c47\u603b&#xff0c;\u91cc\u9762\u5305\u62ec\u4e86\u4e00\u4e9b\u6211\u81ea\u5df1\u7684\u60f3\u6cd5&#xff0c;\u5982\u679c\u6709\u95ee\u9898\u8bf7\u6307\u6b63\u6211&#xff01;<\/p>\n<\/p>\n<h4>\u4e00.SpringIOC<\/h4>\n<p>\u5728\u4f20\u7edf\u5f00\u53d1\u4e2d&#xff0c;\u521b\u5efa\u5bf9\u8c61\u5fc5\u987b\u8981\u624b\u52a8new\u5bf9\u8c61&#xff0c;\u5bfc\u81f4\u4ee3\u7801\u7684\u8026\u5408\u5ea6\u662f\u5f88\u9ad8\u7684\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u5bf9\u8c61\u4e0e\u5bf9\u8c61\u4e4b\u95f4\u7684\u8c03\u7528\u662f\u975e\u5e38\u9ad8\u7684&#xff0c;\u6211\u4eec\u4e0d\u53ef\u80fd\u6bcf\u6b21\u8c03\u7528\u5bf9\u8c61\u90fd\u91cd\u65b0new\u4e00\u4e0b&#xff0c;\u4f8b\u5982&#xff1a;controller&#xff0c;service&#xff0c;dao\u8fd9\u4e9b\u7c7b\u90fd\u662f\u76f8\u4e92\u4f9d\u8d56&#xff0c;\u76f8\u4e92\u5173\u8054\u3002IOC\u901a\u8fc7\u63a7\u5236\u53cd\u8f6c&#xff0c;\u5bf9\u8c61\u521b\u5efa\u6743\u9650\u4ea4\u7ed9Spring\u5bb9\u5668&#xff0c;\u5bb9\u5668\u521d\u59cb\u5316\u65f6\u521b\u5efa\u5bf9\u8c61\u5e76\u7ef4\u62a4\u5bf9\u8c61\u95f4\u7684\u76f8\u4e92\u4f9d\u8d56&#xff0c;\u4f7f\u7528\u65f6\u76f4\u63a5\u4ece\u5bb9\u5668\u4e2d\u83b7\u53d6&#xff0c;\u5b9e\u73b0\u4ee3\u7801\u7684\u89e3\u8026\u3002<\/p>\n<p><span style=\"color:#fe2c24\">\u521d\u59cb\u5316\u662f\u4ec0\u4e48&#xff1a;\u5728SpringIOC\u5bb9\u5668\u4e2d&#xff0c;\u201c\u542f\u52a8\u5e76\u505a\u597d\u51c6\u5907\u5de5\u4f5c\u201d\u7684\u8fc7\u7a0b\u3002<\/span><\/p>\n<p><span style=\"background-color:#ffd900\">\u5173\u952e\u8bcd&#xff1a;new\u5bf9\u8c61&#xff0c;\u8026\u5408\u6027\u9ad8\u3002\u63a7\u5236\u53cd\u8f6c&#xff0c;\u5bf9\u8c61\u521b\u5efa\u4ea4\u7ed9ioc\u5bb9\u5668&#xff0c;\u521d\u59cb\u5316&#xff08;\u521b\u5efa\u5bf9\u8c61&#043;\u7ef4\u62a4\u5bf9\u8c61\u95f4\u4f9d\u8d56&#xff09;&#xff0c;\u89e3\u8026\u3002<\/span><\/p>\n<\/p>\n<h4>\u4e8c.bean<\/h4>\n<p>1&#xff0c;Spring\u4e2d\u4ec0\u4e48\u662fbean&#xff1f;Spring\u4e2d\u4ec0\u4e48\u662fIOC\u5bb9\u5668&#xff1f;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"219\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260903233403-6a9a03eb1d988.png\" width=\"754\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e0a\u9762\u8bf4\u4e86&#xff0c;IOC\u901a\u8fc7\u63a7\u5236\u53cd\u8f6c&#xff0c;\u5bf9\u8c61\u521b\u5efa\u6743\u4ea4\u7ed9IOC\u5bb9\u5668&#xff0c;\u5bb9\u5668\u521d\u59cb\u5316\u521b\u5efa\u5bf9\u8c61&#xff0c;\u7ef4\u62a4\u4f9d\u8d56\u3002<\/p>\n<p>\u6240\u4ee5IOC\u5bb9\u5668\u662f\u8d1f\u8d23\u7ba1\u7406bean\u5b9a\u4e49\u52a0\u8f7d&#xff0c;\u4f5c\u7528&#xff1a;\u5b9e\u4f8b\u5316&#xff0c;\u521b\u5efa\u5bf9\u8c61&#xff0c;\u6ce8\u5165\u4f9d\u8d56&#xff0c;\u751f\u547d\u5468\u671f\u63a7\u5236\u3002<\/p>\n<p>Bean\u662fIOC\u5bb9\u5668\u7ba1\u7406\u7684\u5bf9\u8c61&#xff0c;\u7531\u5bb9\u5668\u521b\u5efa&#043;\u6ce8\u5165\u4f9d\u8d56&#043;\u7ba1\u7406\u751f\u547d\u5468\u671f\u3002<\/p>\n<\/p>\n<p><span style=\"color:#0d0016\"><span style=\"background-color:#ffd7b9\">\u9762\u8bd5\u98981.spring\u6846\u67b6\u4e2d\u7684\u5355\u4f8bbean\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u5417&#xff1f;<\/span><\/span><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168\u7684&#xff0c;Spring\u6846\u67b6\u6709\u4e00\u4e2a&#064;Scope\u6ce8\u89e3&#xff0c;\u9ed8\u8ba4\u503c\u662fsingleton\u5355\u4f8b&#xff0c;\u56e0\u4e3a\u4e00\u822c\u5728Spring\u7684bean\u662f\u6ce8\u5165\u65e0\u72b6\u6001\u5bf9\u8c61\u3002\u6ca1\u6709\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898&#xff0c;\u5982\u679c\u5728bean\u4e2d\u5b9a\u4e49\u4e86\u53ef\u4fee\u6539\u7684\u6210\u5458\u53d8\u91cf&#xff0c;\u8981\u8003\u8651\u7ebf\u7a0b\u5b89\u5168&#xff0c;\u53ef\u4f7f\u7528\u591a\u4f8b\u6216\u52a0\u9501\u89e3\u51b3\u3002<\/p>\n<p>&#064;Service<br \/>\n&#064;Scope&#xff08;\u201csingleton\u201d&#xff09;  \/\/bean\u5728\u6bcf\u4e2aSpringIOC\u5bb9\u5668\u4e2d\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b<br \/>\n                       \/\/prototype:\u4e00\u4e2abean\u53ef\u4ee5\u5b9a\u4e49\u591a\u4e2a\u5b9e\u4f8b<\/p>\n<p>&#064;controller<br \/>\n&#064;RequestMapping(&#034;\/user&#034;)<br \/>\npublic class userController{<br \/>\n    private int count;  \/\/\u5b9e\u4f8b\u6210\u5458\u53d8\u91cf<br \/>\n    &#064;Autowired<br \/>\n    private UserService userservice; \/\/\u5bf9\u8c61\u5f15\u7528<br \/>\n}<\/p>\n<p>\u7ed3\u8bba&#xff1a;<span style=\"color:#1c7331\">Spring\u9ed8\u8ba4\u5355\u4f8bbean\u672c\u8eab\u4e0d\u4fdd\u8bc1\u7ebf\u7a0b\u5b89\u5168&#xff0c;\u7ebf\u7a0b\u5b89\u5168\u4e0e\u5426\u770bBean\u662f\u4e0d\u662f\u65e0\u72b6\u6001\u7684\u3002<\/span><\/p>\n<p>\u4ec0\u4e48\u662f\u65e0\u72b6\u6001\u7684\u5bf9\u8c61&#xff1f;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8fd9\u4e2a\u7c7b\u4e2d\u6ca1\u6709\u6210\u5458\u53d8\u91cf&#xff0c;\u5bf9\u8c61\u4e0d\u4f1a\u4fdd\u5b58\u4efb\u4f55\u4e1a\u52a1\u6570\u636e&#xff0c;\u6240\u6709\u6570\u636e\u90fd\u662f\u65b9\u6cd5\u8c03\u7528\u7684\u65f6\u5019\u901a\u8fc7\u53c2\u6570\u4f20\u8fdb\u6765&#xff0c;\u7528\u5b8c\u5c31\u6254\u3002<\/p>\n<p>\u65e0\u72b6\u6001\u4ee3\u7801\u5982\u4e0b&#xff1a;<\/p>\n<p>&#064;Service<br \/>\npublic class UserService {<br \/>\n    \/\/ \u6ca1\u6709\u5199 private User user; \u8fd9\u79cd\u6210\u5458\u53d8\u91cf<\/p>\n<p>    public void queryUser(Long userId){<br \/>\n        \/\/ userId\u662f\u8c03\u7528\u65b9\u6cd5\u4f20\u8fc7\u6765\u7684\u5c40\u90e8\u53d8\u91cf&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u8c03\u7528\u90fd\u4f1a\u81ea\u5df1\u751f\u6210\u4e00\u4efd&#xff0c;\u4e92\u4e0d\u5e72\u6270<br \/>\n    }<br \/>\n}<\/p>\n<p>\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u8c03\u7528queryUser&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u53d8\u91cf\u90fd\u5728\u81ea\u5df1\u6808\u4e2d&#xff0c;\u4e0d\u4f1a\u76f8\u4e92\u7be1\u6539\u6570\u636e&#xff0c;\u8fd9\u4e2a\u65f6\u5019\u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<\/p>\n<p><span style=\"background-color:#c7e6ea\">\u7ebf\u7a0b\u4e0d\u5b89\u5168\u600e\u4e48\u89e3\u51b3*2\u65b9\u6cd5<\/span><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01.\u6539\u6210\u591a\u4f8b&#064;Scope(&#034;prototype&#034;)&#xff0c;\u6bcf\u6b21\u8bf7\u6c42\u90fd\u65b0\u5efaBean\u5bf9\u8c61&#xff0c;\u6bcf\u4e2a\u7ebf\u7a0b\u7528\u81ea\u5df1\u7684\u5bf9\u8c61\u3002\u76f8\u4e92\u4e0d\u5e72\u6270&#xff0c;\u5f00\u9500\u4f1a\u6bd4\u8f83\u5927\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a02.\u52a0\u9501synchronized&#xff1a;\u540c\u4e00\u65f6\u95f4\u53ea\u5141\u8bb8\u4e00\u4e2a\u7ebf\u7a0b\u64cd\u4f5c\u5171\u4eab\u6210\u5458\u53d8\u91cf&#xff0c;\u5e76\u53d1\u6027\u80fd\u4e0b\u964d\u3002<\/p>\n<\/p>\n<h4>\u4e09.AOP<\/h4>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0springAOP\u662f<span style=\"color:#0d0016\"><span style=\"background-color:#fff5e6\">\u9762\u5411\u5207\u9762\u7f16\u7a0b<\/span><\/span>&#xff0c;\u5e95\u5c42\u662f\u57fa\u4e8e\u52a8\u6001\u4ee3\u7406\u5b9e\u73b0\u7684&#xff0c;\u521b\u5efa\u4e00\u4e2a\u5207\u9762\u7c7b&#xff0c;\u7528\u4e8e\u5b58\u653e\u62bd\u53d6\u51fa\u6765\u65e5\u5fd7&#xff0c;\u4e8b\u52a1\u7b49\u4e0e\u4e1a\u52a1\u65e0\u5173\u7684<span style=\"color:#be191c\">\u6a2a\u5207\u903b\u8f91<\/span>\u3002\u901a\u8fc7&#064;Aspect&#xff08;\u6807\u8bb0\u5207\u9762&#xff09;&#043;\u4e94\u5927\u901a\u77e5&#8212;-&gt;\u5b9a\u4e49\u589e\u5f3a\u65f6\u673a\u3002\u5229\u7528\u52a8\u6001\u4ee3\u7406\u5728\u76ee\u6807\u65b9\u6cd5\u6267\u884c\u524d\u540e\u63d2\u5165\u6a2a\u5207\u903b\u8f91&#xff0c;\u5b9e\u73b0\u4ee3\u7801\u7684\u590d\u7528\u548c\u89e3\u8026\u3002<\/p>\n<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"302\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260903233403-6a9a03eb2d2f2.png\" width=\"737\" \/><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e94\u5927\u901a\u77e5\u4f5c\u7528\u662f\u544a\u8bc9Spring\u4ec0\u4e48\u65f6\u5019\u628a\u54ea\u4e2a\u6a2a\u5207\u903b\u8f91\u201c\u690d\u5165\u201d\u65b9\u6cd5\u4e2d\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0<span style=\"color:#0d0016\">\u00a0\u00a0\u00a0\u00a0<span style=\"background-color:#ff9900\">\u6838\u5fc3&#061;AOP\u73af\u7ed5\u901a\u77e5&#043;\u5207\u70b9\u8868\u8fbe\u5f0f<\/span><\/span><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u901a\u8fc7\u73af\u7ed5\u901a\u77e5\u7684\u53c2\u6570\u83b7\u53d6\u8bf7\u6c42\u7684\u53c2\u6570&#xff08;\u7c7b&#xff0c;\u65b9\u6cd5&#xff0c;\u6ce8\u89e3&#xff0c;\u8bf7\u6c42\u65b9\u5f0f&#xff09;\u83b7\u53d6\u6210\u529f\u540e\u4fdd\u5b58\u6570\u636e\u5e93\u3002<\/p>\n<p>\/**<br \/>\n * AOP\u65e5\u5fd7\u5207\u9762<br \/>\n * &#064;Aspect \u6807\u8bb0\u8fd9\u662f\u5207\u9762\u7c7b<br \/>\n * &#064;Component \u4ea4\u7ed9Spring\u7ba1\u7406<br \/>\n *\/<br \/>\n&#064;Aspect<br \/>\n&#064;Component<br \/>\npublic class LogAspect {<\/p>\n<p>    \/\/ \u5207\u70b9\u8868\u8fbe\u5f0f&#xff1a;\u62e6\u622a\u81ea\u5df1\u9879\u76eecontroller\u4e0b\u6240\u6709\u65b9\u6cd5&#xff0c;\u6839\u636e\u4f60\u7684\u5305\u8def\u5f84\u4fee\u6539<br \/>\n    &#064;Pointcut(&#034;execution(* com.example.demo.controller.*.*(..))&#034;)<br \/>\n    public void pointCut(){}<\/p>\n<p>    \/**<br \/>\n     * \u73af\u7ed5\u901a\u77e5&#xff1a;\u529f\u80fd\u6700\u5f3a&#xff0c;\u53ef\u4ee5\u5728\u76ee\u6807\u65b9\u6cd5\u524d\u540e\u6267\u884c&#xff0c;\u8fd8\u80fd\u63a7\u5236\u662f\u5426\u6267\u884c\u76ee\u6807\u65b9\u6cd5<br \/>\n     * ProceedingJoinPoint&#xff1a;\u53ea\u80fd\u73af\u7ed5\u901a\u77e5\u4f7f\u7528&#xff0c;\u53ef\u4ee5\u6267\u884c\u76ee\u6807\u65b9\u6cd5 proceed()<br \/>\n     *\/<br \/>\n    &#064;Around(&#034;pointCut()&#034;)<br \/>\n    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {<\/p>\n<p>        \/\/ 1. \u83b7\u53d6\u76ee\u6807\u7c7b<br \/>\n        Class&lt;?&gt; targetClass &#061; joinPoint.getTarget().getClass();<br \/>\n        String className &#061; targetClass.getName();<\/p>\n<p>        \/\/ 2. \u83b7\u53d6\u65b9\u6cd5\u7b7e\u540d\u3001\u65b9\u6cd5\u5bf9\u8c61<br \/>\n        MethodSignature signature &#061; (MethodSignature) joinPoint.getSignature();<br \/>\n        Method method &#061; signature.getMethod();<br \/>\n        String methodName &#061; method.getName();<\/p>\n<p>        \/\/ 3. \u83b7\u53d6\u65b9\u6cd5\u5165\u53c2<br \/>\n        Object[] args &#061; joinPoint.getArgs();<br \/>\n        String params &#061; Arrays.toString(args);<\/p>\n<p>        \/\/ 4. \u83b7\u53d6\u65b9\u6cd5\u4e0a\u6240\u6709\u6ce8\u89e3<br \/>\n        Annotation[] annotations &#061; method.getAnnotations();<\/p>\n<p>        System.out.println(&#034;\u3010AOP\u5207\u9762\u3011\u7c7b\u540d&#xff1a;&#034; &#043; className);<br \/>\n        System.out.println(&#034;\u3010AOP\u5207\u9762\u3011\u65b9\u6cd5\u540d&#xff1a;&#034; &#043; methodName);<br \/>\n        System.out.println(&#034;\u3010AOP\u5207\u9762\u3011\u8bf7\u6c42\u53c2\u6570&#xff1a;&#034; &#043; params);<br \/>\n        System.out.println(&#034;\u3010AOP\u5207\u9762\u3011\u65b9\u6cd5\u6ce8\u89e3&#xff1a;&#034; &#043; Arrays.toString(annotations));<\/p>\n<p>        long start &#061; System.currentTimeMillis();<br \/>\n        Object result;<br \/>\n        try {<br \/>\n            \/\/ \u6267\u884c\u76ee\u6807\u65b9\u6cd5&#xff0c;\u8fd9\u884c\u5c31\u662f\u8c03\u7528\u6211\u4eec\u5199\u7684controller\/service\u4e1a\u52a1\u65b9\u6cd5<br \/>\n            result &#061; joinPoint.proceed();<br \/>\n        } catch (Throwable e) {<br \/>\n            \/\/ \u8bb0\u5f55\u5f02\u5e38\u65e5\u5fd7<br \/>\n            System.out.println(&#034;\u65b9\u6cd5\u6267\u884c\u5f02\u5e38&#xff1a;&#034; &#043; e.getMessage());<br \/>\n            throw e;<br \/>\n        }<br \/>\n        long costTime &#061; System.currentTimeMillis() &#8211; start;<\/p>\n<p>        System.out.println(&#034;\u3010AOP\u5207\u9762\u3011\u8fd4\u56de\u7ed3\u679c&#xff1a;&#034; &#043; result);<br \/>\n        System.out.println(&#034;\u3010AOP\u5207\u9762\u3011\u6267\u884c\u8017\u65f6&#xff1a;&#034; &#043; costTime &#043; &#034; ms&#034;);<\/p>\n<p>\u6ce8\u610f&#xff1a;SpringBoot \u5fc5\u987b\u52a0&#064;Component\u628a\u5207\u9762\u4ea4\u7ed9\u5bb9\u5668&#xff0c;\u5426\u5219\u5207\u9762\u4e0d\u751f\u6548\u3002<\/p>\n<\/p>\n<h4>\u56db.\u4e8b\u52a1<\/h4>\n<p><span style=\"background-color:#ffd900\">\u4e8b\u52a1\u5c31\u662f\u8981\u4e48\u5168\u90e8\u6210\u529f&#xff0c;\u8981\u4e0d\u6570\u636e\u5c31\u5168\u90e8\u56de\u6eda\u3002<\/span><\/p>\n<p>\u76f8\u5173\u6ce8\u89e3&#xff1a;&#064;Transactional&#xff08;\u653e\u5728service\u5c42\u4e1a\u52a1\u4e0a&#xff09;<\/p>\n<p>\u4e8b\u52a1\u5206\u4e3a\u7f16\u7a0b\u5f0f\u4e8b\u52a1&#043;\u58f0\u660e\u5f0f\u4e8b\u52a1<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a01.\u7f16\u7a0b\u5f0f\u4e8b\u52a1&#xff1a;\u5177\u6709\u5165\u4fb5\u6027&#xff0c;\u53ea\u662f\u5728service\u7684\u4e1a\u52a1\u65b9\u6cd5\u4e0a\u5199\u4e8b\u52a1\u4ee3\u7801&#xff0c;\u7528TransactionTemplate\u628a\u4e1a\u52a1\u903b\u8f91\u8dd1\u8d77\u6765&#xff08;\u4e8b\u52a1\u4ee3\u7801&#043;\u4e1a\u52a1\u4ee3\u7801&#xff09;&#xff0c;\u6240\u4ee5\u4fee\u6539\u4e1a\u52a1\u4ee3\u7801\u65f6\u5019\u8fd8\u8981\u4fee\u6539\u4e1a\u52a1\u4ee3\u7801&#xff0c;\u6240\u4ee5\u5efa\u8bae\u4f7f\u7528\u58f0\u660e\u5f0f\u4e8b\u52a1<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a02.\u58f0\u660e\u5f0f\u4e8b\u52a1&#xff1a;\u58f0\u660e\u5f0f\u4e8b\u52a1\u5c31\u662f\u57fa\u4e8eAOP&#xff0c;\u901a\u8fc7&#064;Transactional\u6ce8\u89e3&#xff0c;\u8ba9Spring\u81ea\u52a8\u7528\u4e8e\u5207\u9762\u7ec7\u5165\u4e8b\u52a1\u903b\u8f91\u3002<\/p>\n<\/p>\n<p><span style=\"color:#0d0016\"><span style=\"background-color:#faa572\">\u9762\u8bd5\u9898&#xff1a;Spring\u4e2d\u4e8b\u52a1\u662f\u5982\u4f55\u5b9e\u73b0\u7684&#xff1f;<\/span><\/span><\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6211\u4eec\u4f7f\u7528\u58f0\u660e\u5f0f\u4e8b\u52a1&#xff0c;\u901a\u8fc7AOP\u529f\u80fd&#xff0c;\u5e95\u5c42\u4f9d\u9760AOP\u52a8\u6001\u4ee3\u7406\u6280\u672f&#xff0c;\u524d\u540e\u8fdb\u884c\u62e6\u622a&#xff0c;\u6267\u884c\u65b9\u6cd5\u524d\u5f00\u542f\u4e8b\u52a1&#xff0c;\u6267\u884c\u65b9\u6cd5\u540e\u6267\u884c\u60c5\u51b5\u63d0\u4ea4\u6216\u56de\u6eda\u4e8b\u52a1\u3002\u4e0b\u9762\u4ee3\u7801\u662f\u6211\u641c\u8c46\u5305\u7684&#xff0c;\u6211\u89c9\u5f97\u53ef\u4ee5\u770b\u61c2\u3002<\/p>\n<p>\u4e0b\u9762\u4ee3\u7801\u662f\u4e8b\u52a1\u4ee3\u7801&#xff1a;<\/p>\n<p>import org.springframework.stereotype.Service;<br \/>\nimport org.springframework.transaction.annotation.Transactional;<\/p>\n<p>&#064;Service<br \/>\npublic class AccountService {<\/p>\n<p>    \/\/ \u52a0\u4e0a\u6ce8\u89e3&#xff1a;\u5f53\u524d\u65b9\u6cd5\u53d7Spring\u4e8b\u52a1\u7ba1\u7406<br \/>\n    &#064;Transactional<br \/>\n    public void transferMoney() {<br \/>\n        \/\/ \u4e1a\u52a11&#xff1a;A\u6263\u94b1<br \/>\n        deductMoneyA();<\/p>\n<p>        \/\/ \u6a21\u62df\u51fa\u9519&#xff01;\u629b\u51fa\u8fd0\u884c\u65f6\u5f02\u5e38&#xff0c;\u4e8b\u52a1\u89e6\u53d1\u56de\u6eda&#xff0c;A\u6263\u94b1\u64cd\u4f5c\u4f1a\u64a4\u9500<br \/>\n        int i &#061; 1 \/ 0;<\/p>\n<p>        \/\/ \u4e1a\u52a12&#xff1a;B\u52a0\u94b1<br \/>\n        addMoneyB();<br \/>\n    }<\/p>\n<p>    private void deductMoneyA() {<br \/>\n        System.out.println(&#034;A\u8d26\u6237\u6263\u94b1&#034;);<br \/>\n    }<br \/>\n    private void addMoneyB() {<br \/>\n        System.out.println(&#034;B\u8d26\u6237\u52a0\u94b1&#034;);<br \/>\n    }<br \/>\n}<\/p>\n<h3>\u5e95\u5c42\u4f2a\u4ee3\u7801&#xff08;\u5e2e\u52a9\u7406\u89e3 AOP \u4ee3\u7406\u505a\u4e86\u4ec0\u4e48&#xff0c;\u4e0d\u662f\u5b9e\u9645\u9879\u76ee\u4ee3\u7801&#xff09;<\/h3>\n<p>\/\/ AOP\u4ee3\u7406\u589e\u5f3a\u903b\u8f91&#xff08;Spring\u5e95\u5c42\u81ea\u52a8\u751f\u6210&#xff09;<br \/>\npublic void transferMoneyProxy(){<br \/>\n    try{<br \/>\n        \/\/ 1.\u5f00\u542f\u4e8b\u52a1<br \/>\n        beginTransaction();<\/p>\n<p>        \/\/ \u6267\u884c\u771f\u5b9e\u4e1a\u52a1\u65b9\u6cd5 transferMoney()<br \/>\n        realObject.transferMoney();<\/p>\n<p>        \/\/ \u65e0\u5f02\u5e38&#xff1a;\u63d0\u4ea4\u4e8b\u52a1<br \/>\n        commitTransaction();<br \/>\n    }catch (RuntimeException e){<br \/>\n        \/\/ \u51fa\u73b0\u8fd0\u884c\u65f6\u5f02\u5e38&#xff1a;\u56de\u6eda<br \/>\n        rollbackTransaction();<br \/>\n        throw e;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u6838\u5fc3&#xff1a;Spring \u58f0\u660e\u5f0f\u4e8b\u52a1\u5e95\u5c42\u57fa\u4e8e AOP \u52a8\u6001\u4ee3\u7406<\/p>\n<li>\u7ed9\u52a0\u4e86&#064;Transactional\u7684\u7c7b \/ \u65b9\u6cd5\u751f\u6210\u4ee3\u7406\u5bf9\u8c61<\/li>\n<li>\u4ee3\u7406\u62e6\u622a\u76ee\u6807\u65b9\u6cd5&#xff1a;\u65b9\u6cd5\u6267\u884c\u524d\u5f00\u542f\u4e8b\u52a1<\/li>\n<li>\u65b9\u6cd5\u6b63\u5e38\u8dd1\u5b8c \u2192 \u63d0\u4ea4\u4e8b\u52a1<\/li>\n<li>\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38 \u2192 \u56de\u6eda\u4e8b\u52a1<\/li>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u5b66\u4e60\u4e86Spring\u7684\u76f8\u5173\u77e5\u8bc6&#xff0c;IOC&#xff0c;bean&#xff0c;AOP&#xff0c;\u4e8b\u52a1&#xff0c;\u63a5\u4e0b\u6765\u6211\u5c06\u5c06\u6211\u7684\u5b66\u4e60\u8fc7\u7a0b\u8bb0\u5f55\u4e0b\u6765&#xff0c;\u5206\u4eab\u5230CSDN&#xff0c;\u505a\u4e00\u4e2a\u77e5\u8bc6\u70b9\u6c47\u603b&#xff0c;\u91cc\u9762\u5305\u62ec\u4e86\u4e00\u4e9b\u6211\u81ea\u5df1\u7684\u60f3\u6cd5&#xff0c;\u5982\u679c\u6709\u95ee\u9898\u8bf7\u6307\u6b63\u6211&#xff01;\u4e00.SpringIOC\u5728\u4f20\u7edf\u5f00\u53d1\u4e2d&#xff0c;\u521b\u5efa\u5bf9\u8c61\u5fc5\u987b\u8981\u624b\u52a8new\u5bf9\u8c61&#xff0c;\u5bfc\u81f4\u4ee3\u7801\u7684\u8026\u5408\u5ea6\u662f\u5f88\u9ad8\u7684\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u5bf9\u8c61\u4e0e\u5bf9\u8c61\u4e4b\u95f4\u7684\u8c03\u7528\u662f\u975e\u5e38<\/p>\n","protected":false},"author":2,"featured_media":100221,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[11468,5220,99,101,371],"topic":[],"class_list":["post-100223","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-11468","tag-ioc","tag-java","tag-spring","tag-371"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u5927\u5b66\u751f\u5c0f\u80a0\u5b66\u4e60\uff1aSpring\u7bc7\uff08IOC+bean+AOP+\u4e8b\u52a1\uff09\u5185\u7f6e\u5e38\u7528\u9762\u8bd5\u9898\u6574\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/100223.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u5927\u5b66\u751f\u5c0f\u80a0\u5b66\u4e60\uff1aSpring\u7bc7\uff08IOC+bean+AOP+\u4e8b\u52a1\uff09\u5185\u7f6e\u5e38\u7528\u9762\u8bd5\u9898\u6574\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4eca\u5929\u5b66\u4e60\u4e86Spring\u7684\u76f8\u5173\u77e5\u8bc6&#xff0c;IOC&#xff0c;bean&#xff0c;AOP&#xff0c;\u4e8b\u52a1&#xff0c;\u63a5\u4e0b\u6765\u6211\u5c06\u5c06\u6211\u7684\u5b66\u4e60\u8fc7\u7a0b\u8bb0\u5f55\u4e0b\u6765&#xff0c;\u5206\u4eab\u5230CSDN&#xff0c;\u505a\u4e00\u4e2a\u77e5\u8bc6\u70b9\u6c47\u603b&#xff0c;\u91cc\u9762\u5305\u62ec\u4e86\u4e00\u4e9b\u6211\u81ea\u5df1\u7684\u60f3\u6cd5&#xff0c;\u5982\u679c\u6709\u95ee\u9898\u8bf7\u6307\u6b63\u6211&#xff01;\u4e00.SpringIOC\u5728\u4f20\u7edf\u5f00\u53d1\u4e2d&#xff0c;\u521b\u5efa\u5bf9\u8c61\u5fc5\u987b\u8981\u624b\u52a8new\u5bf9\u8c61&#xff0c;\u5bfc\u81f4\u4ee3\u7801\u7684\u8026\u5408\u5ea6\u662f\u5f88\u9ad8\u7684\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u5bf9\u8c61\u4e0e\u5bf9\u8c61\u4e4b\u95f4\u7684\u8c03\u7528\u662f\u975e\u5e38\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/100223.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-03T23:34:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260903233403-6a9a03eb1d988.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/100223.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/100223.html\",\"name\":\"\u5927\u5b66\u751f\u5c0f\u80a0\u5b66\u4e60\uff1aSpring\u7bc7\uff08IOC+bean+AOP+\u4e8b\u52a1\uff09\u5185\u7f6e\u5e38\u7528\u9762\u8bd5\u9898\u6574\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-03T23:34:04+00:00\",\"dateModified\":\"2026-09-03T23:34:04+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/100223.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/100223.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/100223.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u5927\u5b66\u751f\u5c0f\u80a0\u5b66\u4e60\uff1aSpring\u7bc7\uff08IOC+bean+AOP+\u4e8b\u52a1\uff09\u5185\u7f6e\u5e38\u7528\u9762\u8bd5\u9898\u6574\u7406\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u5927\u5b66\u751f\u5c0f\u80a0\u5b66\u4e60\uff1aSpring\u7bc7\uff08IOC+bean+AOP+\u4e8b\u52a1\uff09\u5185\u7f6e\u5e38\u7528\u9762\u8bd5\u9898\u6574\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/100223.html","og_locale":"zh_CN","og_type":"article","og_title":"\u5927\u5b66\u751f\u5c0f\u80a0\u5b66\u4e60\uff1aSpring\u7bc7\uff08IOC+bean+AOP+\u4e8b\u52a1\uff09\u5185\u7f6e\u5e38\u7528\u9762\u8bd5\u9898\u6574\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4eca\u5929\u5b66\u4e60\u4e86Spring\u7684\u76f8\u5173\u77e5\u8bc6&#xff0c;IOC&#xff0c;bean&#xff0c;AOP&#xff0c;\u4e8b\u52a1&#xff0c;\u63a5\u4e0b\u6765\u6211\u5c06\u5c06\u6211\u7684\u5b66\u4e60\u8fc7\u7a0b\u8bb0\u5f55\u4e0b\u6765&#xff0c;\u5206\u4eab\u5230CSDN&#xff0c;\u505a\u4e00\u4e2a\u77e5\u8bc6\u70b9\u6c47\u603b&#xff0c;\u91cc\u9762\u5305\u62ec\u4e86\u4e00\u4e9b\u6211\u81ea\u5df1\u7684\u60f3\u6cd5&#xff0c;\u5982\u679c\u6709\u95ee\u9898\u8bf7\u6307\u6b63\u6211&#xff01;\u4e00.SpringIOC\u5728\u4f20\u7edf\u5f00\u53d1\u4e2d&#xff0c;\u521b\u5efa\u5bf9\u8c61\u5fc5\u987b\u8981\u624b\u52a8new\u5bf9\u8c61&#xff0c;\u5bfc\u81f4\u4ee3\u7801\u7684\u8026\u5408\u5ea6\u662f\u5f88\u9ad8\u7684\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u5bf9\u8c61\u4e0e\u5bf9\u8c61\u4e4b\u95f4\u7684\u8c03\u7528\u662f\u975e\u5e38","og_url":"https:\/\/www.wsisp.com\/helps\/100223.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-03T23:34:04+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260903233403-6a9a03eb1d988.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/100223.html","url":"https:\/\/www.wsisp.com\/helps\/100223.html","name":"\u5927\u5b66\u751f\u5c0f\u80a0\u5b66\u4e60\uff1aSpring\u7bc7\uff08IOC+bean+AOP+\u4e8b\u52a1\uff09\u5185\u7f6e\u5e38\u7528\u9762\u8bd5\u9898\u6574\u7406 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-03T23:34:04+00:00","dateModified":"2026-09-03T23:34:04+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/100223.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/100223.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/100223.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u5927\u5b66\u751f\u5c0f\u80a0\u5b66\u4e60\uff1aSpring\u7bc7\uff08IOC+bean+AOP+\u4e8b\u52a1\uff09\u5185\u7f6e\u5e38\u7528\u9762\u8bd5\u9898\u6574\u7406"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/100223","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=100223"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/100223\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/100221"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=100223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=100223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=100223"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=100223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}