{"id":4311,"date":"2019-02-14T09:34:04","date_gmt":"2019-02-14T01:34:04","guid":{"rendered":"https:\/\/xinchewhd.com.cn\/index.php\/spring\/%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3spring%e4%b8%a4%e5%a4%a7%e7%89%b9%e6%80%a7%ef%bc%9aioc%e5%92%8caop\/"},"modified":"2019-02-14T09:34:04","modified_gmt":"2019-02-14T01:34:04","slug":"%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3spring%e4%b8%a4%e5%a4%a7%e7%89%b9%e6%80%a7%ef%bc%9aioc%e5%92%8caop","status":"publish","type":"post","link":"https:\/\/xinchewhd.com.cn\/index.php\/spring\/%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3spring%e4%b8%a4%e5%a4%a7%e7%89%b9%e6%80%a7%ef%bc%9aioc%e5%92%8caop\/","title":{"rendered":"\u6df1\u5165\u7406\u89e3Spring\u4e24\u5927\u7279\u6027\uff1aIoC\u548cAOP"},"content":{"rendered":"<div class='wxsyncmain'>\n<p style=\"text-indent:50px;\">\u4f17\u6240\u5468\u77e5\uff0cSpring\u62e5\u6709\u4e24\u5927\u7279\u6027\uff1aIoC\u548cAOP\u3002<strong>IoC<\/strong><strong>\uff0c\u82f1\u6587\u5168\u79f0<\/strong><strong>Inversion of Control<\/strong><strong>\uff0c\u610f\u4e3a\u63a7\u5236\u53cd\u8f6c\u3002<\/strong><strong>AOP<\/strong><strong>\uff0c\u82f1\u6587\u5168\u79f0<\/strong><strong>Aspect-Oriented Programming<\/strong><strong>\uff0c\u610f\u4e3a\u9762\u5411\u5207\u9762\u7f16\u7a0b<\/strong>\u3002<\/p>\n<p style=\"text-indent:50px;\">Spring\u6838\u5fc3\u5bb9\u5668\u7684\u4e3b\u8981\u7ec4\u4ef6\u662fBean\u5de5\u5382\uff08BeanFactory\uff09\uff0cBean\u5de5\u5382\u4f7f\u7528\u63a7\u5236\u53cd\u8f6c\uff08IoC\uff09\u6a21\u5f0f\u6765\u964d\u4f4e\u7a0b\u5e8f\u4ee3\u7801\u4e4b\u95f4\u7684\u8026\u5408\u5ea6\uff0c\u5e76\u63d0\u4f9b\u4e86\u9762\u5411\u5207\u9762\u7f16\u7a0b\uff08AOP\uff09\u7684\u5b9e\u73b0\u3002<\/p>\n<p style=\"text-indent:50px;\">\u7b80\u5355\u6765\u8bf4\uff0c<span style=\"color:#ff0000;\">Spring<\/span><span style=\"color:#ff0000;\">\u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684\u63a7\u5236\u53cd\u8f6c\uff08<\/span><span style=\"color:#ff0000;\">IoC<\/span><span style=\"color:#ff0000;\">\uff09\u548c\u9762\u5411\u5207\u9762\u7f16\u7a0b\uff08<\/span><span style=\"color:#ff0000;\">AOP<\/span><span style=\"color:#ff0000;\">\uff09\u7684\u5bb9\u5668\u6846\u67b6<\/span>\u3002<\/p>\n<p style=\"text-indent:50px;\">\u4e0b\u9762\uff0c\u6211\u4eec\u7b80\u8981\u8bf4\u660e\u4e0b\u8fd9\u4e24\u5927\u7279\u6027\u3002<\/p>\n<p style=\"text-indent:50px;\">\n<h1><span style=\"font-size: 20px;\"><strong>1. Spring\u5e38\u7528\u6ce8\u89e3<\/strong><\/span><\/h1>\n<p style=\"text-indent:50px;\">\u5728\u5177\u4f53\u4ecb\u7ecdIoC\u548cAOP\u4e4b\u524d\uff0c\u6211\u4eec\u5148\u7b80\u8981\u8bf4\u660e\u4e0bSpring\u5e38\u7528\u6ce8\u89e3<\/p>\n<p style=\"text-indent:50px;\">1\u3001<strong>@Controller<\/strong>\uff1a\u7528\u4e8e\u6807\u6ce8\u63a7\u5236\u5668\u5c42\u7ec4\u4ef6<\/p>\n<p style=\"text-indent:50px;\">2\u3001<strong>@Service<\/strong>\uff1a\u7528\u4e8e\u6807\u6ce8\u4e1a\u52a1\u5c42\u7ec4\u4ef6<\/p>\n<p style=\"text-indent:50px;\">3\u3001<strong>@Component<\/strong> : \u7528\u4e8e\u6807\u6ce8\u8fd9\u662f\u4e00\u4e2a\u53d7 Spring \u7ba1\u7406\u7684\u7ec4\u4ef6\uff0c\u7ec4\u4ef6\u5f15\u7528\u540d\u79f0\u662f\u7c7b\u540d\uff0c\u7b2c\u4e00\u4e2a\u5b57\u6bcd\u5c0f\u5199\u3002\u53ef\u4ee5\u4f7f\u7528@Component(\u201cname\u201d) \u6307\u5b9a\u7ec4\u4ef6\u7684\u540d\u79f0\u3002<\/p>\n<p style=\"text-indent:50px;\">4\u3001<strong>@Repository<\/strong>\uff1a\u7528\u4e8e\u6807\u6ce8\u6570\u636e\u8bbf\u95ee\u7ec4\u4ef6\uff0c\u5373DAO\u7ec4\u4ef6<\/p>\n<p style=\"text-indent:50px;\">5\u3001<strong>@Scope(\"prototype\")<\/strong>\uff1a\u5c06\u7ec4\u4ef6\u7684\u8303\u56f4\u8bbe\u7f6e\u4e3a\u539f\u578b\u7684\uff08\u5373\u591a\u4f8b\uff09\u3002\u4fdd\u8bc1\u6bcf\u4e00\u4e2a\u8bf7\u6c42\u6709\u4e00\u4e2a\u5355\u72ec\u7684action\u6765\u5904\u7406\uff0c\u907f\u514daction\u7684\u7ebf\u7a0b\u95ee\u9898\u3002<\/p>\n<p style=\"text-indent:50px;\">\u7531\u4e8eSpring\u9ed8\u8ba4\u662f\u5355\u4f8b\u7684\uff0c\u53ea\u4f1a\u521b\u5efa\u4e00\u4e2aaction\u5bf9\u8c61\uff0c\u6bcf\u6b21\u8bbf\u95ee\u90fd\u662f\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5bb9\u6613\u4ea7\u751f\u5e76\u53d1\u95ee\u9898\uff0c\u6570\u636e\u4e0d\u5b89\u5168\u3002<\/p>\n<p style=\"text-indent:50px;\">6\u3001<strong>@Autowired<\/strong>\uff1a\u9ed8\u8ba4\u6309\u7c7b\u578b\u8fdb\u884c\u81ea\u52a8\u88c5\u914d\u3002\u5728\u5bb9\u5668\u67e5\u627e\u5339\u914d\u7684Bean\uff0c\u5f53\u6709\u4e14\u4ec5\u6709\u4e00\u4e2a\u5339\u914d\u7684Bean\u65f6\uff0cSpring\u5c06\u5176\u6ce8\u5165@Autowired\u6807\u6ce8\u7684\u53d8\u91cf\u4e2d\u3002<\/p>\n<p style=\"text-indent:50px;\">7\u3001<strong>@Resource<\/strong>\uff1a\u9ed8\u8ba4\u6309\u540d\u79f0\u8fdb\u884c\u81ea\u52a8\u88c5\u914d\uff0c\u5f53\u627e\u4e0d\u5230\u4e0e\u540d\u79f0\u5339\u914d\u7684Bean\u65f6\u4f1a\u6309\u7c7b\u578b\u88c5\u914d\u3002<\/p>\n<p style=\"text-indent:50px;\">\n<h1><strong><span style=\"font-size: 20px;\">2. \u63a7\u5236\u53cd\u8f6c(IoC)<\/span><\/strong><\/h1>\n<p style=\"text-indent:50px;\"><span style=\"color:#f33b45;\">\u63a7\u5236\u53cd\u8f6c<\/span>\uff0c\u7b80\u5355\u70b9\u8bf4\uff0c<span style=\"color:#f33b45;\">\u5c31\u662f\u521b\u5efa\u5bf9\u8c61\u7684\u63a7\u5236\u6743\uff0c\u88ab\u53cd\u8f6c\u5230\u4e86Spring\u6846\u67b6\u4e0a\u3002<\/span><\/p>\n<p style=\"text-indent:50px;\">\u901a\u5e38\uff0c\u6211\u4eec\u5b9e\u4f8b\u5316\u4e00\u4e2a\u5bf9\u8c61\u65f6\uff0c\u90fd\u662f\u4f7f\u7528\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u6765new\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u7531\u6211\u4eec\u81ea\u5df1\u6765\u63a7\u5236\u7684\uff0c\u800c\u63a7\u5236\u53cd\u8f6c\u5c31\u628anew\u5bf9\u8c61\u7684\u5de5\u4ea4\u7ed9\u4e86Spring\u5bb9\u5668\u3002<\/p>\n<p style=\"text-indent:50px;\">\n<p style=\"text-indent:50px;\">\u300a<span style=\"color:#000000;\">expert ONE-ON-ONE J2EE Development without EJB<\/span>\u300b\u7b2c6\u7ae0\u4e2d\u6307\u51fa<\/p>\n<blockquote>\n<p>P128<\/p>\n<p style=\"text-indent:50px;\">IoC Implementation Strategies<\/p>\n<p style=\"text-indent:50px;\">IoC is a broad concept that can be implemented in different ways. There are two main types:<\/p>\n<p style=\"text-indent:50px;\">Dependency Lookup: The container provides callbacks to components, and a lookup context.This is the EJB and Apache Avalon approach. It leaves the onus on each component to use container APIs to look up resources and collaborators. The Inversion of Control is limited to the container invoking callback methods that application code can use to obtain resources.<\/p>\n<p style=\"text-indent:50px;\">Dependency Injection: Components do no look up; they provide plain Java methods enabling the container to resolve dependencies. The container is wholly responsible for wiring up components, passing resolved objects in to JavaBean properties or constructors. Use of JavaBean properties is called Setter Injection; use of constructor arguments is called Constructor Injection.<\/p>\n<p style=\"text-indent:50px;\">\n<p>P130<\/p>\n<p style=\"text-indent:50px;\">The second IoC strategy-Dependency Injection-is usually preferable.<\/p>\n<\/blockquote>\n<p style=\"text-indent:50px;\">\n<p style=\"text-indent:50px;\">\u4e3b\u8981\u610f\u601d\u4e3a\uff1a<\/p>\n<blockquote>\n<p style=\"text-indent:50px;\">IoC\u7684\u4e3b\u8981\u5b9e\u73b0\u65b9\u5f0f\u6709\u4e24\u79cd\uff1a\u4f9d\u8d56\u67e5\u627e\u3001\u4f9d\u8d56\u6ce8\u5165\u3002<\/p>\n<p style=\"text-indent:50px;\">\u4f9d\u8d56\u6ce8\u5165\u662f\u4e00\u79cd\u66f4\u53ef\u53d6\u7684\u65b9\u5f0f\u3002<\/p>\n<\/blockquote>\n<p style=\"text-indent:50px;\">\n<p style=\"text-indent:50px;\">\u90a3\u4e48\u4f9d\u8d56\u67e5\u627e\u548c\u4f9d\u8d56\u6ce8\u5165\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f<\/p>\n<p style=\"text-indent:50px;\">\u4f9d\u8d56\u67e5\u627e\uff0c\u4e3b\u8981\u662f\u5bb9\u5668\u4e3a\u7ec4\u4ef6\u63d0\u4f9b\u4e00\u4e2a\u56de\u8c03\u63a5\u53e3\u548c\u4e0a\u4e0b\u6587\u73af\u5883\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u7ec4\u4ef6\u5c31\u5fc5\u987b\u81ea\u5df1\u4f7f\u7528\u5bb9\u5668\u63d0\u4f9b\u7684API\u6765\u67e5\u627e\u8d44\u6e90\u548c\u534f\u4f5c\u5bf9\u8c61\uff0c\u63a7\u5236\u53cd\u8f6c\u4ec5\u4f53\u73b0\u5728\u90a3\u4e9b\u56de\u8c03\u65b9\u6cd5\u4e0a\uff0c\u5bb9\u5668\u8c03\u7528\u8fd9\u4e9b\u56de\u8c03\u65b9\u6cd5\uff0c\u4ece\u800c\u5e94\u7528\u4ee3\u7801\u83b7\u53d6\u5230\u8d44\u6e90\u3002<\/p>\n<p style=\"text-indent:50px;\">\u4f9d\u8d56\u6ce8\u5165\uff0c\u7ec4\u4ef6\u4e0d\u505a\u5b9a\u4f4d\u67e5\u8be2\uff0c\u53ea\u63d0\u4f9b\u6807\u51c6\u7684Java\u65b9\u6cd5\u8ba9\u5bb9\u5668\u53bb\u51b3\u5b9a\u4f9d\u8d56\u5173\u7cfb\u3002\u5bb9\u5668\u5168\u6743\u8d1f\u8d23\u7ec4\u4ef6\u7684\u88c5\u914d\uff0c\u628a\u7b26\u5408\u4f9d\u8d56\u5173\u7cfb\u7684\u5bf9\u8c61\u901a\u8fc7Java Bean\u5c5e\u6027\u6216\u6784\u9020\u65b9\u6cd5\u4f20\u9012\u7ed9\u9700\u8981\u7684\u5bf9\u8c61\u3002<\/p>\n<p style=\"text-indent:50px;\">\n<h2><span style=\"font-size: 18px;\"><strong>2.1 IoC\u5bb9\u5668<\/strong><\/span><\/h2>\n<p style=\"text-indent:50px;\">IoC\u5bb9\u5668\uff1a\u5177\u6709\u4f9d\u8d56\u6ce8\u5165\u529f\u80fd\u7684\u5bb9\u5668\uff0c\u53ef\u4ee5\u521b\u5efa\u5bf9\u8c61\u7684\u5bb9\u5668\u3002IoC\u5bb9\u5668\u8d1f\u8d23\u5b9e\u4f8b\u5316\u3001\u5b9a\u4f4d\u3001\u914d\u7f6e\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u5bf9\u8c61\u5e76\u5efa\u7acb\u8fd9\u4e9b\u5bf9\u8c61\u4e4b\u95f4\u7684\u4f9d\u8d56\u3002<\/p>\n<p style=\"text-indent:50px;\">\n<h2><strong><span style=\"font-size: 18px;\">2.2 \u4f9d\u8d56\u6ce8\u5165<\/span><\/strong><\/h2>\n<p style=\"text-indent:50px;\">DI\uff0c\u82f1\u6587\u5168\u79f0\uff0cDependency Injection\uff0c\u610f\u4e3a\u4f9d\u8d56\u6ce8\u5165\u3002<\/p>\n<p style=\"text-indent:50px;\"><span style=\"color:#f33b45;\">\u4f9d\u8d56\u6ce8\u5165<\/span>\uff1a\u7531IoC\u5bb9\u5668\u52a8\u6001\u5730\u5c06\u67d0\u4e2a\u5bf9\u8c61\u6240\u9700\u8981\u7684\u5916\u90e8\u8d44\u6e90\uff08\u5305\u62ec\u5bf9\u8c61\u3001\u8d44\u6e90\u3001\u5e38\u91cf\u6570\u636e\uff09\u6ce8\u5165\u5230\u7ec4\u4ef6(Controller, Service\u7b49\uff09\u4e4b\u4e2d\u3002\u7b80\u5355\u70b9\u8bf4\uff0c<span style=\"color:#f33b45;\">\u5c31\u662fIoC\u5bb9\u5668\u4f1a\u628a\u5f53\u524d\u5bf9\u8c61\u6240\u9700\u8981\u7684\u5916\u90e8\u8d44\u6e90\u52a8\u6001\u7684\u6ce8\u5165\u7ed9\u6211\u4eec<\/span>\u3002<\/p>\n<p style=\"text-indent:50px;\">\n<p style=\"text-indent:50px;\">Spring\u4f9d\u8d56\u6ce8\u5165\u7684\u65b9\u5f0f\u4e3b\u8981\u6709\u56db\u4e2a\uff0c\u57fa\u4e8e\u6ce8\u89e3\u6ce8\u5165\u65b9\u5f0f\u3001set\u6ce8\u5165\u65b9\u5f0f\u3001\u6784\u9020\u5668\u6ce8\u5165\u65b9\u5f0f\u3001\u9759\u6001\u5de5\u5382\u6ce8\u5165\u65b9\u5f0f\u3002\u63a8\u8350\u4f7f\u7528\u57fa\u4e8e\u6ce8\u89e3\u6ce8\u5165\u65b9\u5f0f\uff0c\u914d\u7f6e\u8f83\u5c11\uff0c\u6bd4\u8f83\u65b9\u4fbf\u3002<\/p>\n<p style=\"text-indent:50px;\">\n<p style=\"text-indent:50px;\"><span style=\"color:#f33b45;\"><strong>\u57fa\u4e8e\u6ce8\u89e3\u6ce8\u5165\u65b9\u5f0f<\/strong><\/span><\/p>\n<p style=\"text-indent:50px;\">\u670d\u52a1\u5c42\u4ee3\u7801<\/p>\n<p style=\"text-align:left;background:#2B2B2B;\"><span style=\"font-size:16px;color:#BBB529;\">@Service<\/span><span style=\"font-size:16px;color:#CC7832;\">public class <\/span><span style=\"font-size:16px;color:#A9B7C6;\">AdminService { <\/span><span style=\"font-size:16px;color:#6A8759;\">\/\/code<\/span><span style=\"font-size:16px;color:#A9B7C6;\">}<\/span><\/p>\n<p style=\"text-indent:50px;\">\u63a7\u5236\u5c42\u4ee3\u7801<\/p>\n<pre style=\"background-color: rgb(43, 43, 43);\"><span style=\"font-size:16px;color:#BBB529;\">@Controller@Scope<\/span><span style=\"font-size:16px;color:#A9B7C6;\">(<\/span><span style=\"font-size:16px;color:#6A8759;\">\"prototype\"<\/span><span style=\"font-size:16px;color:#A9B7C6;\">)<\/span><span style=\"font-size:16px;color:#CC7832;\">public class <\/span><span style=\"font-size:16px;color:#A9B7C6;\">AdminController {<\/span><span lang=\"EN-US\" style=\"\"> <\/span><span style=\"font-size:16px;color:#BBB529;\">@Autowired<\/span><span lang=\"EN-US\" style=\"\"> <\/span><span style=\"font-size:16px;color:#CC7832;\">private <\/span><span style=\"font-size:16px;color:#A9B7C6;\">AdminService <\/span><em><span style=\"font-size:16px;color:#9876AA;\">adminService<\/span><\/em><span style=\"font-size:16px;color:#CC7832;\">;<\/span><span lang=\"EN-US\" style=\"\"><\/span><span style=\"font-size:16px;color:#6A8759;\">\/\/code<\/span><span style=\"font-size:16px;color:#A9B7C6;\">}<\/span><\/pre>\n<p style=\"text-indent:50px;\">@Autowired\u4e0e@Resource\u90fd\u53ef\u4ee5\u7528\u6765\u88c5\u914dBean\uff0c\u90fd\u53ef\u4ee5\u5199\u5728\u5b57\u6bb5\u3001setter\u65b9\u6cd5\u4e0a\u3002\u4ed6\u4eec\u7684\u533a\u522b\u662f\uff1a<\/p>\n<p style=\"text-indent:50px;\"><span style=\"color:#f33b45;\">@Autowired\u9ed8\u8ba4\u6309\u7c7b\u578b\u8fdb\u884c\u81ea\u52a8\u88c5\u914d<\/span>\uff08\u8be5\u6ce8\u89e3\u5c5e\u4e8eSpring\uff09\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u8981\u6c42\u4f9d\u8d56\u5bf9\u8c61\u5fc5\u987b\u5b58\u5728\uff0c\u5982\u679c\u8981\u5141\u8bb8\u4e3anull\uff0c\u9700\u8bbe\u7f6erequired\u5c5e\u6027\u4e3afalse\uff0c\u4f8b\uff1a@Autowired(required=false)\u3002\u5982\u679c\u8981\u4f7f\u7528\u540d\u79f0\u8fdb\u884c\u88c5\u914d\uff0c\u53ef\u4ee5\u4e0e@Qualifier\u6ce8\u89e3\u4e00\u8d77\u4f7f\u7528\u3002<\/p>\n<pre style=\"background-color: rgb(43, 43, 43);\"><span style=\"font-size:16px;color:#BBB529;\">@Autowired@Qualifier<\/span><span style=\"font-size:16px;color:#A9B7C6;\">(<\/span><span style=\"font-size:16px;color:#6A8759;\">\"adminService\"<\/span><span style=\"font-size:16px;color:#A9B7C6;\">)<\/span><span style=\"font-size:16px;color:#CC7832;\">private <\/span><span style=\"font-size:16px;color:#A9B7C6;\">AdminService <\/span><em><span style=\"font-size:16px;color:#9876AA;\">adminService<\/span><\/em><span style=\"font-size:16px;color:#CC7832;\">;<\/span><\/pre>\n<p style=\"text-indent:50px;\"><span style=\"color:#f33b45;\"><\/span><\/p>\n<p style=\"text-indent:50px;\"><span style=\"color:#f33b45;\">@Resource\u9ed8\u8ba4\u6309\u7167\u540d\u79f0\u8fdb\u884c\u88c5\u914d<\/span>\uff08\u8be5\u6ce8\u89e3\u5c5e\u4e8eJ2EE\uff09\uff0c\u540d\u79f0\u53ef\u4ee5\u901a\u8fc7name\u5c5e\u6027\u6765\u6307\u5b9a\u3002\u5982\u679c\u6ca1\u6709\u6307\u5b9aname\u5c5e\u6027\uff0c\u5f53\u6ce8\u89e3\u5199\u5728\u5b57\u6bb5\u4e0a\u65f6\uff0c\u9ed8\u8ba4\u53d6\u5b57\u6bb5\u540d\u8fdb\u884c\u88c5\u914d\uff1b\u5982\u679c\u6ce8\u89e3\u5199\u5728setter\u65b9\u6cd5\u4e0a\uff0c\u9ed8\u8ba4\u53d6\u5c5e\u6027\u540d\u8fdb\u884c\u88c5\u914d\u3002\u5f53\u627e\u4e0d\u5230\u4e0e\u540d\u79f0\u76f8\u5339\u914d\u7684Bean\u65f6\uff0c\u4f1a\u6309\u7167\u7c7b\u578b\u8fdb\u884c\u88c5\u914d\u3002\u4f46\u662f\uff0cname\u5c5e\u6027\u4e00\u65e6\u6307\u5b9a\uff0c\u5c31\u53ea\u4f1a\u6309\u7167\u540d\u79f0\u8fdb\u884c\u88c5\u914d\u3002<\/p>\n<pre style=\"background-color: rgb(43, 43, 43);\"><span style=\"font-size:16px;color:#BBB529;\">@Resource<\/span><span style=\"font-size:16px;color:#A9B7C6;\">(name = <\/span><span style=\"font-size:16px;color:#6A8759;\">\"adminService\"<\/span><span lang=\"EN-US\" style=\"\">)<\/span><span style=\"font-size:16px;color:#CC7832;\">private <\/span><span style=\"font-size:16px;color:#A9B7C6;\">AdminService <\/span><em><span style=\"font-size:16px;color:#9876AA;\">adminService<\/span><\/em><span style=\"font-size:16px;color:#CC7832;\">;<\/span><\/pre>\n<p style=\"text-indent:32px;\">\u9664\u6b64\u4e4b\u5916\uff0c\u5bf9\u4e8e\u4e00\u4e9b\u590d\u6742\u7684\u88c5\u8f7dBean\u7684\u65f6\u673a\uff0c\u6bd4\u5982\u6211\u4eec\u9700\u8981\u6839\u636e\u914d\u7f6e\u88c5\u8f7d\u4e0d\u540c\u7684Bean\uff0c\u4ee5\u5b8c\u6210\u4e0d\u540c\u7684\u64cd\u4f5c\uff0c\u53ef\u4ee5\u4f7f\u7528getBean(\u201cbeanID\u201d)\u7684\u65b9\u5f0f\u6765\u52a0\u8f7dBean\u3002<\/p>\n<p style=\"text-indent:32px;\">\u901a\u8fc7BeanID\u52a0\u8f7dBean\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<p style=\"text-align:left;background:#2B2B2B;\"><span style=\"font-size:16px;color:#BBB529;\">@Component<\/span><span style=\"font-size:16px;color:#CC7832;\">public class <\/span><span style=\"font-size:16px;color:#A9B7C6;\">BeanUtils <\/span><span style=\"font-size:16px;color:#CC7832;\">implements <\/span><span style=\"font-size:16px;color:#A9B7C6;\">ApplicationContextAware { <\/span><span style=\"font-size:16px;color:#CC7832;\">private static <\/span><span style=\"font-size:16px;color:#A9B7C6;\">ApplicationContext <\/span><em><span style=\"font-size:16px;color:#9876AA;\">applicationContext<\/span><\/em><span style=\"font-size:16px;color:#CC7832;\">; <\/span><span style=\"font-size:16px;color:#BBB529;\">@Override <\/span><span style=\"font-size:16px;color:#CC7832;\">public void <\/span><span style=\"font-size:16px;color:#FFC66D;\">setApplicationContext<\/span><span style=\"font-size:16px;color:#A9B7C6;\">(ApplicationContext applicationContext){ <\/span><span style=\"font-size:16px;color:#CC7832;\">if <\/span><span style=\"font-size:16px;color:#A9B7C6;\">(BeanUtils.<\/span><em><span style=\"font-size:16px;color:#9876AA;\">applicationContext <\/span><\/em><span style=\"font-size:16px;color:#A9B7C6;\">== <\/span><span style=\"font-size:16px;color:#CC7832;\">null<\/span><span style=\"font-size:16px;color:#A9B7C6;\">) { BeanUtils.<\/span><em><span style=\"font-size:16px;color:#9876AA;\">applicationContext <\/span><\/em><span style=\"font-size:16px;color:#A9B7C6;\">= applicationContext<\/span><span style=\"font-size:16px;color:#CC7832;\">; <\/span><span style=\"font-size:16px;color:#A9B7C6;\">} }<\/span><em><span style=\"font-size:16px;color:#629755;\"> <\/span><\/em><span style=\"font-size:16px;color:#CC7832;\">public static <\/span><span style=\"font-size:16px;color:#A9B7C6;\">ApplicationContext <\/span><span style=\"font-size:16px;color:#FFC66D;\">getApplicationContext<\/span><span style=\"font-size:16px;color:#A9B7C6;\">() { <\/span><span style=\"font-size:16px;color:#CC7832;\">return <\/span><em><span style=\"font-size:16px;color:#9876AA;\">applicationContext<\/span><\/em><span style=\"font-size:16px;color:#CC7832;\">; <\/span><span style=\"font-size:16px;color:#A9B7C6;\">}<\/span><em><span style=\"font-size:16px;color:#629755;\"> <\/span><\/em><span style=\"font-size:16px;color:#CC7832;\">public static <\/span><span style=\"font-size:16px;color:#A9B7C6;\">Object <\/span><span style=\"font-size:16px;color:#FFC66D;\">getBean<\/span><span style=\"font-size:16px;color:#A9B7C6;\">(String id) <\/span><span style=\"font-size:16px;color:#CC7832;\">throws <\/span><span style=\"font-size:16px;color:#A9B7C6;\">Exception { <\/span><span style=\"font-size:16px;color:#CC7832;\">try <\/span><span style=\"font-size:16px;color:#A9B7C6;\">{ <\/span><span style=\"font-size:16px;color:#CC7832;\">return <\/span><em><span style=\"font-size:16px;color:#9876AA;\">applicationContext<\/span><\/em><span style=\"font-size:16px;color:#A9B7C6;\">.containsBean(id) ? <\/span><em><span style=\"font-size:16px;color:#9876AA;\">applicationContext<\/span><\/em><span style=\"font-size:16px;color:#A9B7C6;\">.getBean(id) : <\/span><span style=\"font-size:16px;color:#CC7832;\">null; <\/span><span style=\"font-size:16px;color:#A9B7C6;\">} <\/span><span style=\"font-size:16px;color:#CC7832;\">catch <\/span><span style=\"font-size:16px;color:#A9B7C6;\">(BeansException e) { e.printStackTrace()<\/span><span style=\"font-size:16px;color:#CC7832;\">; throw new <\/span><span style=\"font-size:16px;color:#A9B7C6;\">Exception(<\/span><span style=\"font-size:16px;color:#6A8759;\">\"not found bean id: \" <\/span><span style=\"font-size:16px;color:#A9B7C6;\">+ id)<\/span><span style=\"font-size:16px;color:#CC7832;\">; <\/span><span style=\"font-size:16px;color:#A9B7C6;\">} }}<\/span><\/p>\n<p style=\"text-indent:32px;\">\n<p style=\"text-indent:32px;\">\u6211\u4eec\u5728\u9700\u8981\u52a0\u8f7dBean\u7684\u5730\u65b9\u8c03\u7528\u8be5\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n<h1><span style=\"font-size: 20px;\"><\/span><\/h1>\n<p style=\"text-align:left;background:#2B2B2B;\"><span style=\"font-size:16px;color:#CC7832;\">public class <\/span><span style=\"font-size:16px;color:#A9B7C6;\">BaseController { <\/span><span style=\"font-size:16px;color:#CC7832;\">protected <\/span><span style=\"font-size:16px;color:#A9B7C6;\">IService <\/span><span style=\"font-size:16px;color:#FFC66D;\">loadService<\/span><span style=\"font-size:16px;color:#A9B7C6;\">(String id) <\/span><span style=\"font-size:16px;color:#CC7832;\">throws <\/span><span style=\"font-size:16px;color:#A9B7C6;\">Exception { IService iService = (IService)BeanUtils.<em>getBean<\/em>(id)<\/span><span style=\"font-size:16px;color:#CC7832;\">; if <\/span><span style=\"font-size:16px;color:#A9B7C6;\">(iService != <\/span><span style=\"font-size:16px;color:#CC7832;\">null<\/span><span style=\"font-size:16px;color:#A9B7C6;\">) { <\/span><span style=\"font-size:16px;color:#CC7832;\">return <\/span><span style=\"font-size:16px;color:#A9B7C6;\">iService<\/span><span style=\"font-size:16px;color:#CC7832;\">; <\/span><span style=\"font-size:16px;color:#A9B7C6;\">} <\/span><span style=\"font-size:16px;color:#CC7832;\">else <\/span><span style=\"font-size:16px;color:#A9B7C6;\">{ <\/span><span style=\"font-size:16px;color:#CC7832;\">throw new <\/span><span style=\"font-size:16px;color:#A9B7C6;\">Exception(<\/span><span style=\"font-size:16px;color:#6A8759;\">\"<\/span><span style=\"font-size:16px;font-family:\u5b8b\u4f53;color:#6A8759;\">\u52a0\u8f7d<\/span><span style=\"font-size:16px;color:#6A8759;\">Bean<\/span><span style=\"font-size:16px;font-family:\u5b8b\u4f53;color:#6A8759;\">\u9519\u8bef<\/span><span style=\"font-size:16px;color:#6A8759;\">\"<\/span><span style=\"font-size:16px;color:#A9B7C6;\">)<\/span><span style=\"font-size:16px;color:#CC7832;\">; <\/span><span style=\"font-size:16px;color:#A9B7C6;\">} }}<\/span><\/p>\n<h1><span style=\"font-size: 20px;\"><strong><\/strong><\/span><\/h1>\n<h1><span style=\"font-size: 20px;\"><strong>3. \u9762\u5411\u5207\u9762\u7f16\u7a0b(AOP)<\/strong><\/span><\/h1>\n<p style=\"text-indent:50px;\">\u9762\u5411\u5207\u9762\u7f16\u7a0b\uff08AOP\uff09\u5c31\u662f\u7eb5\u5411\u7684\u7f16\u7a0b\u3002\u6bd4\u5982\u4e1a\u52a1A\u548c\u4e1a\u52a1B\u73b0\u5728\u9700\u8981\u4e00\u4e2a\u76f8\u540c\u7684\u64cd\u4f5c\uff0c\u4f20\u7edf\u65b9\u6cd5\u6211\u4eec\u53ef\u80fd\u9700\u8981\u5728A\u3001B\u4e2d\u90fd\u52a0\u5165\u76f8\u5173\u64cd\u4f5c\u4ee3\u7801\uff0c\u800c\u5e94\u7528AOP\u5c31\u53ef\u4ee5\u53ea\u5199\u4e00\u904d\u4ee3\u7801\uff0cA\u3001B\u5171\u7528\u8fd9\u6bb5\u4ee3\u7801\u3002\u5e76\u4e14\uff0c\u5f53A\u3001B\u9700\u8981\u589e\u52a0\u65b0\u7684\u64cd\u4f5c\u65f6\uff0c\u53ef\u4ee5\u5728\u4e0d\u6539\u52a8\u539f\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u7075\u6d3b\u6dfb\u52a0\u65b0\u7684\u4e1a\u52a1\u903b\u8f91\u5b9e\u73b0\u3002<\/p>\n<p style=\"text-indent:50px;\">\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u6bd4\u5982\u5546\u54c1\u67e5\u8be2\u3001\u4fc3\u9500\u67e5\u8be2\u7b49\u4e1a\u52a1\uff0c\u90fd\u9700\u8981\u8bb0\u5f55\u65e5\u5fd7\u3001\u5f02\u5e38\u5904\u7406\u7b49\u64cd\u4f5c\uff0cAOP\u628a\u6240\u6709\u5171\u7528\u4ee3\u7801\u90fd\u5265\u79bb\u51fa\u6765\uff0c\u5355\u72ec\u653e\u7f6e\u5230\u67d0\u4e2a\u7c7b\u4e2d\u8fdb\u884c\u96c6\u4e2d\u7ba1\u7406\uff0c\u5728\u5177\u4f53\u8fd0\u884c\u65f6\uff0c\u7531\u5bb9\u5668\u8fdb\u884c\u52a8\u6001\u7ec7\u5165\u8fd9\u4e9b\u516c\u5171\u4ee3\u7801\u3002<\/p>\n<p style=\"text-indent:50px;\">AOP\u4e3b\u8981\u4e00\u822c\u5e94\u7528\u4e8e<span style=\"color:#f33b45;\">\u7b7e\u540d\u9a8c\u7b7e\u3001\u53c2\u6570\u6821\u9a8c\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u4e8b\u52a1\u63a7\u5236\u3001\u6743\u9650\u63a7\u5236\u3001\u6027\u80fd\u7edf\u8ba1\u3001\u5f02\u5e38\u5904\u7406<\/span>\u7b49\u3002<\/p>\n<h2><span style=\"font-size: 18px;\"><strong>3.1AOP\u6d89\u53ca\u540d\u8bcd<\/strong><\/span><\/h2>\n<p style=\"text-indent:50px;\"><strong>\u5207\u9762\uff08Aspect\uff09<\/strong>\uff1a\u5171\u6709\u529f\u80fd\u7684\u5b9e\u73b0\u3002\u5982\u65e5\u5fd7\u5207\u9762\u3001\u6743\u9650\u5207\u9762\u3001\u9a8c\u7b7e\u5207\u9762\u7b49\u3002<span style=\"color:#ff0000;\">\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u901a\u5e38\u662f\u4e00\u4e2a\u5b58\u653e\u5171\u6709\u529f\u80fd\u5b9e\u73b0\u7684\u6807\u51c6<\/span><span style=\"color:#ff0000;\">Java<\/span><span style=\"color:#ff0000;\">\u7c7b\u3002\u5f53<\/span><span style=\"color:#ff0000;\">Java<\/span><span style=\"color:#ff0000;\">\u7c7b\u4f7f\u7528\u4e86<\/span><span style=\"color:#ff0000;\">@Aspect<\/span><span style=\"color:#ff0000;\">\u6ce8\u89e3\u4fee\u9970\u65f6\uff0c\u5c31\u80fd\u88ab<\/span><span style=\"color:#ff0000;\">AOP<\/span><span style=\"color:#ff0000;\">\u5bb9\u5668\u8bc6\u522b\u4e3a\u5207\u9762\u3002<\/span><\/p>\n<p style=\"text-indent:50px;\"><strong>\u901a\u77e5\uff08Advice\uff09<\/strong>\uff1a\u5207\u9762\u7684\u5177\u4f53\u5b9e\u73b0\u3002\u5c31\u662f\u8981\u7ed9\u76ee\u6807\u5bf9\u8c61\u7ec7\u5165\u7684\u4e8b\u60c5\u3002\u4ee5\u76ee\u6807\u65b9\u6cd5\u4e3a\u53c2\u7167\u70b9\uff0c\u6839\u636e\u653e\u7f6e\u7684\u5730\u65b9\u4e0d\u540c\uff0c\u53ef\u5206\u4e3a\u524d\u7f6e\u901a\u77e5\uff08Before\uff09\u3001\u540e\u7f6e\u901a\u77e5\uff08AfterReturning\uff09\u3001\u5f02\u5e38\u901a\u77e5\uff08AfterThrowing\uff09\u3001\u6700\u7ec8\u901a\u77e5\uff08After\uff09\u4e0e\u73af\u7ed5\u901a\u77e5\uff08Around\uff095\u79cd\u3002<span style=\"color:#ff0000;\">\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u901a\u5e38\u662f\u5207\u9762\u7c7b\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u5177\u4f53\u5c5e\u4e8e\u54ea\u7c7b\u901a\u77e5\uff0c\u901a\u8fc7\u65b9\u6cd5\u4e0a\u7684\u6ce8\u89e3\u533a\u5206\u3002<\/span><\/p>\n<p style=\"text-indent:50px;\"><strong>\u8fde\u63a5\u70b9\uff08JoinPoint\uff09<\/strong>\uff1a\u7a0b\u5e8f\u5728\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u80fd\u591f\u63d2\u5165\u5207\u9762\u7684\u5730\u70b9\u3002\u4f8b\u5982\uff0c\u65b9\u6cd5\u8c03\u7528\u3001\u5f02\u5e38\u629b\u51fa\u7b49\u3002Spring\u53ea\u652f\u6301\u65b9\u6cd5\u7ea7\u7684\u8fde\u63a5\u70b9\u3002<span style=\"color:#ff0000;\">\u4e00\u4e2a\u7c7b\u7684\u6240\u6709\u65b9\u6cd5\u524d\u3001\u540e\u3001\u629b\u51fa\u5f02\u5e38\u65f6\u7b49\u90fd\u662f\u8fde\u63a5\u70b9\u3002<\/span><\/p>\n<p style=\"text-indent:50px;\"><strong>\u5207\u5165\u70b9\uff08Pointcut\uff09<\/strong>\uff1a\u7528\u4e8e\u5b9a\u4e49\u901a\u77e5\u5e94\u8be5\u5207\u5165\u5230\u54ea\u4e9b\u8fde\u63a5\u70b9\u4e0a\u3002\u4e0d\u540c\u7684\u901a\u77e5\u901a\u5e38\u9700\u8981\u5207\u5165\u5230\u4e0d\u540c\u7684\u8fde\u63a5\u70b9\u4e0a\uff0c\u8fd9\u79cd\u7cbe\u51c6\u7684\u5339\u914d\u662f\u7531\u5207\u5165\u70b9\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u6765\u5b9a\u4e49\u7684\u3002<\/p>\n<p style=\"text-indent:50px;\">\u6bd4\u5982\uff0c\u5728\u4e0a\u9762\u6240\u8bf4\u7684\u8fde\u63a5\u70b9\u7684\u57fa\u7840\u4e0a\uff0c\u6765\u5b9a\u4e49\u5207\u5165\u70b9\u3002\u6211\u4eec\u6709\u4e00\u4e2a\u7c7b\uff0c\u7c7b\u91cc\u670910\u4e2a\u65b9\u6cd5\uff0c\u90a3\u5c31\u4ea7\u751f\u4e86\u51e0\u5341\u4e2a\u8fde\u63a5\u70b9\u3002\u4f46\u662f\u6211\u4eec\u5e76\u4e0d\u60f3\u5728\u6240\u6709\u65b9\u6cd5\u4e0a\u90fd\u7ec7\u5165\u901a\u77e5\uff0c\u6211\u4eec\u53ea\u60f3\u8ba9\u5176\u4e2d\u7684\u51e0\u4e2a\u65b9\u6cd5\uff0c\u5728\u8c03\u7528\u4e4b\u524d\u68c0\u9a8c\u4e0b\u5165\u53c2\u662f\u5426\u5408\u6cd5\uff0c\u90a3\u4e48\u5c31\u7528\u5207\u70b9\u6765\u5b9a\u4e49\u8fd9\u51e0\u4e2a\u65b9\u6cd5\uff0c\u8ba9\u5207\u70b9\u6765\u7b5b\u9009\u8fde\u63a5\u70b9\uff0c\u9009\u4e2d\u6211\u4eec\u60f3\u8981\u7684\u65b9\u6cd5\u3002<span style=\"color:#ff0000;\">\u5207\u5165\u70b9\u5c31\u662f\u6765\u5b9a\u4e49\u54ea\u4e9b\u7c7b\u91cc\u9762\u7684\u54ea\u4e9b\u65b9\u6cd5\u4f1a\u5f97\u5230\u901a\u77e5\u3002<\/span><\/p>\n<p style=\"text-indent:50px;\"><strong>\u76ee\u6807\u5bf9\u8c61\uff08Target\uff09<\/strong>\uff1a<span style=\"color:#ff0000;\">\u90a3\u4e9b\u5373\u5c06\u5207\u5165\u5207\u9762\u7684\u5bf9\u8c61<\/span>\uff0c\u4e5f\u5c31\u662f\u90a3\u4e9b\u88ab\u901a\u77e5\u7684\u5bf9\u8c61\u3002\u8fd9\u4e9b\u5bf9\u8c61\u4e13\u6ce8\u4e1a\u52a1\u672c\u8eab\u7684\u903b\u8f91\uff0c\u6240\u6709\u7684\u5171\u6709\u529f\u80fd\u7b49\u5f85AOP\u5bb9\u5668\u7684\u5207\u5165\u3002<\/p>\n<p style=\"text-indent:50px;\"><strong>\u4ee3\u7406\u5bf9\u8c61\uff08Proxy\uff09<\/strong>\uff1a\u5c06\u901a\u77e5\u5e94\u7528\u5230\u76ee\u6807\u5bf9\u8c61\u4e4b\u540e\u88ab\u52a8\u6001\u521b\u5efa\u7684\u5bf9\u8c61\u3002\u53ef\u4ee5\u7b80\u5355\u5730\u7406\u89e3\u4e3a\uff0c\u4ee3\u7406\u5bf9\u8c61\u7684\u529f\u80fd\u7b49\u4e8e\u76ee\u6807\u5bf9\u8c61\u672c\u8eab\u4e1a\u52a1\u903b\u8f91\u52a0\u4e0a\u5171\u6709\u529f\u80fd\u3002\u4ee3\u7406\u5bf9\u8c61\u5bf9\u4e8e\u4f7f\u7528\u8005\u800c\u8a00\u662f\u900f\u660e\u7684\uff0c\u662f\u7a0b\u5e8f\u8fd0\u884c\u8fc7\u7a0b\u4e2d\u7684\u4ea7\u7269\u3002<span style=\"color:#ff0000;\">\u76ee\u6807\u5bf9\u8c61\u88ab\u7ec7\u5165\u5171\u6709\u529f\u80fd\u540e\u4ea7\u751f\u7684\u5bf9\u8c61\u3002<\/span><\/p>\n<p style=\"text-indent:50px;\"><strong>\u7ec7\u5165\uff08Weaving\uff09<\/strong>\uff1a\u5c06\u5207\u9762\u5e94\u7528\u5230\u76ee\u6807\u5bf9\u8c61\u4ece\u800c\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4ee3\u7406\u5bf9\u8c61\u7684\u8fc7\u7a0b\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u53ef\u4ee5\u53d1\u751f\u5728\u7f16\u8bd1\u65f6\u3001\u7c7b\u52a0\u8f7d\u65f6\u3001\u8fd0\u884c\u65f6\u3002Spring\u662f\u5728\u8fd0\u884c\u65f6\u5b8c\u6210\u7ec7\u5165\uff0c\u8fd0\u884c\u65f6\u7ec7\u5165\u901a\u8fc7Java\u8bed\u8a00\u7684\u53cd\u5c04\u673a\u5236\u4e0e\u52a8\u6001\u4ee3\u7406\u673a\u5236\u6765\u52a8\u6001\u5b9e\u73b0\u3002<\/p>\n<p style=\"text-indent:50px;\">\n<h2><strong><span style=\"font-size: 18px;\">3.2 Pointcut\u7528\u6cd5<\/span><\/strong><\/h2>\n<p style=\"text-indent:50px;\">Pointcut\u683c\u5f0f\u4e3a\uff1a<\/p>\n<blockquote>\n<p style=\"text-indent:50px;\">execution(modifier-pattern? ret-type-pattern declaring-type-pattern? name-pattern(param-pattern) throws-pattern?)<\/p>\n<\/blockquote>\n<p style=\"text-indent:50px;\"><strong><\/strong><\/p>\n<p style=\"text-indent:50px;\"><strong>\u4fee\u9970\u7b26\u5339\u914d modifier-pattern?<\/strong> \u4f8b\uff1apublic private<\/p>\n<p style=\"text-indent:50px;\"><strong>\u8fd4\u56de\u503c\u5339\u914d ret-type-pattern<\/strong> \u53ef\u4ee5\u7528 * \u8868\u793a\u4efb\u610f\u8fd4\u56de\u503c<\/p>\n<p style=\"text-indent:50px;\"><strong>\u7c7b\u8def\u5f84\u5339\u914d declaring-type-pattern?<\/strong> \u5168\u8def\u5f84\u7684\u7c7b\u540d<\/p>\n<p style=\"text-indent:50px;\"><strong>\u65b9\u6cd5\u540d\u5339\u914d name-pattern<\/strong> \u53ef\u4ee5\u6307\u5b9a\u65b9\u6cd5\u540d\u6216\u8005\u7528 * \u8868\u793a\u6240\u6709\u65b9\u6cd5\uff1bset* \u8868\u793a\u6240\u6709\u4ee5set\u5f00\u5934\u7684\u65b9\u6cd5<\/p>\n<p style=\"text-indent:50px;\"><strong>\u53c2\u6570\u5339\u914d (param-pattern)<\/strong> \u53ef\u4ee5\u6307\u5b9a\u5177\u4f53\u7684\u53c2\u6570\u7c7b\u578b\uff0c\u591a\u4e2a\u53c2\u6570\u7528\u201c,\u201d\u5206\u9694\uff1b\u53ef\u4ee5\u7528 * \u8868\u793a\u5339\u914d\u4efb\u610f\u7c7b\u578b\u7684\u53c2\u6570\uff1b\u53ef\u4ee5\u7528 (..) \u8868\u793a\u96f6\u4e2a\u6216\u591a\u4e2a\u4efb\u610f\u53c2\u6570<\/p>\n<p style=\"text-indent:50px;\"><strong>\u5f02\u5e38\u7c7b\u578b\u5339\u914dthrows-pattern?<\/strong> \u4f8b\uff1athrows Exception<\/p>\n<p style=\"text-indent:50px;\"><span style=\"color:#f33b45;\">\u5176\u4e2d\u540e\u9762\u8ddf\u7740 ? \u8868\u793a\u53ef\u9009\u9879<\/span><\/p>\n<p style=\"text-indent:50px;\"><span style=\"color:#f33b45;\"><\/span><\/p>\n<p style=\"text-indent:50px;\">\u4f8b\uff1a<\/p>\n<pre style=\"background-color: rgb(43, 43, 43);text-align: left;\"><span style=\"font-size: 16px;color: rgb(187, 181, 41);\">@Pointcut<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">(<\/span><span style=\"font-size: 16px;color: rgb(106, 135, 89);\">\"execution(public * cn.wbnull. springbootdemo.controller.              <\/span> <span style=\"font-size: 16px;color: rgb(106, 135, 89);\">*.*(..))\"<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">)<\/span><span style=\"text-align: left;font-size: 16px;color: rgb(204, 120, 50);\">private void <\/span><span style=\"text-align: left;font-size: 16px;color: rgb(255, 198, 109);\">sign<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">() {}<\/span><\/pre>\n<h2><strong><span style=\"font-size: 18px;\">3.3 \u4e00\u4e2a\u4f8b\u5b50<\/span><\/strong><\/h2>\n<p style=\"text-indent:50px;\">\u4ee5<a href=\"http:\/\/mp.weixin.qq.com\/s?__biz=MzAxOTkxNTgzMA==&amp;mid=2247483764&amp;idx=1&amp;sn=2c8b72baf0b97b465ebe3d628842e2c2&amp;chksm=9c3ef42dab497d3bd6635200afc00b9892b4d9be9b064e9b0f7a8ca7148c66e3753f1e0d2d09&amp;scene=21#wechat_redirect\" target=\"_blank\" data-itemshowtype=\"0\" data-linktype=\"2\" rel=\"noopener\" rel=\"nofollow\" >Spring Boot\u5165\u95e8\uff1a\u4f7f\u7528AOP\u5b9e\u73b0\u62e6\u622a\u5668<\/a>\u4e2d\u7684AOP\u4e3a\u4f8b<\/p>\n<pre style=\"background-color: rgb(43, 43, 43);\"><span style=\"font-size: 16px;color: rgb(187, 181, 41);\">@Aspect@Component<\/span><span style=\"text-align: left;font-size: 16px;color: rgb(204, 120, 50);\">public class <\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">SignAop {}<\/span><\/pre>\n<p style=\"text-indent:50px;\">SignAop\u7c7b\u4f7f\u7528\u4e86@Aspect\u6ce8\u89e3\uff0c\u5219\u8be5\u7c7b\u53ef\u4ee5\u88abAOP\u5bb9\u5668\u8bc6\u522b\u4e3a\u5207\u9762\u3002<\/p>\n<p style=\"text-indent:50px;\">\n<pre style=\"background-color: rgb(43, 43, 43);\"><span style=\"font-size: 16px;color: rgb(187, 181, 41);\">@Aspect@Component<\/span><span style=\"text-align: left;font-size: 16px;color: rgb(204, 120, 50);\">public class <\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">SignAop {<\/span><em><span lang=\"EN-US\" style=\"\"> <\/span><\/em><span style=\"font-size: 16px;color: rgb(187, 181, 41);\">@Pointcut<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">(<\/span><span style=\"font-size: 16px;color: rgb(106, 135, 89);\">\"execution(public * cn.wbnull.springbootdemo.<\/span><span style=\"font-size: 16px;color: rgb(106, 135, 89);\">controller<\/span><span style=\"font-size: 16px;color: rgb(106, 135, 89);\">               .*.*(..))\"<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">)<\/span><span lang=\"EN-US\" style=\"\"> <\/span><span style=\"text-align: left;font-size: 16px;color: rgb(204, 120, 50);\">private void <\/span><span style=\"text-align: left;font-size: 16px;color: rgb(255, 198, 109);\">signAop<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">() {<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\"> }}<\/span><\/pre>\n<p style=\"text-indent:50px;\">@Pointcut\u58f0\u660e\u4e00\u4e2a\u5207\u5165\u70b9\uff0c\u8303\u56f4\u4e3acontroller\u5305\u4e0b\u6240\u6709\u7684\u7c7b\u7684\u6240\u6709\u65b9\u6cd5<\/p>\n<p style=\"text-indent:50px;\">\u6ce8\uff1a\u4f5c\u4e3a\u5207\u5165\u70b9\u7b7e\u540d\u7684\u65b9\u6cd5\u5fc5\u987b\u8fd4\u56devoid\u7c7b\u578b<\/p>\n<p style=\"text-indent:50px;\">\n<pre style=\"background-color: rgb(43, 43, 43);\"><span style=\"font-size: 16px;color: rgb(187, 181, 41);\">@Aspect@Component<\/span><span style=\"text-align: left;font-size: 16px;color: rgb(204, 120, 50);\">public class <\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">SignAop {<\/span><em><span lang=\"EN-US\" style=\"\"> <\/span><\/em><span style=\"font-size: 16px;color: rgb(187, 181, 41);\">@Pointcut<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">(<\/span><span style=\"font-size: 16px;color: rgb(106, 135, 89);\">\"execution(public * cn.wbnull.springbootdemo.controller.                <\/span><span style=\"font-size: 16px;color: rgb(106, 135, 89);\">*.*(..))\"<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">)<\/span><span lang=\"EN-US\" style=\"\"> <\/span><span style=\"text-align: left;font-size: 16px;color: rgb(204, 120, 50);\">private void <\/span><span style=\"text-align: left;font-size: 16px;color: rgb(255, 198, 109);\">signAop<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">() { } <\/span><em><span lang=\"EN-US\" style=\"\"> <\/span><\/em><span style=\"font-size: 16px;color: rgb(187, 181, 41);\">@Before<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">(<\/span><span style=\"font-size: 16px;color: rgb(106, 135, 89);\">\"signAop()\"<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">)<\/span><span lang=\"EN-US\" style=\"\"> <\/span><span style=\"text-align: left;font-size: 16px;color: rgb(204, 120, 50);\">public void <\/span><span style=\"text-align: left;font-size: 16px;color: rgb(255, 198, 109);\">doBefore<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">(JoinPoint joinPoint) <\/span><span style=\"text-align: left;font-size: 16px;color: rgb(204, 120, 50);\">throws <\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">Exception <\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">{<\/span><span lang=\"EN-US\" style=\"\"><\/span><span style=\"font-size: 16px;color: rgb(106, 135, 89);\"> \/\/code<\/span><span lang=\"EN-US\" style=\"\"> <\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">}<\/span><em><span lang=\"EN-US\" style=\"\"> <\/span><\/em><span style=\"font-size: 16px;color: rgb(187, 181, 41);\">@AfterReturning<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">(<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">value = <\/span><span style=\"font-size: 16px;color: rgb(106, 135, 89);\">\"signAop()\"<\/span><span style=\"text-align: left;font-size: 16px;color: rgb(204, 120, 50);\">,<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\"> returning = <\/span><span style=\"font-size: 16px;color: rgb(106, 135, 89);\">\"params\"<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">)<\/span><span lang=\"EN-US\" style=\"\"> <\/span><span style=\"text-align: left;font-size: 16px;color: rgb(204, 120, 50);\">public<\/span> <span style=\"font-size: 16px;color: rgb(169, 183, 198);\">JSONObject <\/span><span style=\"text-align: left;font-size: 16px;color: rgb(255, 198, 109);\">doAfterReturning<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">(JoinPoint joinPoint<\/span><span style=\"text-align: left;font-size: 16px;color: rgb(204, 120, 50);\">, <\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">          JSONObject<\/span><span lang=\"EN-US\" style=\"\"> <\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">para<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">ms<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">) { <\/span><span style=\"font-size: 16px;color: rgb(106, 135, 89);\">\/\/code<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">}<\/span><span style=\"font-size: 16px;color: rgb(169, 183, 198);\">}<\/span><\/pre>\n<p style=\"text-indent:50px;\">doBefore()\u65b9\u6cd5\u4f7f\u7528@Before(\"signAop()\")\u6ce8\u89e3\uff0c\u8868\u793a\u524d\u7f6e\u901a\u77e5\uff08\u5728\u67d0\u8fde\u63a5\u70b9\u4e4b\u524d\u6267\u884c\u7684\u901a\u77e5\uff09\uff0c\u4f46\u8fd9\u4e2a\u901a\u77e5\u4e0d\u80fd\u963b\u6b62\u8fde\u63a5\u70b9\u4e4b\u524d\u7684\u6267\u884c\u6d41\u7a0b\uff0c\u9664\u975e\u5b83\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\u3002<\/p>\n<p style=\"text-indent:50px;\">doAfterReturning()\u65b9\u6cd5\u4f7f\u7528@AfterReturning(value = \"signAop()\", returning = \"params\")\u6ce8\u89e3\uff0c\u8868\u793a\u540e\u7f6e\u901a\u77e5\uff08\u5728\u67d0\u8fde\u63a5\u70b9\u6b63\u5e38\u5b8c\u6210\u540e\u6267\u884c\u7684\u901a\u77e5\uff09\uff0c\u901a\u5e38\u5728\u4e00\u4e2a\u5339\u914d\u7684\u65b9\u6cd5\u8fd4\u56de\u7684\u65f6\u5019\u6267\u884c\u3002<\/p>\n<p style=\"text-indent:50px;\">\u5b9e\u9645\u8fd0\u884c\u65f6\uff0c\u5728\u8fdb\u5165controller\u5305\u4e0b\u6240\u6709\u65b9\u6cd5\u524d\uff0c\u90fd\u4f1a\u8fdb\u5165doBefore()\u65b9\u6cd5\uff0c\u5728controller\u5305\u4e0b\u65b9\u6cd5\u6267\u884c\u5b8c\u6210\u540e\uff0c\u90fd\u4f1a\u8fdb\u5165doAfterReturning()\u65b9\u6cd5\u3002<\/p>\n<p style=\"text-indent:50px;\">\n<p style=\"text-indent:50px;\">AOP\u5177\u4f53\u4f7f\u7528\u53ef\u4ee5\u53c2\u8003<a href=\"http:\/\/mp.weixin.qq.com\/s?__biz=MzAxOTkxNTgzMA==&amp;mid=2247483764&amp;idx=1&amp;sn=2c8b72baf0b97b465ebe3d628842e2c2&amp;chksm=9c3ef42dab497d3bd6635200afc00b9892b4d9be9b064e9b0f7a8ca7148c66e3753f1e0d2d09&amp;scene=21#wechat_redirect\" target=\"_blank\" data-itemshowtype=\"0\" data-linktype=\"2\" rel=\"noopener\" rel=\"nofollow\" >Spring Boot\u5165\u95e8\uff1a\u4f7f\u7528AOP\u5b9e\u73b0\u62e6\u622a\u5668<\/a><\/p>\n<p style=\"text-indent:50px;\">\n<p style=\"text-indent:50px;\">\u5230\u8fd9\u91cc\uff0cSpring\u4e24\u5927\u7279\u6027IoC\u548cAOP\u57fa\u672c\u8bb2\u8ff0\u5b8c\u6210\uff0c\u540e\u7eed\u82e5\u60f3\u8d77\u5176\u4ed6\u518d\u8fdb\u884c\u8865\u5145\u3002<\/p>\n<p style=\"text-indent:50px;\">\n<blockquote>\n<p>\u672c\u7bc7\u6587\u7ae0\u6765\u6e90\u4e8e\u5fae\u4fe1\u516c\u4f17\u53f7:                 \u7a0b\u5e8f\u5458null              <\/p>\n<\/blockquote>\n<\/div>\n","raw":"","protected":false},"excerpt":{"rendered":"<p>\u4f17\u6240\u5468\u77e5\uff0cSpring\u62e5\u6709\u4e24\u5927\u7279\u6027\uff1aIoC\u548cAOP\u3002IoC\uff0c\u82f1\u6587\u5168\u79f0Inversion of Control\uff0c\u610f\u4e3a\u63a7\u5236\u53cd\u8f6c\u3002AOP &#8230;<\/p>\n","protected":false},"author":1,"featured_media":4308,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[6],"tags":[],"class_list":["post-4311","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-spring"],"post_thumbnail_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524152621859-150x150.jpeg","content_first_image":null,"post_medium_image_300":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524152621859-300x128.jpeg","post_thumbnail_image_624":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524152621859-1024x435.jpeg","post_frist_image":null,"post_medium_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524152621859-300x128.jpeg","post_large_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524152621859-1024x435.jpeg","post_full_image":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2024\/05\/20240524152621859.jpeg","post_all_images":[],"videoAdId":"","listAd":"0","listAdId":"","listAdEvery":6,"total_comments":0,"category_name":"Spring","post_date":"2019-02-14","like_count":"0","praiseWord":"\u9f13\u52b1","copyright_state":"","excitationAd":"0","rewardedVideoAdId":"","detailAdId":"","detailAd":"0","enterpriseMinapp":"0","audios":[],"postImageUrl":"https:\/\/wp-moto-1258805347.cos.ap-shanghai.myqcloud.com\/2023\/05\/20230519082947553.jpg","avatarurls":[],"related_posts":null,"pageviews":346,"next_post_id":208,"next_post_title":"SpringBoot + Docker \u5b9e\u73b0\u4e00\u6b21\u6784\u5efa\u5230\u5904\u8fd0\u884c","previous_post_id":null,"previous_post_title":null,"_links":{"self":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4311","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/comments?post=4311"}],"version-history":[{"count":0,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/posts\/4311\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media\/4308"}],"wp:attachment":[{"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/media?parent=4311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/categories?post=4311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xinchewhd.com.cn\/index.php\/wp-json\/wp\/v2\/tags?post=4311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}