{"id":10006081,"date":"2022-08-02T00:47:39","date_gmt":"2022-08-01T16:47:39","guid":{"rendered":"https:\/\/ityet.com\/?p=10006081"},"modified":"2022-08-02T00:47:39","modified_gmt":"2022-08-01T16:47:39","slug":"spring-%e5%b8%b8%e8%a7%81%e9%9d%a2%e8%af%95%e9%a2%98%e6%80%bb%e7%bb%93-javaguide","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006081","title":{"rendered":"Spring \u5e38\u89c1\u9762\u8bd5\u9898\u603b\u7ed3 | JavaGuide"},"content":{"rendered":"<blockquote><p>\u9996\u53d1\u4e8e <a href=\"https:\/\/link.segmentfault.com\/?enc=U7%2BkbYiS9TMlLcSABcxFyA%3D%3D.bjq%2Fhm70csEPTV7De6EoukCKEpWxVoGVoeqqQDofJYk%3D\" rel=\"nofollow\">JavaGuide \u5728\u7ebf\u7f51\u7ad9<\/a>\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=Ly1hubIrx7Xf3rfKtlRxqQ%3D%3D.HmpKvrzT0oQMdP48AlQAWhBGa2gUFPfIWY9wiS1dM4139PRBwSpt8wP%2FLsG0GqE%2FGV5mLUu5D1oyXmCen8foSBpvzHswG3ZJRZTLr9E6IwZP3bs8uEgFwReB8fPUY5IN\" rel=\"nofollow\">Spring \u5e38\u89c1\u9762\u8bd5\u9898\u603b\u7ed3<\/a><\/p><\/blockquote>\n<p>\u6700\u8fd1\u5728\u5bf9 JavaGuide \u7684\u5185\u5bb9\u8fdb\u884c\u91cd\u6784\u5b8c\u5584\uff0c\u540c\u6b65\u4e00\u4e0b\u6700\u65b0\u66f4\u65b0\uff0c\u5e0c\u671b\u80fd\u591f\u5e2e\u52a9\u4f60\u3002<\/p>\n<h2>Spring \u57fa\u7840<\/h2>\n<h3>\u4ec0\u4e48\u662f Spring \u6846\u67b6?<\/h3>\n<p>Spring \u662f\u4e00\u6b3e\u5f00\u6e90\u7684\u8f7b\u91cf\u7ea7 Java \u5f00\u53d1\u6846\u67b6\uff0c\u65e8\u5728\u63d0\u9ad8\u5f00\u53d1\u4eba\u5458\u7684\u5f00\u53d1\u6548\u7387\u4ee5\u53ca\u7cfb\u7edf\u7684\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>\u6211\u4eec\u4e00\u822c\u8bf4 Spring \u6846\u67b6\u6307\u7684\u90fd\u662f Spring Framework\uff0c\u5b83\u662f\u5f88\u591a\u6a21\u5757\u7684\u96c6\u5408\uff0c\u4f7f\u7528\u8fd9\u4e9b\u6a21\u5757\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u534f\u52a9\u6211\u4eec\u8fdb\u884c\u5f00\u53d1\uff0c\u6bd4\u5982\u8bf4 Spring \u652f\u6301 IoC\uff08Inverse of Control:\u63a7\u5236\u53cd\u8f6c\uff09 \u548c AOP(Aspect-Oriented Programming:\u9762\u5411\u5207\u9762\u7f16\u7a0b)\u3001\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u5bf9\u6570\u636e\u5e93\u8fdb\u884c\u8bbf\u95ee\u3001\u53ef\u4ee5\u5f88\u65b9\u4fbf\u5730\u96c6\u6210\u7b2c\u4e09\u65b9\u7ec4\u4ef6\uff08\u7535\u5b50\u90ae\u4ef6\uff0c\u4efb\u52a1\uff0c\u8c03\u5ea6\uff0c\u7f13\u5b58\u7b49\u7b49\uff09\u3001\u5bf9\u5355\u5143\u6d4b\u8bd5\u652f\u6301\u6bd4\u8f83\u597d\u3001\u652f\u6301 RESTful Java \u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254919\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Spring \u6700\u6838\u5fc3\u7684\u601d\u60f3\u5c31\u662f\u4e0d\u91cd\u65b0\u9020\u8f6e\u5b50\uff0c\u5f00\u7bb1\u5373\u7528\uff0c\u63d0\u9ad8\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<p>Spring \u7ffb\u8bd1\u8fc7\u6765\u5c31\u662f\u6625\u5929\u7684\u610f\u601d\uff0c\u53ef\u89c1\u5176\u76ee\u6807\u548c\u4f7f\u547d\u5c31\u662f\u4e3a Java \u7a0b\u5e8f\u5458\u5e26\u6765\u6625\u5929\u554a\uff01\u611f\u52a8\uff01<\/p>\n<p>\ud83e\udd10 \u591a\u63d0\u4e00\u5634 \uff1a <strong>\u8bed\u8a00\u7684\u6d41\u884c\u901a\u5e38\u9700\u8981\u4e00\u4e2a\u6740\u624b\u7ea7\u7684\u5e94\u7528\uff0cSpring \u5c31\u662f Java \u751f\u6001\u7684\u4e00\u4e2a\u6740\u624b\u7ea7\u7684\u5e94\u7528\u6846\u67b6\u3002<\/strong><\/p>\n<p>Spring \u63d0\u4f9b\u7684\u6838\u5fc3\u529f\u80fd\u4e3b\u8981\u662f IoC \u548c AOP\u3002\u5b66\u4e60 Spring \uff0c\u4e00\u5b9a\u8981\u628a IoC \u548c AOP \u7684\u6838\u5fc3\u601d\u60f3\u641e\u61c2\uff01<\/p>\n<ul>\n<li>Spring \u5b98\u7f51\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=rgzsUdM7QNMlj8ElJqOzHw%3D%3D.p%2FfjOopkv6TMt7kw6Z76R8gPu19TepZPLn4H9%2FcJaoQ%3D\" rel=\"nofollow\">https:\/\/spring.io\/<\/a><\/li>\n<li>Github \u5730\u5740\uff1a <a href=\"https:\/\/link.segmentfault.com\/?enc=m5PReEYJpP5gydM%2FAEaQUg%3D%3D.z4n5HZAHLYGl8qb0U6IJn486lqxcxwO7uKpWr3Q8DgwS3jvBRC6Cti%2FAkpkHDSrBemtAHVK%2FkGeZH5jIlpCPig%3D%3D\" rel=\"nofollow\">https:\/\/github.com\/spring-pro&#8230;<\/a><\/li>\n<\/ul>\n<h3>Spring \u5305\u542b\u7684\u6a21\u5757\u6709\u54ea\u4e9b\uff1f<\/h3>\n<p><strong>Spring4.x \u7248\u672c<\/strong> \uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254920\" alt=\"Spring4.x\u4e3b\u8981\u6a21\u5757\" title=\"Spring4.x\u4e3b\u8981\u6a21\u5757\" loading=\"lazy\"><\/p>\n<p><strong>Spring5.x \u7248\u672c<\/strong> \uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254921\" alt=\"Spring5.x\u4e3b\u8981\u6a21\u5757\" title=\"Spring5.x\u4e3b\u8981\u6a21\u5757\" loading=\"lazy\"><\/p>\n<p>Spring5.x \u7248\u672c\u4e2d Web \u6a21\u5757\u7684 Portlet \u7ec4\u4ef6\u5df2\u7ecf\u88ab\u5e9f\u5f03\u6389\uff0c\u540c\u65f6\u589e\u52a0\u4e86\u7528\u4e8e\u5f02\u6b65\u54cd\u5e94\u5f0f\u5904\u7406\u7684 WebFlux \u7ec4\u4ef6\u3002<\/p>\n<p>Spring \u5404\u4e2a\u6a21\u5757\u7684\u4f9d\u8d56\u5173\u7cfb\u5982\u4e0b\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254922\" alt=\"Spring \u5404\u4e2a\u6a21\u5757\u7684\u4f9d\u8d56\u5173\u7cfb\" title=\"Spring \u5404\u4e2a\u6a21\u5757\u7684\u4f9d\u8d56\u5173\u7cfb\" loading=\"lazy\"><\/p>\n<h4>Core Container<\/h4>\n<p>Spring \u6846\u67b6\u7684\u6838\u5fc3\u6a21\u5757\uff0c\u4e5f\u53ef\u4ee5\u8bf4\u662f\u57fa\u7840\u6a21\u5757\uff0c\u4e3b\u8981\u63d0\u4f9b IoC \u4f9d\u8d56\u6ce8\u5165\u529f\u80fd\u7684\u652f\u6301\u3002Spring \u5176\u4ed6\u6240\u6709\u7684\u529f\u80fd\u57fa\u672c\u90fd\u9700\u8981\u4f9d\u8d56\u4e8e\u8be5\u6a21\u5757\uff0c\u6211\u4eec\u4ece\u4e0a\u9762\u90a3\u5f20 Spring \u5404\u4e2a\u6a21\u5757\u7684\u4f9d\u8d56\u5173\u7cfb\u56fe\u5c31\u53ef\u4ee5\u770b\u51fa\u6765\u3002<\/p>\n<ul>\n<li><strong>spring-core<\/strong> \uff1aSpring \u6846\u67b6\u57fa\u672c\u7684\u6838\u5fc3\u5de5\u5177\u7c7b\u3002<\/li>\n<li><strong>spring-beans<\/strong> \uff1a\u63d0\u4f9b\u5bf9 bean \u7684\u521b\u5efa\u3001\u914d\u7f6e\u548c\u7ba1\u7406\u7b49\u529f\u80fd\u7684\u652f\u6301\u3002<\/li>\n<li><strong>spring-context<\/strong> \uff1a\u63d0\u4f9b\u5bf9\u56fd\u9645\u5316\u3001\u4e8b\u4ef6\u4f20\u64ad\u3001\u8d44\u6e90\u52a0\u8f7d\u7b49\u529f\u80fd\u7684\u652f\u6301\u3002<\/li>\n<li><strong>spring-expression<\/strong> \uff1a\u63d0\u4f9b\u5bf9\u8868\u8fbe\u5f0f\u8bed\u8a00\uff08Spring Expression Language\uff09 SpEL \u7684\u652f\u6301\uff0c\u53ea\u4f9d\u8d56\u4e8e core \u6a21\u5757\uff0c\u4e0d\u4f9d\u8d56\u4e8e\u5176\u4ed6\u6a21\u5757\uff0c\u53ef\u4ee5\u5355\u72ec\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<h4>AOP<\/h4>\n<ul>\n<li><strong>spring-aspects<\/strong> \uff1a\u8be5\u6a21\u5757\u4e3a\u4e0e AspectJ \u7684\u96c6\u6210\u63d0\u4f9b\u652f\u6301\u3002<\/li>\n<li><strong>spring-aop<\/strong> \uff1a\u63d0\u4f9b\u4e86\u9762\u5411\u5207\u9762\u7684\u7f16\u7a0b\u5b9e\u73b0\u3002<\/li>\n<li><strong>spring-instrument<\/strong> \uff1a\u63d0\u4f9b\u4e86\u4e3a JVM \u6dfb\u52a0\u4ee3\u7406\uff08agent\uff09\u7684\u529f\u80fd\u3002 \u5177\u4f53\u6765\u8bb2\uff0c\u5b83\u4e3a Tomcat \u63d0\u4f9b\u4e86\u4e00\u4e2a\u7ec7\u5165\u4ee3\u7406\uff0c\u80fd\u591f\u4e3a Tomcat \u4f20\u9012\u7c7b\u6587 \u4ef6\uff0c\u5c31\u50cf\u8fd9\u4e9b\u6587\u4ef6\u662f\u88ab\u7c7b\u52a0\u8f7d\u5668\u52a0\u8f7d\u7684\u4e00\u6837\u3002\u6ca1\u6709\u7406\u89e3\u4e5f\u6ca1\u5173\u7cfb\uff0c\u8fd9\u4e2a\u6a21\u5757\u7684\u4f7f\u7528\u573a\u666f\u975e\u5e38\u6709\u9650\u3002<\/li>\n<\/ul>\n<h4>Data Access\/Integration<\/h4>\n<ul>\n<li><strong>spring-jdbc<\/strong> \uff1a\u63d0\u4f9b\u4e86\u5bf9\u6570\u636e\u5e93\u8bbf\u95ee\u7684\u62bd\u8c61 JDBC\u3002\u4e0d\u540c\u7684\u6570\u636e\u5e93\u90fd\u6709\u81ea\u5df1\u72ec\u7acb\u7684 API \u7528\u4e8e\u64cd\u4f5c\u6570\u636e\u5e93\uff0c\u800c Java \u7a0b\u5e8f\u53ea\u9700\u8981\u548c JDBC API \u4ea4\u4e92\uff0c\u8fd9\u6837\u5c31\u5c4f\u853d\u4e86\u6570\u636e\u5e93\u7684\u5f71\u54cd\u3002<\/li>\n<li><strong>spring-tx<\/strong> \uff1a\u63d0\u4f9b\u5bf9\u4e8b\u52a1\u7684\u652f\u6301\u3002<\/li>\n<li><strong>spring-orm<\/strong> \uff1a \u63d0\u4f9b\u5bf9 Hibernate\u3001JPA \u3001iBatis \u7b49 ORM \u6846\u67b6\u7684\u652f\u6301\u3002<\/li>\n<li><strong>spring-oxm<\/strong> \uff1a\u63d0\u4f9b\u4e00\u4e2a\u62bd\u8c61\u5c42\u652f\u6491 OXM(Object-to-XML-Mapping)\uff0c\u4f8b\u5982\uff1aJAXB\u3001Castor\u3001XMLBeans\u3001JiBX \u548c XStream \u7b49\u3002<\/li>\n<li><strong>spring-jms<\/strong> : \u6d88\u606f\u670d\u52a1\u3002\u81ea Spring Framework 4.1 \u4ee5\u540e\uff0c\u5b83\u8fd8\u63d0\u4f9b\u4e86\u5bf9 spring-messaging \u6a21\u5757\u7684\u7ee7\u627f\u3002<\/li>\n<\/ul>\n<h4>Spring Web<\/h4>\n<ul>\n<li><strong>spring-web<\/strong> \uff1a\u5bf9 Web \u529f\u80fd\u7684\u5b9e\u73b0\u63d0\u4f9b\u4e00\u4e9b\u6700\u57fa\u7840\u7684\u652f\u6301\u3002<\/li>\n<li><strong>spring-webmvc<\/strong> \uff1a \u63d0\u4f9b\u5bf9 Spring MVC \u7684\u5b9e\u73b0\u3002<\/li>\n<li><strong>spring-websocket<\/strong> \uff1a \u63d0\u4f9b\u4e86\u5bf9 WebSocket \u7684\u652f\u6301\uff0cWebSocket \u53ef\u4ee5\u8ba9\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u7aef\u8fdb\u884c\u53cc\u5411\u901a\u4fe1\u3002<\/li>\n<li><strong>spring-webflux<\/strong> \uff1a\u63d0\u4f9b\u5bf9 WebFlux \u7684\u652f\u6301\u3002WebFlux \u662f Spring Framework 5.0 \u4e2d\u5f15\u5165\u7684\u65b0\u7684\u54cd\u5e94\u5f0f\u6846\u67b6\u3002\u4e0e Spring MVC \u4e0d\u540c\uff0c\u5b83\u4e0d\u9700\u8981 Servlet API\uff0c\u662f\u5b8c\u5168\u5f02\u6b65\u3002<\/li>\n<\/ul>\n<h4>Messaging<\/h4>\n<p><strong>spring-messaging<\/strong> \u662f\u4ece Spring4.0 \u5f00\u59cb\u65b0\u52a0\u5165\u7684\u4e00\u4e2a\u6a21\u5757\uff0c\u4e3b\u8981\u804c\u8d23\u662f\u4e3a Spring \u6846\u67b6\u96c6\u6210\u4e00\u4e9b\u57fa\u7840\u7684\u62a5\u6587\u4f20\u9001\u5e94\u7528\u3002<\/p>\n<h4>Spring Test<\/h4>\n<p>Spring \u56e2\u961f\u63d0\u5021\u6d4b\u8bd5\u9a71\u52a8\u5f00\u53d1\uff08TDD\uff09\u3002\u6709\u4e86\u63a7\u5236\u53cd\u8f6c (IoC)\u7684\u5e2e\u52a9\uff0c\u5355\u5143\u6d4b\u8bd5\u548c\u96c6\u6210\u6d4b\u8bd5\u53d8\u5f97\u66f4\u7b80\u5355\u3002<\/p>\n<p>Spring \u7684\u6d4b\u8bd5\u6a21\u5757\u5bf9 JUnit\uff08\u5355\u5143\u6d4b\u8bd5\u6846\u67b6\uff09\u3001TestNG\uff08\u7c7b\u4f3c JUnit\uff09\u3001Mockito\uff08\u4e3b\u8981\u7528\u6765 Mock \u5bf9\u8c61\uff09\u3001PowerMock\uff08\u89e3\u51b3 Mockito \u7684\u95ee\u9898\u6bd4\u5982\u65e0\u6cd5\u6a21\u62df final, static\uff0c private \u65b9\u6cd5\uff09\u7b49\u7b49\u5e38\u7528\u7684\u6d4b\u8bd5\u6846\u67b6\u652f\u6301\u7684\u90fd\u6bd4\u8f83\u597d\u3002<\/p>\n<h3>Spring,Spring MVC,Spring Boot \u4e4b\u95f4\u4ec0\u4e48\u5173\u7cfb?<\/h3>\n<p>\u5f88\u591a\u4eba\u5bf9 Spring,Spring MVC,Spring Boot \u8fd9\u4e09\u8005\u50bb\u50bb\u5206\u4e0d\u6e05\u695a\uff01\u8fd9\u91cc\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b\u8fd9\u4e09\u8005\uff0c\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u6ca1\u6709\u4ec0\u4e48\u9ad8\u6df1\u7684\u4e1c\u897f\u3002<\/p>\n<p>Spring \u5305\u542b\u4e86\u591a\u4e2a\u529f\u80fd\u6a21\u5757\uff08\u4e0a\u9762\u521a\u521a\u63d0\u9ad8\u8fc7\uff09\uff0c\u5176\u4e2d\u6700\u91cd\u8981\u7684\u662f Spring-Core\uff08\u4e3b\u8981\u63d0\u4f9b IoC \u4f9d\u8d56\u6ce8\u5165\u529f\u80fd\u7684\u652f\u6301\uff09 \u6a21\u5757\uff0c Spring \u4e2d\u7684\u5176\u4ed6\u6a21\u5757\uff08\u6bd4\u5982 Spring MVC\uff09\u7684\u529f\u80fd\u5b9e\u73b0\u57fa\u672c\u90fd\u9700\u8981\u4f9d\u8d56\u4e8e\u8be5\u6a21\u5757\u3002<\/p>\n<p>\u4e0b\u56fe\u5bf9\u5e94\u7684\u662f Spring4.x \u7248\u672c\u3002\u76ee\u524d\u6700\u65b0\u7684 5.x \u7248\u672c\u4e2d Web \u6a21\u5757\u7684 Portlet \u7ec4\u4ef6\u5df2\u7ecf\u88ab\u5e9f\u5f03\u6389\uff0c\u540c\u65f6\u589e\u52a0\u4e86\u7528\u4e8e\u5f02\u6b65\u54cd\u5e94\u5f0f\u5904\u7406\u7684 WebFlux \u7ec4\u4ef6\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254923\" alt=\"Spring\u4e3b\u8981\u6a21\u5757\" title=\"Spring\u4e3b\u8981\u6a21\u5757\" loading=\"lazy\"><\/p>\n<p>Spring MVC \u662f Spring \u4e2d\u7684\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u6a21\u5757\uff0c\u4e3b\u8981\u8d4b\u4e88 Spring \u5feb\u901f\u6784\u5efa MVC \u67b6\u6784\u7684 Web \u7a0b\u5e8f\u7684\u80fd\u529b\u3002MVC \u662f\u6a21\u578b(Model)\u3001\u89c6\u56fe(View)\u3001\u63a7\u5236\u5668(Controller)\u7684\u7b80\u5199\uff0c\u5176\u6838\u5fc3\u601d\u60f3\u662f\u901a\u8fc7\u5c06\u4e1a\u52a1\u903b\u8f91\u3001\u6570\u636e\u3001\u663e\u793a\u5206\u79bb\u6765\u7ec4\u7ec7\u4ee3\u7801\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254924\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u4f7f\u7528 Spring \u8fdb\u884c\u5f00\u53d1\u5404\u79cd\u914d\u7f6e\u8fc7\u4e8e\u9ebb\u70e6\u6bd4\u5982\u5f00\u542f\u67d0\u4e9b Spring \u7279\u6027\u65f6\uff0c\u9700\u8981\u7528 XML \u6216 Java \u8fdb\u884c\u663e\u5f0f\u914d\u7f6e\u3002\u4e8e\u662f\uff0cSpring Boot \u8bde\u751f\u4e86\uff01<\/p>\n<p>Spring \u65e8\u5728\u7b80\u5316 J2EE \u4f01\u4e1a\u5e94\u7528\u7a0b\u5e8f\u5f00\u53d1\u3002Spring Boot \u65e8\u5728\u7b80\u5316 Spring \u5f00\u53d1\uff08\u51cf\u5c11\u914d\u7f6e\u6587\u4ef6\uff0c\u5f00\u7bb1\u5373\u7528\uff01\uff09\u3002<\/p>\n<p>Spring Boot \u53ea\u662f\u7b80\u5316\u4e86\u914d\u7f6e\uff0c\u5982\u679c\u4f60\u9700\u8981\u6784\u5efa MVC \u67b6\u6784\u7684 Web \u7a0b\u5e8f\uff0c\u4f60\u8fd8\u662f\u9700\u8981\u4f7f\u7528 Spring MVC \u4f5c\u4e3a MVC \u6846\u67b6\uff0c\u53ea\u662f\u8bf4 Spring Boot \u5e2e\u4f60\u7b80\u5316\u4e86 Spring MVC \u7684\u5f88\u591a\u914d\u7f6e\uff0c\u771f\u6b63\u505a\u5230\u5f00\u7bb1\u5373\u7528\uff01<\/p>\n<h2>Spring IoC<\/h2>\n<h3>\u8c08\u8c08\u81ea\u5df1\u5bf9\u4e8e Spring IoC \u7684\u4e86\u89e3<\/h3>\n<p><strong>IoC\uff08Inverse of Control:\u63a7\u5236\u53cd\u8f6c\uff09<\/strong> \u662f\u4e00\u79cd\u8bbe\u8ba1\u601d\u60f3\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u5177\u4f53\u7684\u6280\u672f\u5b9e\u73b0\u3002IoC \u7684\u601d\u60f3\u5c31\u662f\u5c06\u539f\u672c\u5728\u7a0b\u5e8f\u4e2d\u624b\u52a8\u521b\u5efa\u5bf9\u8c61\u7684\u63a7\u5236\u6743\uff0c\u4ea4\u7531 Spring \u6846\u67b6\u6765\u7ba1\u7406\u3002\u4e0d\u8fc7\uff0c IoC \u5e76\u975e Spring \u7279\u6709\uff0c\u5728\u5176\u4ed6\u8bed\u8a00\u4e2d\u4e5f\u6709\u5e94\u7528\u3002<\/p>\n<p><strong>\u4e3a\u4ec0\u4e48\u53eb\u63a7\u5236\u53cd\u8f6c\uff1f<\/strong><\/p>\n<ul>\n<li><strong>\u63a7\u5236<\/strong> \uff1a\u6307\u7684\u662f\u5bf9\u8c61\u521b\u5efa\uff08\u5b9e\u4f8b\u5316\u3001\u7ba1\u7406\uff09\u7684\u6743\u529b<\/li>\n<li><strong>\u53cd\u8f6c<\/strong> \uff1a\u63a7\u5236\u6743\u4ea4\u7ed9\u5916\u90e8\u73af\u5883\uff08Spring \u6846\u67b6\u3001IoC \u5bb9\u5668\uff09<\/li>\n<\/ul>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254925\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5c06\u5bf9\u8c61\u4e4b\u95f4\u7684\u76f8\u4e92\u4f9d\u8d56\u5173\u7cfb\u4ea4\u7ed9 IoC \u5bb9\u5668\u6765\u7ba1\u7406\uff0c\u5e76\u7531 IoC \u5bb9\u5668\u5b8c\u6210\u5bf9\u8c61\u7684\u6ce8\u5165\u3002\u8fd9\u6837\u53ef\u4ee5\u5f88\u5927\u7a0b\u5ea6\u4e0a\u7b80\u5316\u5e94\u7528\u7684\u5f00\u53d1\uff0c\u628a\u5e94\u7528\u4ece\u590d\u6742\u7684\u4f9d\u8d56\u5173\u7cfb\u4e2d\u89e3\u653e\u51fa\u6765\u3002 IoC \u5bb9\u5668\u5c31\u50cf\u662f\u4e00\u4e2a\u5de5\u5382\u4e00\u6837\uff0c\u5f53\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u7684\u65f6\u5019\uff0c\u53ea\u9700\u8981\u914d\u7f6e\u597d\u914d\u7f6e\u6587\u4ef6\/\u6ce8\u89e3\u5373\u53ef\uff0c\u5b8c\u5168\u4e0d\u7528\u8003\u8651\u5bf9\u8c61\u662f\u5982\u4f55\u88ab\u521b\u5efa\u51fa\u6765\u7684\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u4e00\u4e2a Service \u7c7b\u53ef\u80fd\u4f9d\u8d56\u4e86\u5f88\u591a\u5176\u4ed6\u7684\u7c7b\uff0c\u5047\u5982\u6211\u4eec\u9700\u8981\u5b9e\u4f8b\u5316\u8fd9\u4e2a Service\uff0c\u4f60\u53ef\u80fd\u8981\u6bcf\u6b21\u90fd\u8981\u641e\u6e05\u8fd9\u4e2a Service \u6240\u6709\u5e95\u5c42\u7c7b\u7684\u6784\u9020\u51fd\u6570\uff0c\u8fd9\u53ef\u80fd\u4f1a\u628a\u4eba\u903c\u75af\u3002\u5982\u679c\u5229\u7528 IoC \u7684\u8bdd\uff0c\u4f60\u53ea\u9700\u8981\u914d\u7f6e\u597d\uff0c\u7136\u540e\u5728\u9700\u8981\u7684\u5730\u65b9\u5f15\u7528\u5c31\u884c\u4e86\uff0c\u8fd9\u5927\u5927\u589e\u52a0\u4e86\u9879\u76ee\u7684\u53ef\u7ef4\u62a4\u6027\u4e14\u964d\u4f4e\u4e86\u5f00\u53d1\u96be\u5ea6\u3002<\/p>\n<p>\u5728 Spring \u4e2d\uff0c IoC \u5bb9\u5668\u662f Spring \u7528\u6765\u5b9e\u73b0 IoC \u7684\u8f7d\u4f53\uff0c IoC \u5bb9\u5668\u5b9e\u9645\u4e0a\u5c31\u662f\u4e2a Map\uff08key\uff0cvalue\uff09\uff0cMap \u4e2d\u5b58\u653e\u7684\u662f\u5404\u79cd\u5bf9\u8c61\u3002<\/p>\n<p>Spring \u65f6\u4ee3\u6211\u4eec\u4e00\u822c\u901a\u8fc7 XML \u6587\u4ef6\u6765\u914d\u7f6e Bean\uff0c\u540e\u6765\u5f00\u53d1\u4eba\u5458\u89c9\u5f97 XML \u6587\u4ef6\u6765\u914d\u7f6e\u4e0d\u592a\u597d\uff0c\u4e8e\u662f SpringBoot \u6ce8\u89e3\u914d\u7f6e\u5c31\u6162\u6162\u5f00\u59cb\u6d41\u884c\u8d77\u6765\u3002<\/p>\n<p>\u76f8\u5173\u9605\u8bfb\uff1a<\/p>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=LDMagG9mlaYLHN%2FyNS3sGw%3D%3D.OHaHihTjvm1ltam4bBJoIA4meFP9O%2FA042PKxgd8Gc3Oxy%2B8ZgVQkzJZe%2Be4AtmF\" rel=\"nofollow\">IoC \u6e90\u7801\u9605\u8bfb<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=2%2F7HH5hffi8FdstbENRaBQ%3D%3D.9dbmpGacso4ufnSErgWaZKSQXv8Bx2Y40cIoPfoQl5mwU3FzjemUCOgiY%2Bna8br91LSQVnKzVlcdmGOS46bF4YKIdPbYXfBjaD8W4GH9pMHpWUY5Dwt5Tt078i7qjTkEJFgd88I3ZBr2IZH0fkGCOYvqiwot%2F7kbGcN1AWyntm6SCDv2xxAt6XkjEc8q9gg5vCay6aXgSIbQwZytHD3c12pZf9v57ZXVTtmqkyOzQw3kzu%2BGMCp3fDdgWBy9pQL32QiRSkgBYY%2FuR4QY4bE5f1GNlgh5iCuYJp7g%2BVh34H0%3D\" rel=\"nofollow\">\u9762\u8bd5\u88ab\u95ee\u4e86\u51e0\u767e\u904d\u7684 IoC \u548c AOP \uff0c\u8fd8\u5728\u50bb\u50bb\u641e\u4e0d\u6e05\u695a\uff1f<\/a><\/li>\n<\/ul>\n<h3>\u4ec0\u4e48\u662f Spring Bean\uff1f<\/h3>\n<p>\u7b80\u5355\u6765\u8bf4\uff0cBean \u4ee3\u6307\u7684\u5c31\u662f\u90a3\u4e9b\u88ab IoC \u5bb9\u5668\u6240\u7ba1\u7406\u7684\u5bf9\u8c61\u3002<\/p>\n<p>\u6211\u4eec\u9700\u8981\u544a\u8bc9 IoC \u5bb9\u5668\u5e2e\u52a9\u6211\u4eec\u7ba1\u7406\u54ea\u4e9b\u5bf9\u8c61\uff0c\u8fd9\u4e2a\u662f\u901a\u8fc7\u914d\u7f6e\u5143\u6570\u636e\u6765\u5b9a\u4e49\u7684\u3002\u914d\u7f6e\u5143\u6570\u636e\u53ef\u4ee5\u662f XML \u6587\u4ef6\u3001\u6ce8\u89e3\u6216\u8005 Java \u914d\u7f6e\u7c7b\u3002<\/p>\n<pre><code class=\"xml\">&lt;!-- Constructor-arg with 'value' attribute --&gt;\n&lt;bean id=&quot;...&quot; class=&quot;...&quot;&gt;\n   &lt;constructor-arg value=&quot;...&quot;\/&gt;\n&lt;\/bean&gt;<\/code><\/pre>\n<p>\u4e0b\u56fe\u7b80\u5355\u5730\u5c55\u793a\u4e86 IoC \u5bb9\u5668\u5982\u4f55\u4f7f\u7528\u914d\u7f6e\u5143\u6570\u636e\u6765\u7ba1\u7406\u5bf9\u8c61\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254926\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><code>org.springframework.beans<\/code>\u548c <code>org.springframework.context<\/code> \u8fd9\u4e24\u4e2a\u5305\u662f IoC \u5b9e\u73b0\u7684\u57fa\u7840\uff0c\u5982\u679c\u60f3\u8981\u7814\u7a76 IoC \u76f8\u5173\u7684\u6e90\u7801\u7684\u8bdd\uff0c\u53ef\u4ee5\u53bb\u770b\u770b<\/p>\n<h3>\u5c06\u4e00\u4e2a\u7c7b\u58f0\u660e\u4e3a Bean \u7684\u6ce8\u89e3\u6709\u54ea\u4e9b?<\/h3>\n<ul>\n<li><code>@Component<\/code> \uff1a\u901a\u7528\u7684\u6ce8\u89e3\uff0c\u53ef\u6807\u6ce8\u4efb\u610f\u7c7b\u4e3a <code>Spring<\/code> \u7ec4\u4ef6\u3002\u5982\u679c\u4e00\u4e2a Bean \u4e0d\u77e5\u9053\u5c5e\u4e8e\u54ea\u4e2a\u5c42\uff0c\u53ef\u4ee5\u4f7f\u7528<code>@Component<\/code> \u6ce8\u89e3\u6807\u6ce8\u3002<\/li>\n<li><code>@Repository<\/code> : \u5bf9\u5e94\u6301\u4e45\u5c42\u5373 Dao \u5c42\uff0c\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5e93\u76f8\u5173\u64cd\u4f5c\u3002<\/li>\n<li><code>@Service<\/code> : \u5bf9\u5e94\u670d\u52a1\u5c42\uff0c\u4e3b\u8981\u6d89\u53ca\u4e00\u4e9b\u590d\u6742\u7684\u903b\u8f91\uff0c\u9700\u8981\u7528\u5230 Dao \u5c42\u3002<\/li>\n<li><code>@Controller<\/code> : \u5bf9\u5e94 Spring MVC \u63a7\u5236\u5c42\uff0c\u4e3b\u8981\u7528\u6237\u63a5\u53d7\u7528\u6237\u8bf7\u6c42\u5e76\u8c03\u7528 Service \u5c42\u8fd4\u56de\u6570\u636e\u7ed9\u524d\u7aef\u9875\u9762\u3002<\/li>\n<\/ul>\n<h3>@Component \u548c @Bean \u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/h3>\n<ul>\n<li><code>@Component<\/code> \u6ce8\u89e3\u4f5c\u7528\u4e8e\u7c7b\uff0c\u800c<code>@Bean<\/code>\u6ce8\u89e3\u4f5c\u7528\u4e8e\u65b9\u6cd5\u3002<\/li>\n<li><code>@Component<\/code>\u901a\u5e38\u662f\u901a\u8fc7\u7c7b\u8def\u5f84\u626b\u63cf\u6765\u81ea\u52a8\u4fa6\u6d4b\u4ee5\u53ca\u81ea\u52a8\u88c5\u914d\u5230 Spring \u5bb9\u5668\u4e2d\uff08\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 <code>@ComponentScan<\/code> \u6ce8\u89e3\u5b9a\u4e49\u8981\u626b\u63cf\u7684\u8def\u5f84\u4ece\u4e2d\u627e\u51fa\u6807\u8bc6\u4e86\u9700\u8981\u88c5\u914d\u7684\u7c7b\u81ea\u52a8\u88c5\u914d\u5230 Spring \u7684 bean \u5bb9\u5668\u4e2d\uff09\u3002<code>@Bean<\/code> \u6ce8\u89e3\u901a\u5e38\u662f\u6211\u4eec\u5728\u6807\u6709\u8be5\u6ce8\u89e3\u7684\u65b9\u6cd5\u4e2d\u5b9a\u4e49\u4ea7\u751f\u8fd9\u4e2a bean,<code>@Bean<\/code>\u544a\u8bc9\u4e86 Spring \u8fd9\u662f\u67d0\u4e2a\u7c7b\u7684\u5b9e\u4f8b\uff0c\u5f53\u6211\u9700\u8981\u7528\u5b83\u7684\u65f6\u5019\u8fd8\u7ed9\u6211\u3002<\/li>\n<li><code>@Bean<\/code> \u6ce8\u89e3\u6bd4 <code>@Component<\/code> \u6ce8\u89e3\u7684\u81ea\u5b9a\u4e49\u6027\u66f4\u5f3a\uff0c\u800c\u4e14\u5f88\u591a\u5730\u65b9\u6211\u4eec\u53ea\u80fd\u901a\u8fc7 <code>@Bean<\/code> \u6ce8\u89e3\u6765\u6ce8\u518c bean\u3002\u6bd4\u5982\u5f53\u6211\u4eec\u5f15\u7528\u7b2c\u4e09\u65b9\u5e93\u4e2d\u7684\u7c7b\u9700\u8981\u88c5\u914d\u5230 <code>Spring<\/code>\u5bb9\u5668\u65f6\uff0c\u5219\u53ea\u80fd\u901a\u8fc7 <code>@Bean<\/code>\u6765\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n<p><code>@Bean<\/code>\u6ce8\u89e3\u4f7f\u7528\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"java\">@Configuration\npublic class AppConfig {\n    @Bean\n    public TransferService transferService() {\n        return new TransferServiceImpl();\n    }\n\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u76f8\u5f53\u4e8e\u4e0b\u9762\u7684 xml \u914d\u7f6e<\/p>\n<pre><code class=\"xml\">&lt;beans&gt;\n    &lt;bean id=&quot;transferService&quot; class=&quot;com.acme.TransferServiceImpl&quot;\/&gt;\n&lt;\/beans&gt;<\/code><\/pre>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u4f8b\u5b50\u662f\u901a\u8fc7 <code>@Component<\/code> \u65e0\u6cd5\u5b9e\u73b0\u7684\u3002<\/p>\n<pre><code class=\"java\">@Bean\npublic OneService getService(status) {\n    case (status)  {\n        when 1:\n                return new serviceImpl1();\n        when 2:\n                return new serviceImpl2();\n        when 3:\n                return new serviceImpl3();\n    }\n}<\/code><\/pre>\n<h3>\u6ce8\u5165 Bean \u7684\u6ce8\u89e3\u6709\u54ea\u4e9b\uff1f<\/h3>\n<p>Spring \u5185\u7f6e\u7684 <code>@Autowired<\/code> \u4ee5\u53ca JDK \u5185\u7f6e\u7684 <code>@Resource<\/code> \u548c <code>@Inject<\/code> \u90fd\u53ef\u4ee5\u7528\u4e8e\u6ce8\u5165 Bean\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>Annotaion<\/th>\n<th>Package<\/th>\n<th>Source<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>@Autowired<\/code><\/td>\n<td><code>org.springframework.bean.factory<\/code><\/td>\n<td>Spring 2.5+<\/td>\n<\/tr>\n<tr>\n<td><code>@Resource<\/code><\/td>\n<td><code>javax.annotation<\/code><\/td>\n<td>Java JSR-250<\/td>\n<\/tr>\n<tr>\n<td><code>@Inject<\/code><\/td>\n<td><code>javax.inject<\/code><\/td>\n<td>Java JSR-330<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><code>@Autowired<\/code> \u548c<code>@Resource<\/code>\u4f7f\u7528\u7684\u6bd4\u8f83\u591a\u4e00\u4e9b\u3002<\/p>\n<h3>@Autowired \u548c @Resource \u7684\u533a\u522b\u662f\u4ec0\u4e48\uff1f<\/h3>\n<p><code>Autowired<\/code> \u5c5e\u4e8e Spring \u5185\u7f6e\u7684\u6ce8\u89e3\uff0c\u9ed8\u8ba4\u7684\u6ce8\u5165\u65b9\u5f0f\u4e3a<code>byType<\/code>\uff08\u6839\u636e\u7c7b\u578b\u8fdb\u884c\u5339\u914d\uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\u4f1a\u4f18\u5148\u6839\u636e\u63a5\u53e3\u7c7b\u578b\u53bb\u5339\u914d\u5e76\u6ce8\u5165 Bean \uff08\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b\uff09\u3002<\/p>\n<p><strong>\u8fd9\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898\u5462\uff1f<\/strong> \u5f53\u4e00\u4e2a\u63a5\u53e3\u5b58\u5728\u591a\u4e2a\u5b9e\u73b0\u7c7b\u7684\u8bdd\uff0c<code>byType<\/code>\u8fd9\u79cd\u65b9\u5f0f\u5c31\u65e0\u6cd5\u6b63\u786e\u6ce8\u5165\u5bf9\u8c61\u4e86\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u65f6\u5019 Spring \u4f1a\u540c\u65f6\u627e\u5230\u591a\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u9009\u62e9\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u5b83\u81ea\u5df1\u4e0d\u77e5\u9053\u9009\u62e9\u54ea\u4e00\u4e2a\u3002<\/p>\n<p>\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u6ce8\u5165\u65b9\u5f0f\u4f1a\u53d8\u4e3a <code>byName<\/code>\uff08\u6839\u636e\u540d\u79f0\u8fdb\u884c\u5339\u914d\uff09\uff0c\u8fd9\u4e2a\u540d\u79f0\u901a\u5e38\u5c31\u662f\u7c7b\u540d\uff08\u9996\u5b57\u6bcd\u5c0f\u5199\uff09\u3002\u5c31\u6bd4\u5982\u8bf4\u4e0b\u9762\u4ee3\u7801\u4e2d\u7684 <code>smsService<\/code> \u5c31\u662f\u6211\u8fd9\u91cc\u6240\u8bf4\u7684\u540d\u79f0\uff0c\u8fd9\u6837\u5e94\u8be5\u6bd4\u8f83\u597d\u7406\u89e3\u4e86\u5427\u3002<\/p>\n<pre><code class=\"java\">\/\/ smsService \u5c31\u662f\u6211\u4eec\u4e0a\u9762\u6240\u8bf4\u7684\u540d\u79f0\n@Autowired\nprivate SmsService smsService;<\/code><\/pre>\n<p>\u4e3e\u4e2a\u4f8b\u5b50\uff0c<code>SmsService<\/code> \u63a5\u53e3\u6709\u4e24\u4e2a\u5b9e\u73b0\u7c7b: <code>SmsServiceImpl1<\/code>\u548c <code>SmsServiceImpl2<\/code>\uff0c\u4e14\u5b83\u4eec\u90fd\u5df2\u7ecf\u88ab Spring \u5bb9\u5668\u6240\u7ba1\u7406\u3002<\/p>\n<pre><code class=\"java\">\/\/ \u62a5\u9519\uff0cbyName \u548c byType \u90fd\u65e0\u6cd5\u5339\u914d\u5230 bean\n@Autowired\nprivate SmsService smsService;\n\/\/ \u6b63\u786e\u6ce8\u5165 SmsServiceImpl1 \u5bf9\u8c61\u5bf9\u5e94\u7684 bean\n@Autowired\nprivate SmsService smsServiceImpl1;\n\/\/ \u6b63\u786e\u6ce8\u5165  SmsServiceImpl1 \u5bf9\u8c61\u5bf9\u5e94\u7684 bean\n\/\/ smsServiceImpl1 \u5c31\u662f\u6211\u4eec\u4e0a\u9762\u6240\u8bf4\u7684\u540d\u79f0\n@Autowired\n@Qualifier(value = &quot;smsServiceImpl1&quot;)\nprivate SmsService smsService;<\/code><\/pre>\n<p>\u6211\u4eec\u8fd8\u662f\u5efa\u8bae\u901a\u8fc7 <code>@Qualifier<\/code> \u6ce8\u89e3\u6765\u663e\u793a\u6307\u5b9a\u540d\u79f0\u800c\u4e0d\u662f\u4f9d\u8d56\u53d8\u91cf\u7684\u540d\u79f0\u3002<\/p>\n<p><code>@Resource<\/code>\u5c5e\u4e8e JDK \u63d0\u4f9b\u7684\u6ce8\u89e3\uff0c\u9ed8\u8ba4\u6ce8\u5165\u65b9\u5f0f\u4e3a <code>byName<\/code>\u3002\u5982\u679c\u65e0\u6cd5\u901a\u8fc7\u540d\u79f0\u5339\u914d\u5230\u5bf9\u5e94\u7684 Bean \u7684\u8bdd\uff0c\u6ce8\u5165\u65b9\u5f0f\u4f1a\u53d8\u4e3a<code>byType<\/code>\u3002<\/p>\n<p><code>@Resource<\/code> \u6709\u4e24\u4e2a\u6bd4\u8f83\u91cd\u8981\u4e14\u65e5\u5e38\u5f00\u53d1\u5e38\u7528\u7684\u5c5e\u6027\uff1a<code>name<\/code>\uff08\u540d\u79f0\uff09\u3001<code>type<\/code>\uff08\u7c7b\u578b\uff09\u3002<\/p>\n<pre><code class=\"java\">public @interface Resource {\n    String name() default &quot;&quot;;\n    Class&lt;?&gt; type() default Object.class;\n}<\/code><\/pre>\n<p>\u5982\u679c\u4ec5\u6307\u5b9a <code>name<\/code> \u5c5e\u6027\u5219\u6ce8\u5165\u65b9\u5f0f\u4e3a<code>byName<\/code>\uff0c\u5982\u679c\u4ec5\u6307\u5b9a<code>type<\/code>\u5c5e\u6027\u5219\u6ce8\u5165\u65b9\u5f0f\u4e3a<code>byType<\/code>\uff0c\u5982\u679c\u540c\u65f6\u6307\u5b9a<code>name<\/code> \u548c<code>type<\/code>\u5c5e\u6027\uff08\u4e0d\u5efa\u8bae\u8fd9\u4e48\u505a\uff09\u5219\u6ce8\u5165\u65b9\u5f0f\u4e3a<code>byType<\/code>+<code>byName<\/code>\u3002<\/p>\n<pre><code class=\"java\">\/\/ \u62a5\u9519\uff0cbyName \u548c byType \u90fd\u65e0\u6cd5\u5339\u914d\u5230 bean\n@Resource\nprivate SmsService smsService;\n\/\/ \u6b63\u786e\u6ce8\u5165 SmsServiceImpl1 \u5bf9\u8c61\u5bf9\u5e94\u7684 bean\n@Resource\nprivate SmsService smsServiceImpl1;\n\/\/ \u6b63\u786e\u6ce8\u5165 SmsServiceImpl1 \u5bf9\u8c61\u5bf9\u5e94\u7684 bean\uff08\u6bd4\u8f83\u63a8\u8350\u8fd9\u79cd\u65b9\u5f0f\uff09\n@Resource(name = &quot;smsServiceImpl1&quot;)\nprivate SmsService smsService;<\/code><\/pre>\n<p>\u7b80\u5355\u603b\u7ed3\u4e00\u4e0b\uff1a<\/p>\n<ul>\n<li><code>@Autowired<\/code> \u662f Spring \u63d0\u4f9b\u7684\u6ce8\u89e3\uff0c<code>@Resource<\/code> \u662f JDK \u63d0\u4f9b\u7684\u6ce8\u89e3\u3002<\/li>\n<li><code>Autowired<\/code> \u9ed8\u8ba4\u7684\u6ce8\u5165\u65b9\u5f0f\u4e3a<code>byType<\/code>\uff08\u6839\u636e\u7c7b\u578b\u8fdb\u884c\u5339\u914d\uff09\uff0c<code>@Resource<\/code>\u9ed8\u8ba4\u6ce8\u5165\u65b9\u5f0f\u4e3a <code>byName<\/code>\uff08\u6839\u636e\u540d\u79f0\u8fdb\u884c\u5339\u914d\uff09\u3002<\/li>\n<li>\u5f53\u4e00\u4e2a\u63a5\u53e3\u5b58\u5728\u591a\u4e2a\u5b9e\u73b0\u7c7b\u7684\u60c5\u51b5\u4e0b\uff0c<code>@Autowired<\/code> \u548c<code>@Resource<\/code>\u90fd\u9700\u8981\u901a\u8fc7\u540d\u79f0\u624d\u80fd\u6b63\u786e\u5339\u914d\u5230\u5bf9\u5e94\u7684 Bean\u3002<code>Autowired<\/code> \u53ef\u4ee5\u901a\u8fc7 <code>@Qualifier<\/code> \u6ce8\u89e3\u6765\u663e\u793a\u6307\u5b9a\u540d\u79f0\uff0c<code>@Resource<\/code>\u53ef\u4ee5\u901a\u8fc7 <code>name<\/code> \u5c5e\u6027\u6765\u663e\u793a\u6307\u5b9a\u540d\u79f0\u3002<\/li>\n<\/ul>\n<h3>Bean \u7684\u4f5c\u7528\u57df\u6709\u54ea\u4e9b?<\/h3>\n<p>Spring \u4e2d Bean \u7684\u4f5c\u7528\u57df\u901a\u5e38\u6709\u4e0b\u9762\u51e0\u79cd\uff1a<\/p>\n<ul>\n<li><strong>singleton<\/strong> : IoC \u5bb9\u5668\u4e2d\u53ea\u6709\u552f\u4e00\u7684 bean \u5b9e\u4f8b\u3002Spring \u4e2d\u7684 bean \u9ed8\u8ba4\u90fd\u662f\u5355\u4f8b\u7684\uff0c\u662f\u5bf9\u5355\u4f8b\u8bbe\u8ba1\u6a21\u5f0f\u7684\u5e94\u7528\u3002<\/li>\n<li><strong>prototype<\/strong> : \u6bcf\u6b21\u83b7\u53d6\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684 bean \u5b9e\u4f8b\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fde\u7eed <code>getBean()<\/code> \u4e24\u6b21\uff0c\u5f97\u5230\u7684\u662f\u4e0d\u540c\u7684 Bean \u5b9e\u4f8b\u3002<\/li>\n<li><strong>request<\/strong> \uff08\u4ec5 Web \u5e94\u7528\u53ef\u7528\uff09: \u6bcf\u4e00\u6b21 HTTP \u8bf7\u6c42\u90fd\u4f1a\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684 bean\uff08\u8bf7\u6c42 bean\uff09\uff0c\u8be5 bean \u4ec5\u5728\u5f53\u524d HTTP request \u5185\u6709\u6548\u3002<\/li>\n<li><strong>session<\/strong> \uff08\u4ec5 Web \u5e94\u7528\u53ef\u7528\uff09 : \u6bcf\u4e00\u6b21\u6765\u81ea\u65b0 session \u7684 HTTP \u8bf7\u6c42\u90fd\u4f1a\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684 bean\uff08\u4f1a\u8bdd bean\uff09\uff0c\u8be5 bean \u4ec5\u5728\u5f53\u524d HTTP session \u5185\u6709\u6548\u3002<\/li>\n<li><strong>application\/global-session<\/strong> \uff08\u4ec5 Web \u5e94\u7528\u53ef\u7528\uff09\uff1a \u6bcf\u4e2a Web \u5e94\u7528\u5728\u542f\u52a8\u65f6\u521b\u5efa\u4e00\u4e2a Bean\uff08\u5e94\u7528 Bean\uff09\uff0c\uff0c\u8be5 bean \u4ec5\u5728\u5f53\u524d\u5e94\u7528\u542f\u52a8\u65f6\u95f4\u5185\u6709\u6548\u3002<\/li>\n<li><strong>websocket<\/strong> \uff08\u4ec5 Web \u5e94\u7528\u53ef\u7528\uff09\uff1a\u6bcf\u4e00\u6b21 WebSocket \u4f1a\u8bdd\u4ea7\u751f\u4e00\u4e2a\u65b0\u7684 bean\u3002<\/li>\n<\/ul>\n<p><strong>\u5982\u4f55\u914d\u7f6e bean \u7684\u4f5c\u7528\u57df\u5462\uff1f<\/strong><\/p>\n<p>xml \u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"xml\">&lt;bean id=&quot;...&quot; class=&quot;...&quot; scope=&quot;singleton&quot;&gt;&lt;\/bean&gt;<\/code><\/pre>\n<p>\u6ce8\u89e3\u65b9\u5f0f\uff1a<\/p>\n<pre><code class=\"java\">@Bean\n@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)\npublic Person personPrototype() {\n    return new Person();\n}<\/code><\/pre>\n<h3>\u5355\u4f8b Bean \u7684\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\u4e86\u89e3\u5417\uff1f<\/h3>\n<p>\u5927\u90e8\u5206\u65f6\u5019\u6211\u4eec\u5e76\u6ca1\u6709\u5728\u9879\u76ee\u4e2d\u4f7f\u7528\u591a\u7ebf\u7a0b\uff0c\u6240\u4ee5\u5f88\u5c11\u6709\u4eba\u4f1a\u5173\u6ce8\u8fd9\u4e2a\u95ee\u9898\u3002\u5355\u4f8b Bean \u5b58\u5728\u7ebf\u7a0b\u95ee\u9898\uff0c\u4e3b\u8981\u662f\u56e0\u4e3a\u5f53\u591a\u4e2a\u7ebf\u7a0b\u64cd\u4f5c\u540c\u4e00\u4e2a\u5bf9\u8c61\u7684\u65f6\u5019\u662f\u5b58\u5728\u8d44\u6e90\u7ade\u4e89\u7684\u3002<\/p>\n<p>\u5e38\u89c1\u7684\u6709\u4e24\u79cd\u89e3\u51b3\u529e\u6cd5\uff1a<\/p>\n<ol>\n<li>\u5728 Bean \u4e2d\u5c3d\u91cf\u907f\u514d\u5b9a\u4e49\u53ef\u53d8\u7684\u6210\u5458\u53d8\u91cf\u3002<\/li>\n<li>\u5728\u7c7b\u4e2d\u5b9a\u4e49\u4e00\u4e2a <code>ThreadLocal<\/code> \u6210\u5458\u53d8\u91cf\uff0c\u5c06\u9700\u8981\u7684\u53ef\u53d8\u6210\u5458\u53d8\u91cf\u4fdd\u5b58\u5728 <code>ThreadLocal<\/code> \u4e2d\uff08\u63a8\u8350\u7684\u4e00\u79cd\u65b9\u5f0f\uff09\u3002<\/li>\n<\/ol>\n<p>\u4e0d\u8fc7\uff0c\u5927\u90e8\u5206 Bean \u5b9e\u9645\u90fd\u662f\u65e0\u72b6\u6001\uff08\u6ca1\u6709\u5b9e\u4f8b\u53d8\u91cf\uff09\u7684\uff08\u6bd4\u5982 Dao\u3001Service\uff09\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c Bean \u662f\u7ebf\u7a0b\u5b89\u5168\u7684\u3002<\/p>\n<h3>Bean \u7684\u751f\u547d\u5468\u671f\u4e86\u89e3\u4e48?<\/h3>\n<blockquote><p>\u4e0b\u9762\u7684\u5185\u5bb9\u6574\u7406\u81ea\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=M%2Bx7B06odQrI%2BvNwNiOcwA%3D%3D.aJ0PRjlBE6NqitU7GGy2PdISHDO8oojRbSETXtdS9uVbiNaxnWkb7Uw8hLpuI0D590vouoxfzCI%2BEprR915tsBOA2YEg6rU84A2jO%2FmZIYq83Y507i14ozkbOZwR6k4ODaLW845bnbExQXEmJAL%2Bd3NSVyRxoGl3mISdGVpPM%2Bg6FnyInsabCeE%2BFN2BCAQwlwgN2RHfcbhg8DOgo6nOmop3sCJGDwFFhkS3CMGOaxU%3D\" rel=\"nofollow\">https:\/\/yemengying.com\/2016\/0&#8230;<\/a> \u3002<\/p><\/blockquote>\n<ul>\n<li>Bean \u5bb9\u5668\u627e\u5230\u914d\u7f6e\u6587\u4ef6\u4e2d Spring Bean \u7684\u5b9a\u4e49\u3002<\/li>\n<li>Bean \u5bb9\u5668\u5229\u7528 Java Reflection API \u521b\u5efa\u4e00\u4e2a Bean \u7684\u5b9e\u4f8b\u3002<\/li>\n<li>\u5982\u679c\u6d89\u53ca\u5230\u4e00\u4e9b\u5c5e\u6027\u503c \u5229\u7528 <code>set()<\/code>\u65b9\u6cd5\u8bbe\u7f6e\u4e00\u4e9b\u5c5e\u6027\u503c\u3002<\/li>\n<li>\u5982\u679c Bean \u5b9e\u73b0\u4e86 <code>BeanNameAware<\/code> \u63a5\u53e3\uff0c\u8c03\u7528 <code>setBeanName()<\/code>\u65b9\u6cd5\uff0c\u4f20\u5165 Bean \u7684\u540d\u5b57\u3002<\/li>\n<li>\u5982\u679c Bean \u5b9e\u73b0\u4e86 <code>BeanClassLoaderAware<\/code> \u63a5\u53e3\uff0c\u8c03\u7528 <code>setBeanClassLoader()<\/code>\u65b9\u6cd5\uff0c\u4f20\u5165 <code>ClassLoader<\/code>\u5bf9\u8c61\u7684\u5b9e\u4f8b\u3002<\/li>\n<li>\u5982\u679c Bean \u5b9e\u73b0\u4e86 <code>BeanFactoryAware<\/code> \u63a5\u53e3\uff0c\u8c03\u7528 <code>setBeanFactory()<\/code>\u65b9\u6cd5\uff0c\u4f20\u5165 <code>BeanFactory<\/code>\u5bf9\u8c61\u7684\u5b9e\u4f8b\u3002<\/li>\n<li>\u4e0e\u4e0a\u9762\u7684\u7c7b\u4f3c\uff0c\u5982\u679c\u5b9e\u73b0\u4e86\u5176\u4ed6 <code>*.Aware<\/code>\u63a5\u53e3\uff0c\u5c31\u8c03\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\u3002<\/li>\n<li>\u5982\u679c\u6709\u548c\u52a0\u8f7d\u8fd9\u4e2a Bean \u7684 Spring \u5bb9\u5668\u76f8\u5173\u7684 <code>BeanPostProcessor<\/code> \u5bf9\u8c61\uff0c\u6267\u884c<code>postProcessBeforeInitialization()<\/code> \u65b9\u6cd5<\/li>\n<li>\u5982\u679c Bean \u5b9e\u73b0\u4e86<code>InitializingBean<\/code>\u63a5\u53e3\uff0c\u6267\u884c<code>afterPropertiesSet()<\/code>\u65b9\u6cd5\u3002<\/li>\n<li>\u5982\u679c Bean \u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u5305\u542b init-method \u5c5e\u6027\uff0c\u6267\u884c\u6307\u5b9a\u7684\u65b9\u6cd5\u3002<\/li>\n<li>\u5982\u679c\u6709\u548c\u52a0\u8f7d\u8fd9\u4e2a Bean \u7684 Spring \u5bb9\u5668\u76f8\u5173\u7684 <code>BeanPostProcessor<\/code> \u5bf9\u8c61\uff0c\u6267\u884c<code>postProcessAfterInitialization()<\/code> \u65b9\u6cd5<\/li>\n<li>\u5f53\u8981\u9500\u6bc1 Bean \u7684\u65f6\u5019\uff0c\u5982\u679c Bean \u5b9e\u73b0\u4e86 <code>DisposableBean<\/code> \u63a5\u53e3\uff0c\u6267\u884c <code>destroy()<\/code> \u65b9\u6cd5\u3002<\/li>\n<li>\u5f53\u8981\u9500\u6bc1 Bean \u7684\u65f6\u5019\uff0c\u5982\u679c Bean \u5728\u914d\u7f6e\u6587\u4ef6\u4e2d\u7684\u5b9a\u4e49\u5305\u542b destroy-method \u5c5e\u6027\uff0c\u6267\u884c\u6307\u5b9a\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p>\u56fe\u793a\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254927\" alt=\"Spring Bean \u751f\u547d\u5468\u671f\" title=\"Spring Bean \u751f\u547d\u5468\u671f\" loading=\"lazy\"><\/p>\n<p>\u4e0e\u4e4b\u6bd4\u8f83\u7c7b\u4f3c\u7684\u4e2d\u6587\u7248\u672c:<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254928\" alt=\"Spring Bean \u751f\u547d\u5468\u671f\" title=\"Spring Bean \u751f\u547d\u5468\u671f\" loading=\"lazy\"><\/p>\n<h2>Spring AoP<\/h2>\n<h3>\u8c08\u8c08\u81ea\u5df1\u5bf9\u4e8e AOP \u7684\u4e86\u89e3<\/h3>\n<p>AOP(Aspect-Oriented Programming:\u9762\u5411\u5207\u9762\u7f16\u7a0b)\u80fd\u591f\u5c06\u90a3\u4e9b\u4e0e\u4e1a\u52a1\u65e0\u5173\uff0c\u5374\u4e3a\u4e1a\u52a1\u6a21\u5757\u6240\u5171\u540c\u8c03\u7528\u7684\u903b\u8f91\u6216\u8d23\u4efb\uff08\u4f8b\u5982\u4e8b\u52a1\u5904\u7406\u3001\u65e5\u5fd7\u7ba1\u7406\u3001\u6743\u9650\u63a7\u5236\u7b49\uff09\u5c01\u88c5\u8d77\u6765\uff0c\u4fbf\u4e8e\u51cf\u5c11\u7cfb\u7edf\u7684\u91cd\u590d\u4ee3\u7801\uff0c\u964d\u4f4e\u6a21\u5757\u95f4\u7684\u8026\u5408\u5ea6\uff0c\u5e76\u6709\u5229\u4e8e\u672a\u6765\u7684\u53ef\u62d3\u5c55\u6027\u548c\u53ef\u7ef4\u62a4\u6027\u3002<\/p>\n<p>Spring AOP \u5c31\u662f\u57fa\u4e8e\u52a8\u6001\u4ee3\u7406\u7684\uff0c\u5982\u679c\u8981\u4ee3\u7406\u7684\u5bf9\u8c61\uff0c\u5b9e\u73b0\u4e86\u67d0\u4e2a\u63a5\u53e3\uff0c\u90a3\u4e48 Spring AOP \u4f1a\u4f7f\u7528 <strong>JDK Proxy<\/strong>\uff0c\u53bb\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61\uff0c\u800c\u5bf9\u4e8e\u6ca1\u6709\u5b9e\u73b0\u63a5\u53e3\u7684\u5bf9\u8c61\uff0c\u5c31\u65e0\u6cd5\u4f7f\u7528 JDK Proxy \u53bb\u8fdb\u884c\u4ee3\u7406\u4e86\uff0c\u8fd9\u65f6\u5019 Spring AOP \u4f1a\u4f7f\u7528 <strong>Cglib<\/strong> \u751f\u6210\u4e00\u4e2a\u88ab\u4ee3\u7406\u5bf9\u8c61\u7684\u5b50\u7c7b\u6765\u4f5c\u4e3a\u4ee3\u7406\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254929\" alt=\"SpringAOPProcess\" title=\"SpringAOPProcess\" loading=\"lazy\"><\/p>\n<p>\u5f53\u7136\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 <strong>AspectJ<\/strong> \uff01Spring AOP \u5df2\u7ecf\u96c6\u6210\u4e86 AspectJ \uff0cAspectJ \u5e94\u8be5\u7b97\u7684\u4e0a\u662f Java \u751f\u6001\u7cfb\u7edf\u4e2d\u6700\u5b8c\u6574\u7684 AOP \u6846\u67b6\u4e86\u3002<\/p>\n<p>AOP \u5207\u9762\u7f16\u7a0b\u8bbe\u8ba1\u5230\u7684\u4e00\u4e9b\u4e13\u4e1a\u672f\u8bed\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th align=\"left\">\u672f\u8bed<\/th>\n<th align=\"center\">\u542b\u4e49<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td align=\"left\">\u76ee\u6807(Target)<\/td>\n<td align=\"center\">\u88ab\u901a\u77e5\u7684\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">\u4ee3\u7406(Proxy)<\/td>\n<td align=\"center\">\u5411\u76ee\u6807\u5bf9\u8c61\u5e94\u7528\u901a\u77e5\u4e4b\u540e\u521b\u5efa\u7684\u4ee3\u7406\u5bf9\u8c61<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">\u8fde\u63a5\u70b9(JoinPoint)<\/td>\n<td align=\"center\">\u76ee\u6807\u5bf9\u8c61\u7684\u6240\u5c5e\u7c7b\u4e2d\uff0c\u5b9a\u4e49\u7684\u6240\u6709\u65b9\u6cd5\u5747\u4e3a\u8fde\u63a5\u70b9<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">\u5207\u5165\u70b9(Pointcut)<\/td>\n<td align=\"center\">\u88ab\u5207\u9762\u62e6\u622a \/ \u589e\u5f3a\u7684\u8fde\u63a5\u70b9\uff08\u5207\u5165\u70b9\u4e00\u5b9a\u662f\u8fde\u63a5\u70b9\uff0c\u8fde\u63a5\u70b9\u4e0d\u4e00\u5b9a\u662f\u5207\u5165\u70b9\uff09<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">\u901a\u77e5(Advice)<\/td>\n<td align=\"center\">\u589e\u5f3a\u7684\u903b\u8f91 \/ \u4ee3\u7801\uff0c\u4e5f\u5373\u62e6\u622a\u5230\u76ee\u6807\u5bf9\u8c61\u7684\u8fde\u63a5\u70b9\u4e4b\u540e\u8981\u505a\u7684\u4e8b\u60c5<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">\u5207\u9762(Aspect)<\/td>\n<td align=\"center\">\u5207\u5165\u70b9(Pointcut)+\u901a\u77e5(Advice)<\/td>\n<\/tr>\n<tr>\n<td align=\"left\">Weaving(\u7ec7\u5165)<\/td>\n<td align=\"center\">\u5c06\u901a\u77e5\u5e94\u7528\u5230\u76ee\u6807\u5bf9\u8c61\uff0c\u8fdb\u800c\u751f\u6210\u4ee3\u7406\u5bf9\u8c61\u7684\u8fc7\u7a0b\u52a8\u4f5c<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3>Spring AOP \u548c AspectJ AOP \u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/h3>\n<p><strong>Spring AOP \u5c5e\u4e8e\u8fd0\u884c\u65f6\u589e\u5f3a\uff0c\u800c AspectJ \u662f\u7f16\u8bd1\u65f6\u589e\u5f3a\u3002<\/strong> Spring AOP \u57fa\u4e8e\u4ee3\u7406(Proxying)\uff0c\u800c AspectJ \u57fa\u4e8e\u5b57\u8282\u7801\u64cd\u4f5c(Bytecode Manipulation)\u3002<\/p>\n<p>Spring AOP \u5df2\u7ecf\u96c6\u6210\u4e86 AspectJ \uff0cAspectJ \u5e94\u8be5\u7b97\u7684\u4e0a\u662f Java \u751f\u6001\u7cfb\u7edf\u4e2d\u6700\u5b8c\u6574\u7684 AOP \u6846\u67b6\u4e86\u3002AspectJ \u76f8\u6bd4\u4e8e Spring AOP \u529f\u80fd\u66f4\u52a0\u5f3a\u5927\uff0c\u4f46\u662f Spring AOP \u76f8\u5bf9\u6765\u8bf4\u66f4\u7b80\u5355\uff0c<\/p>\n<p>\u5982\u679c\u6211\u4eec\u7684\u5207\u9762\u6bd4\u8f83\u5c11\uff0c\u90a3\u4e48\u4e24\u8005\u6027\u80fd\u5dee\u5f02\u4e0d\u5927\u3002\u4f46\u662f\uff0c\u5f53\u5207\u9762\u592a\u591a\u7684\u8bdd\uff0c\u6700\u597d\u9009\u62e9 AspectJ \uff0c\u5b83\u6bd4 Spring AOP \u5feb\u5f88\u591a\u3002<\/p>\n<h3>AspectJ \u5b9a\u4e49\u7684\u901a\u77e5\u7c7b\u578b\u6709\u54ea\u4e9b\uff1f<\/h3>\n<ul>\n<li><strong>Before<\/strong>\uff08\u524d\u7f6e\u901a\u77e5\uff09\uff1a\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\u8c03\u7528\u4e4b\u524d\u89e6\u53d1<\/li>\n<li><strong>After<\/strong> \uff08\u540e\u7f6e\u901a\u77e5\uff09\uff1a\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\u8c03\u7528\u4e4b\u540e\u89e6\u53d1<\/li>\n<li><strong>AfterReturning<\/strong>\uff08\u8fd4\u56de\u901a\u77e5\uff09\uff1a\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\u8c03\u7528\u5b8c\u6210\uff0c\u5728\u8fd4\u56de\u7ed3\u679c\u503c\u4e4b\u540e\u89e6\u53d1<\/li>\n<li><strong>AfterThrowing<\/strong>\uff08\u5f02\u5e38\u901a\u77e5\uff09 \uff1a\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\u8fd0\u884c\u4e2d\u629b\u51fa \/ \u89e6\u53d1\u5f02\u5e38\u540e\u89e6\u53d1\u3002AfterReturning \u548c AfterThrowing \u4e24\u8005\u4e92\u65a5\u3002\u5982\u679c\u65b9\u6cd5\u8c03\u7528\u6210\u529f\u65e0\u5f02\u5e38\uff0c\u5219\u4f1a\u6709\u8fd4\u56de\u503c\uff1b\u5982\u679c\u65b9\u6cd5\u629b\u51fa\u4e86\u5f02\u5e38\uff0c\u5219\u4e0d\u4f1a\u6709\u8fd4\u56de\u503c\u3002<\/li>\n<li><strong>Around<\/strong>\uff1a \uff08\u73af\u7ed5\u901a\u77e5\uff09\u7f16\u7a0b\u5f0f\u63a7\u5236\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\u8c03\u7528\u3002\u73af\u7ed5\u901a\u77e5\u662f\u6240\u6709\u901a\u77e5\u7c7b\u578b\u4e2d\u53ef\u64cd\u4f5c\u8303\u56f4\u6700\u5927\u7684\u4e00\u79cd\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u76f4\u63a5\u62ff\u5230\u76ee\u6807\u5bf9\u8c61\uff0c\u4ee5\u53ca\u8981\u6267\u884c\u7684\u65b9\u6cd5\uff0c\u6240\u4ee5\u73af\u7ed5\u901a\u77e5\u53ef\u4ee5\u4efb\u610f\u7684\u5728\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\u8c03\u7528\u524d\u540e\u641e\u4e8b\uff0c\u751a\u81f3\u4e0d\u8c03\u7528\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5<\/li>\n<\/ul>\n<h3>\u591a\u4e2a\u5207\u9762\u7684\u6267\u884c\u987a\u5e8f\u5982\u4f55\u63a7\u5236\uff1f<\/h3>\n<p>1\u3001\u901a\u5e38\u4f7f\u7528<code>@Order<\/code> \u6ce8\u89e3\u76f4\u63a5\u5b9a\u4e49\u5207\u9762\u987a\u5e8f<\/p>\n<pre><code class=\"java\">\/\/ \u503c\u8d8a\u5c0f\u4f18\u5148\u7ea7\u8d8a\u9ad8\n@Order(3)\n@Component\n@Aspect\npublic class LoggingAspect implements Ordered {<\/code><\/pre>\n<p><strong>2\u3001\u5b9e\u73b0<code>Ordered<\/code> \u63a5\u53e3\u91cd\u5199 <code>getOrder<\/code> \u65b9\u6cd5\u3002<\/strong><\/p>\n<pre><code class=\"java\">@Component\n@Aspect\npublic class LoggingAspect implements Ordered {\n\n    \/\/ ....\n\n    @Override\n    public int getOrder() {\n        \/\/ \u8fd4\u56de\u503c\u8d8a\u5c0f\u4f18\u5148\u7ea7\u8d8a\u9ad8\n        return 1;\n    }\n}<\/code><\/pre>\n<h2>Spring MVC<\/h2>\n<h3>\u8bf4\u8bf4\u81ea\u5df1\u5bf9\u4e8e Spring MVC \u4e86\u89e3?<\/h3>\n<p>MVC \u662f\u6a21\u578b(Model)\u3001\u89c6\u56fe(View)\u3001\u63a7\u5236\u5668(Controller)\u7684\u7b80\u5199\uff0c\u5176\u6838\u5fc3\u601d\u60f3\u662f\u901a\u8fc7\u5c06\u4e1a\u52a1\u903b\u8f91\u3001\u6570\u636e\u3001\u663e\u793a\u5206\u79bb\u6765\u7ec4\u7ec7\u4ee3\u7801\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254930\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u7f51\u4e0a\u6709\u5f88\u591a\u4eba\u8bf4 MVC \u4e0d\u662f\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u53ea\u662f\u8f6f\u4ef6\u8bbe\u8ba1\u89c4\u8303\uff0c\u6211\u4e2a\u4eba\u66f4\u503e\u5411\u4e8e MVC \u540c\u6837\u662f\u4f17\u591a\u8bbe\u8ba1\u6a21\u5f0f\u4e2d\u7684\u4e00\u79cd\u3002<strong><a href=\"https:\/\/link.segmentfault.com\/?enc=tJ%2FUU%2F4q4KF0%2BNkRhsQeKQ%3D%3D.5CXQYoLNYffHiqYZ0jw4rYpWhda%2BhsfFDi5Zj%2BfHnX3Bd9vBqO6DTo6OdPEFJ5OjZWI%2BY3pD%2B9muh7e7sXnUNg%3D%3D\" rel=\"nofollow\">java-design-patterns<\/a><\/strong> \u9879\u76ee\u4e2d\u5c31\u6709\u5173\u4e8e MVC \u7684\u76f8\u5173\u4ecb\u7ecd\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254931\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u60f3\u8981\u771f\u6b63\u7406\u89e3 Spring MVC\uff0c\u6211\u4eec\u5148\u6765\u770b\u770b Model 1 \u548c Model 2 \u8fd9\u4e24\u4e2a\u6ca1\u6709 Spring MVC \u7684\u65f6\u4ee3\u3002<\/p>\n<p><strong>Model 1 \u65f6\u4ee3<\/strong><\/p>\n<p>\u5f88\u591a\u5b66 Java \u540e\u7aef\u6bd4\u8f83\u665a\u7684\u670b\u53cb\u53ef\u80fd\u5e76\u6ca1\u6709\u63a5\u89e6\u8fc7 Model 1 \u65f6\u4ee3\u4e0b\u7684 JavaWeb \u5e94\u7528\u5f00\u53d1\u3002\u5728 Model1 \u6a21\u5f0f\u4e0b\uff0c\u6574\u4e2a Web \u5e94\u7528\u51e0\u4e4e\u5168\u90e8\u7528 JSP \u9875\u9762\u7ec4\u6210\uff0c\u53ea\u7528\u5c11\u91cf\u7684 JavaBean \u6765\u5904\u7406\u6570\u636e\u5e93\u8fde\u63a5\u3001\u8bbf\u95ee\u7b49\u64cd\u4f5c\u3002<\/p>\n<p>\u8fd9\u4e2a\u6a21\u5f0f\u4e0b JSP \u5373\u662f\u63a7\u5236\u5c42\uff08Controller\uff09\u53c8\u662f\u8868\u73b0\u5c42\uff08View\uff09\u3002\u663e\u800c\u6613\u89c1\uff0c\u8fd9\u79cd\u6a21\u5f0f\u5b58\u5728\u5f88\u591a\u95ee\u9898\u3002\u6bd4\u5982\u63a7\u5236\u903b\u8f91\u548c\u8868\u73b0\u903b\u8f91\u6df7\u6742\u5728\u4e00\u8d77\uff0c\u5bfc\u81f4\u4ee3\u7801\u91cd\u7528\u7387\u6781\u4f4e\uff1b\u518d\u6bd4\u5982\u524d\u7aef\u548c\u540e\u7aef\u76f8\u4e92\u4f9d\u8d56\uff0c\u96be\u4ee5\u8fdb\u884c\u6d4b\u8bd5\u7ef4\u62a4\u5e76\u4e14\u5f00\u53d1\u6548\u7387\u6781\u4f4e\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254932\" alt=\"mvc-mode1\" title=\"mvc-mode1\" loading=\"lazy\"><\/p>\n<p><strong>Model 2 \u65f6\u4ee3<\/strong><\/p>\n<p>\u5b66\u8fc7 Servlet \u5e76\u505a\u8fc7\u76f8\u5173 Demo \u7684\u670b\u53cb\u5e94\u8be5\u4e86\u89e3\u201cJava Bean(Model)+ JSP\uff08View\uff09+Servlet\uff08Controller\uff09 \u201d\u8fd9\u79cd\u5f00\u53d1\u6a21\u5f0f\uff0c\u8fd9\u5c31\u662f\u65e9\u671f\u7684 JavaWeb MVC \u5f00\u53d1\u6a21\u5f0f\u3002<\/p>\n<ul>\n<li>Model:\u7cfb\u7edf\u6d89\u53ca\u7684\u6570\u636e\uff0c\u4e5f\u5c31\u662f dao \u548c bean\u3002<\/li>\n<li>View\uff1a\u5c55\u793a\u6a21\u578b\u4e2d\u7684\u6570\u636e\uff0c\u53ea\u662f\u7528\u6765\u5c55\u793a\u3002<\/li>\n<li>Controller\uff1a\u5904\u7406\u7528\u6237\u8bf7\u6c42\u90fd\u53d1\u9001\u7ed9 \uff0c\u8fd4\u56de\u6570\u636e\u7ed9 JSP \u5e76\u5c55\u793a\u7ed9\u7528\u6237\u3002<\/li>\n<\/ul>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254933\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>Model2 \u6a21\u5f0f\u4e0b\u8fd8\u5b58\u5728\u5f88\u591a\u95ee\u9898\uff0cModel2 \u7684\u62bd\u8c61\u548c\u5c01\u88c5\u7a0b\u5ea6\u8fd8\u8fdc\u8fdc\u4e0d\u591f\uff0c\u4f7f\u7528 Model2 \u8fdb\u884c\u5f00\u53d1\u65f6\u4e0d\u53ef\u907f\u514d\u5730\u4f1a\u91cd\u590d\u9020\u8f6e\u5b50\uff0c\u8fd9\u5c31\u5927\u5927\u964d\u4f4e\u4e86\u7a0b\u5e8f\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u590d\u7528\u6027\u3002<\/p>\n<p>\u4e8e\u662f\uff0c\u5f88\u591a JavaWeb \u5f00\u53d1\u76f8\u5173\u7684 MVC \u6846\u67b6\u5e94\u8fd0\u800c\u751f\u6bd4\u5982 Struts2\uff0c\u4f46\u662f Struts2 \u6bd4\u8f83\u7b28\u91cd\u3002<\/p>\n<p><strong>Spring MVC \u65f6\u4ee3<\/strong><\/p>\n<p>\u968f\u7740 Spring \u8f7b\u91cf\u7ea7\u5f00\u53d1\u6846\u67b6\u7684\u6d41\u884c\uff0cSpring \u751f\u6001\u5708\u51fa\u73b0\u4e86 Spring MVC \u6846\u67b6\uff0c Spring MVC \u662f\u5f53\u524d\u6700\u4f18\u79c0\u7684 MVC \u6846\u67b6\u3002\u76f8\u6bd4\u4e8e Struts2 \uff0c Spring MVC \u4f7f\u7528\u66f4\u52a0\u7b80\u5355\u548c\u65b9\u4fbf\uff0c\u5f00\u53d1\u6548\u7387\u66f4\u9ad8\uff0c\u5e76\u4e14 Spring MVC \u8fd0\u884c\u901f\u5ea6\u66f4\u5feb\u3002<\/p>\n<p>MVC \u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0cSpring MVC \u662f\u4e00\u6b3e\u5f88\u4f18\u79c0\u7684 MVC \u6846\u67b6\u3002Spring MVC \u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u8fdb\u884c\u66f4\u7b80\u6d01\u7684 Web \u5c42\u7684\u5f00\u53d1\uff0c\u5e76\u4e14\u5b83\u5929\u751f\u4e0e Spring \u6846\u67b6\u96c6\u6210\u3002Spring MVC \u4e0b\u6211\u4eec\u4e00\u822c\u628a\u540e\u7aef\u9879\u76ee\u5206\u4e3a Service \u5c42\uff08\u5904\u7406\u4e1a\u52a1\uff09\u3001Dao \u5c42\uff08\u6570\u636e\u5e93\u64cd\u4f5c\uff09\u3001Entity \u5c42\uff08\u5b9e\u4f53\u7c7b\uff09\u3001Controller \u5c42(\u63a7\u5236\u5c42\uff0c\u8fd4\u56de\u6570\u636e\u7ed9\u524d\u53f0\u9875\u9762)\u3002<\/p>\n<h3>Spring MVC \u7684\u6838\u5fc3\u7ec4\u4ef6\u6709\u54ea\u4e9b\uff1f<\/h3>\n<p>\u8bb0\u4f4f\u4e86\u4e0b\u9762\u8fd9\u4e9b\u7ec4\u4ef6\uff0c\u4e5f\u5c31\u8bb0\u4f4f\u4e86 SpringMVC \u7684\u5de5\u4f5c\u539f\u7406\u3002<\/p>\n<ul>\n<li><strong><code>DispatcherServlet<\/code><\/strong> \uff1a<strong>\u6838\u5fc3\u7684\u4e2d\u592e\u5904\u7406\u5668<\/strong>\uff0c\u8d1f\u8d23\u63a5\u6536\u8bf7\u6c42\u3001\u5206\u53d1\uff0c\u5e76\u7ed9\u4e88\u5ba2\u6237\u7aef\u54cd\u5e94\u3002<\/li>\n<li><strong><code>HandlerMapping<\/code><\/strong> \uff1a<strong>\u5904\u7406\u5668\u6620\u5c04\u5668<\/strong>\uff0c\u6839\u636e uri \u53bb\u5339\u914d\u67e5\u627e\u80fd\u5904\u7406\u7684 <code>Handler<\/code> \uff0c\u5e76\u4f1a\u5c06\u8bf7\u6c42\u6d89\u53ca\u5230\u7684\u62e6\u622a\u5668\u548c <code>Handler<\/code> \u4e00\u8d77\u5c01\u88c5\u3002<\/li>\n<li><strong><code>HandlerAdapter<\/code><\/strong> \uff1a<strong>\u5904\u7406\u5668\u9002\u914d\u5668<\/strong>\uff0c\u6839\u636e <code>HandlerMapping<\/code> \u627e\u5230\u7684 <code>Handler<\/code> \uff0c\u9002\u914d\u6267\u884c\u5bf9\u5e94\u7684 <code>Handler<\/code>\uff1b<\/li>\n<li><strong><code>Handler<\/code><\/strong> \uff1a<strong>\u8bf7\u6c42\u5904\u7406\u5668<\/strong>\uff0c\u5904\u7406\u5b9e\u9645\u8bf7\u6c42\u7684\u5904\u7406\u5668\u3002<\/li>\n<li><strong><code>ViewResolver<\/code><\/strong> \uff1a<strong>\u89c6\u56fe\u89e3\u6790\u5668<\/strong>\uff0c\u6839\u636e <code>Handler<\/code> \u8fd4\u56de\u7684\u903b\u8f91\u89c6\u56fe \/ \u89c6\u56fe\uff0c\u89e3\u6790\u5e76\u6e32\u67d3\u771f\u6b63\u7684\u89c6\u56fe\uff0c\u5e76\u4f20\u9012\u7ed9 <code>DispatcherServlet<\/code> \u54cd\u5e94\u5ba2\u6237\u7aef<\/li>\n<\/ul>\n<h3>SpringMVC \u5de5\u4f5c\u539f\u7406\u4e86\u89e3\u5417?<\/h3>\n<p><strong>Spring MVC \u539f\u7406\u5982\u4e0b\u56fe\u6240\u793a\uff1a<\/strong><\/p>\n<blockquote><p>SpringMVC \u5de5\u4f5c\u539f\u7406\u7684\u56fe\u89e3\u6211\u6ca1\u6709\u81ea\u5df1\u753b\uff0c\u76f4\u63a5\u56fe\u7701\u4e8b\u5728\u7f51\u4e0a\u627e\u4e86\u4e00\u4e2a\u975e\u5e38\u6e05\u6670\u76f4\u89c2\u7684\uff0c\u539f\u51fa\u5904\u4e0d\u660e\u3002<\/p><\/blockquote>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254934\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p><strong>\u6d41\u7a0b\u8bf4\u660e\uff08\u91cd\u8981\uff09\uff1a<\/strong><\/p>\n<ol>\n<li>\u5ba2\u6237\u7aef\uff08\u6d4f\u89c8\u5668\uff09\u53d1\u9001\u8bf7\u6c42\uff0c <code>DispatcherServlet<\/code>\u62e6\u622a\u8bf7\u6c42\u3002<\/li>\n<li><code>DispatcherServlet<\/code> \u6839\u636e\u8bf7\u6c42\u4fe1\u606f\u8c03\u7528 <code>HandlerMapping<\/code> \u3002<code>HandlerMapping<\/code> \u6839\u636e uri \u53bb\u5339\u914d\u67e5\u627e\u80fd\u5904\u7406\u7684 <code>Handler<\/code>\uff08\u4e5f\u5c31\u662f\u6211\u4eec\u5e73\u5e38\u8bf4\u7684 <code>Controller<\/code> \u63a7\u5236\u5668\uff09 \uff0c\u5e76\u4f1a\u5c06\u8bf7\u6c42\u6d89\u53ca\u5230\u7684\u62e6\u622a\u5668\u548c <code>Handler<\/code> \u4e00\u8d77\u5c01\u88c5\u3002<\/li>\n<li><code>DispatcherServlet<\/code> \u8c03\u7528 <code>HandlerAdapter<\/code>\u9002\u914d\u6267\u884c <code>Handler<\/code> \u3002<\/li>\n<li><code>Handler<\/code> \u5b8c\u6210\u5bf9\u7528\u6237\u8bf7\u6c42\u7684\u5904\u7406\u540e\uff0c\u4f1a\u8fd4\u56de\u4e00\u4e2a <code>ModelAndView<\/code> \u5bf9\u8c61\u7ed9<code>DispatcherServlet<\/code>\uff0c<code>ModelAndView<\/code> \u987e\u540d\u601d\u4e49\uff0c\u5305\u542b\u4e86\u6570\u636e\u6a21\u578b\u4ee5\u53ca\u76f8\u5e94\u7684\u89c6\u56fe\u7684\u4fe1\u606f\u3002<code>Model<\/code> \u662f\u8fd4\u56de\u7684\u6570\u636e\u5bf9\u8c61\uff0c<code>View<\/code> \u662f\u4e2a\u903b\u8f91\u4e0a\u7684 <code>View<\/code>\u3002<\/li>\n<li><code>ViewResolver<\/code> \u4f1a\u6839\u636e\u903b\u8f91 <code>View<\/code> \u67e5\u627e\u5b9e\u9645\u7684 <code>View<\/code>\u3002<\/li>\n<li><code>DispaterServlet<\/code> \u628a\u8fd4\u56de\u7684 <code>Model<\/code> \u4f20\u7ed9 <code>View<\/code>\uff08\u89c6\u56fe\u6e32\u67d3\uff09\u3002<\/li>\n<li>\u628a <code>View<\/code> \u8fd4\u56de\u7ed9\u8bf7\u6c42\u8005\uff08\u6d4f\u89c8\u5668\uff09<\/li>\n<\/ol>\n<h3>\u7edf\u4e00\u5f02\u5e38\u5904\u7406\u600e\u4e48\u505a\uff1f<\/h3>\n<p>\u63a8\u8350\u4f7f\u7528\u6ce8\u89e3\u7684\u65b9\u5f0f\u7edf\u4e00\u5f02\u5e38\u5904\u7406\uff0c\u5177\u4f53\u4f1a\u4f7f\u7528\u5230 <code>@ControllerAdvice<\/code> + <code>@ExceptionHandler<\/code> \u8fd9\u4e24\u4e2a\u6ce8\u89e3 \u3002<\/p>\n<pre><code class=\"java\">@ControllerAdvice\n@ResponseBody\npublic class GlobalExceptionHandler {\n\n    @ExceptionHandler(BaseException.class)\n    public ResponseEntity&lt;?&gt; handleAppException(BaseException ex, HttpServletRequest request) {\n      \/\/......\n    }\n\n    @ExceptionHandler(value = ResourceNotFoundException.class)\n    public ResponseEntity&lt;ErrorReponse&gt; handleResourceNotFoundException(ResourceNotFoundException ex, HttpServletRequest request) {\n      \/\/......\n    }\n}<\/code><\/pre>\n<p>\u8fd9\u79cd\u5f02\u5e38\u5904\u7406\u65b9\u5f0f\u4e0b\uff0c\u4f1a\u7ed9\u6240\u6709\u6216\u8005\u6307\u5b9a\u7684 <code>Controller<\/code> \u7ec7\u5165\u5f02\u5e38\u5904\u7406\u7684\u903b\u8f91\uff08AOP\uff09\uff0c\u5f53 <code>Controller<\/code> \u4e2d\u7684\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38\u7684\u65f6\u5019\uff0c\u7531\u88ab<code>@ExceptionHandler<\/code> \u6ce8\u89e3\u4fee\u9970\u7684\u65b9\u6cd5\u8fdb\u884c\u5904\u7406\u3002<\/p>\n<p><code>ExceptionHandlerMethodResolver<\/code> \u4e2d <code>getMappedMethod<\/code> \u65b9\u6cd5\u51b3\u5b9a\u4e86\u5f02\u5e38\u5177\u4f53\u88ab\u54ea\u4e2a\u88ab <code>@ExceptionHandler<\/code> \u6ce8\u89e3\u4fee\u9970\u7684\u65b9\u6cd5\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<pre><code class=\"java\">@Nullable\n    private Method getMappedMethod(Class&lt;? extends Throwable&gt; exceptionType) {\n        List&lt;Class&lt;? extends Throwable&gt;&gt; matches = new ArrayList&lt;&gt;();\n    \/\/\u627e\u5230\u53ef\u4ee5\u5904\u7406\u7684\u6240\u6709\u5f02\u5e38\u4fe1\u606f\u3002mappedMethods \u4e2d\u5b58\u653e\u4e86\u5f02\u5e38\u548c\u5904\u7406\u5f02\u5e38\u7684\u65b9\u6cd5\u7684\u5bf9\u5e94\u5173\u7cfb\n        for (Class&lt;? extends Throwable&gt; mappedException : this.mappedMethods.keySet()) {\n            if (mappedException.isAssignableFrom(exceptionType)) {\n                matches.add(mappedException);\n            }\n        }\n    \/\/ \u4e0d\u4e3a\u7a7a\u8bf4\u660e\u6709\u65b9\u6cd5\u5904\u7406\u5f02\u5e38\n        if (!matches.isEmpty()) {\n      \/\/ \u6309\u7167\u5339\u914d\u7a0b\u5ea6\u4ece\u5c0f\u5230\u5927\u6392\u5e8f\n            matches.sort(new ExceptionDepthComparator(exceptionType));\n      \/\/ \u8fd4\u56de\u5904\u7406\u5f02\u5e38\u7684\u65b9\u6cd5\n            return this.mappedMethods.get(matches.get(0));\n        }\n        else {\n            return null;\n        }\n    }<\/code><\/pre>\n<p>\u4ece\u6e90\u4ee3\u7801\u770b\u51fa\uff1a<strong><code>getMappedMethod()<\/code>\u4f1a\u9996\u5148\u627e\u5230\u53ef\u4ee5\u5339\u914d\u5904\u7406\u5f02\u5e38\u7684\u6240\u6709\u65b9\u6cd5\u4fe1\u606f\uff0c\u7136\u540e\u5bf9\u5176\u8fdb\u884c\u4ece\u5c0f\u5230\u5927\u7684\u6392\u5e8f\uff0c\u6700\u540e\u53d6\u6700\u5c0f\u7684\u90a3\u4e00\u4e2a\u5339\u914d\u7684\u65b9\u6cd5(\u5373\u5339\u914d\u5ea6\u6700\u9ad8\u7684\u90a3\u4e2a)\u3002<\/strong><\/p>\n<h2>Spring \u6846\u67b6\u4e2d\u7528\u5230\u4e86\u54ea\u4e9b\u8bbe\u8ba1\u6a21\u5f0f\uff1f<\/h2>\n<p>\u5173\u4e8e\u4e0b\u9762\u4e00\u4e9b\u8bbe\u8ba1\u6a21\u5f0f\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u53ef\u4ee5\u770b\u7b14\u4e3b\u524d\u6bb5\u65f6\u95f4\u7684\u539f\u521b\u6587\u7ae0<a href=\"https:\/\/link.segmentfault.com\/?enc=VN3dg3hJ04QFOolikqEGAw%3D%3D.vei05uJj%2FQSR6FlH7Sq%2BRWVy7EGvUJ9DSTW%2BorLRHxxOnsUDfEET3D7wFNvTChi%2BLOzUEyXpapMg08jSHyuj1XY9Pljp560WTu0ltKApUM5of0JpSwuMLQJvAwyvO4zybVfmE13klfn9xmmSWh0rXObBzero7j3TGGspWL6rZ0UKlD0el9DghsN%2B%2Fs6O4XAlyq0j%2FjCyefHpthmL%2B6nHD3BjDo8Bz%2FuTJk7%2BlFTZVeBEbQh1PMYF11%2BoR8vG7FJuLJxBzQghPZop4s87xc7eBdhzZjdjFpd3a3kelQ2EEuQ%3D\" rel=\"nofollow\">\u300a\u9762\u8bd5\u5b98:\u201c\u8c08\u8c08 Spring \u4e2d\u90fd\u7528\u5230\u4e86\u90a3\u4e9b\u8bbe\u8ba1\u6a21\u5f0f?\u201d\u3002\u300b<\/a> \u3002<\/p>\n<ul>\n<li><strong>\u5de5\u5382\u8bbe\u8ba1\u6a21\u5f0f<\/strong> : Spring \u4f7f\u7528\u5de5\u5382\u6a21\u5f0f\u901a\u8fc7 <code>BeanFactory<\/code>\u3001<code>ApplicationContext<\/code> \u521b\u5efa bean \u5bf9\u8c61\u3002<\/li>\n<li><strong>\u4ee3\u7406\u8bbe\u8ba1\u6a21\u5f0f<\/strong> : Spring AOP \u529f\u80fd\u7684\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u5355\u4f8b\u8bbe\u8ba1\u6a21\u5f0f<\/strong> : Spring \u4e2d\u7684 Bean \u9ed8\u8ba4\u90fd\u662f\u5355\u4f8b\u7684\u3002<\/li>\n<li><strong>\u6a21\u677f\u65b9\u6cd5\u6a21\u5f0f<\/strong> : Spring \u4e2d <code>jdbcTemplate<\/code>\u3001<code>hibernateTemplate<\/code> \u7b49\u4ee5 Template \u7ed3\u5c3e\u7684\u5bf9\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u7c7b\uff0c\u5b83\u4eec\u5c31\u4f7f\u7528\u5230\u4e86\u6a21\u677f\u6a21\u5f0f\u3002<\/li>\n<li><strong>\u5305\u88c5\u5668\u8bbe\u8ba1\u6a21\u5f0f<\/strong> : \u6211\u4eec\u7684\u9879\u76ee\u9700\u8981\u8fde\u63a5\u591a\u4e2a\u6570\u636e\u5e93\uff0c\u800c\u4e14\u4e0d\u540c\u7684\u5ba2\u6237\u5728\u6bcf\u6b21\u8bbf\u95ee\u4e2d\u6839\u636e\u9700\u8981\u4f1a\u53bb\u8bbf\u95ee\u4e0d\u540c\u7684\u6570\u636e\u5e93\u3002\u8fd9\u79cd\u6a21\u5f0f\u8ba9\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5ba2\u6237\u7684\u9700\u6c42\u80fd\u591f\u52a8\u6001\u5207\u6362\u4e0d\u540c\u7684\u6570\u636e\u6e90\u3002<\/li>\n<li><strong>\u89c2\u5bdf\u8005\u6a21\u5f0f:<\/strong> Spring \u4e8b\u4ef6\u9a71\u52a8\u6a21\u578b\u5c31\u662f\u89c2\u5bdf\u8005\u6a21\u5f0f\u5f88\u7ecf\u5178\u7684\u4e00\u4e2a\u5e94\u7528\u3002<\/li>\n<li><strong>\u9002\u914d\u5668\u6a21\u5f0f<\/strong> : Spring AOP \u7684\u589e\u5f3a\u6216\u901a\u77e5(Advice)\u4f7f\u7528\u5230\u4e86\u9002\u914d\u5668\u6a21\u5f0f\u3001spring MVC \u4e2d\u4e5f\u662f\u7528\u5230\u4e86\u9002\u914d\u5668\u6a21\u5f0f\u9002\u914d<code>Controller<\/code>\u3002<\/li>\n<li>&#8230;&#8230;<\/li>\n<\/ul>\n<h2>Spring \u4e8b\u52a1<\/h2>\n<p>Spring\/SpringBoot \u6a21\u5757\u4e0b\u4e13\u95e8\u6709\u4e00\u7bc7\u662f\u8bb2 Spring \u4e8b\u52a1\u7684\uff0c\u603b\u7ed3\u7684\u975e\u5e38\u8be6\u7ec6\uff0c\u901a\u4fd7\u6613\u61c2\u3002<\/p>\n<h3>Spring \u7ba1\u7406\u4e8b\u52a1\u7684\u65b9\u5f0f\u6709\u51e0\u79cd\uff1f<\/h3>\n<ul>\n<li><strong>\u7f16\u7a0b\u5f0f\u4e8b\u52a1<\/strong> \uff1a \u5728\u4ee3\u7801\u4e2d\u786c\u7f16\u7801(\u4e0d\u63a8\u8350\u4f7f\u7528) : \u901a\u8fc7 <code>TransactionTemplate<\/code>\u6216\u8005 <code>TransactionManager<\/code> \u624b\u52a8\u7ba1\u7406\u4e8b\u52a1\uff0c\u5b9e\u9645\u5e94\u7528\u4e2d\u5f88\u5c11\u4f7f\u7528\uff0c\u4f46\u662f\u5bf9\u4e8e\u4f60\u7406\u89e3 Spring \u4e8b\u52a1\u7ba1\u7406\u539f\u7406\u6709\u5e2e\u52a9\u3002<\/li>\n<li><strong>\u58f0\u660e\u5f0f\u4e8b\u52a1<\/strong> \uff1a \u5728 XML \u914d\u7f6e\u6587\u4ef6\u4e2d\u914d\u7f6e\u6216\u8005\u76f4\u63a5\u57fa\u4e8e\u6ce8\u89e3\uff08\u63a8\u8350\u4f7f\u7528\uff09 : \u5b9e\u9645\u662f\u901a\u8fc7 AOP \u5b9e\u73b0\uff08\u57fa\u4e8e<code>@Transactional<\/code> \u7684\u5168\u6ce8\u89e3\u65b9\u5f0f\u4f7f\u7528\u6700\u591a\uff09<\/li>\n<\/ul>\n<h3>Spring \u4e8b\u52a1\u4e2d\u54ea\u51e0\u79cd\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a?<\/h3>\n<p><strong>\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a\u662f\u4e3a\u4e86\u89e3\u51b3\u4e1a\u52a1\u5c42\u65b9\u6cd5\u4e4b\u95f4\u4e92\u76f8\u8c03\u7528\u7684\u4e8b\u52a1\u95ee\u9898<\/strong>\u3002<\/p>\n<p>\u5f53\u4e8b\u52a1\u65b9\u6cd5\u88ab\u53e6\u4e00\u4e2a\u4e8b\u52a1\u65b9\u6cd5\u8c03\u7528\u65f6\uff0c\u5fc5\u987b\u6307\u5b9a\u4e8b\u52a1\u5e94\u8be5\u5982\u4f55\u4f20\u64ad\u3002\u4f8b\u5982\uff1a\u65b9\u6cd5\u53ef\u80fd\u7ee7\u7eed\u5728\u73b0\u6709\u4e8b\u52a1\u4e2d\u8fd0\u884c\uff0c\u4e5f\u53ef\u80fd\u5f00\u542f\u4e00\u4e2a\u65b0\u4e8b\u52a1\uff0c\u5e76\u5728\u81ea\u5df1\u7684\u4e8b\u52a1\u4e2d\u8fd0\u884c\u3002<\/p>\n<p>\u6b63\u786e\u7684\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a\u53ef\u80fd\u7684\u503c\u5982\u4e0b:<\/p>\n<p><strong>1.<code>TransactionDefinition.PROPAGATION_REQUIRED<\/code><\/strong><\/p>\n<p>\u4f7f\u7528\u7684\u6700\u591a\u7684\u4e00\u4e2a\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a\uff0c\u6211\u4eec\u5e73\u65f6\u7ecf\u5e38\u4f7f\u7528\u7684<code>@Transactional<\/code>\u6ce8\u89e3\u9ed8\u8ba4\u4f7f\u7528\u5c31\u662f\u8fd9\u4e2a\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a\u3002\u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u52a0\u5165\u8be5\u4e8b\u52a1\uff1b\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\uff0c\u5219\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\u3002<\/p>\n<p><strong><code>2.TransactionDefinition.PROPAGATION_REQUIRES_NEW<\/code><\/strong><\/p>\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\uff0c\u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u628a\u5f53\u524d\u4e8b\u52a1\u6302\u8d77\u3002\u4e5f\u5c31\u662f\u8bf4\u4e0d\u7ba1\u5916\u90e8\u65b9\u6cd5\u662f\u5426\u5f00\u542f\u4e8b\u52a1\uff0c<code>Propagation.REQUIRES_NEW<\/code>\u4fee\u9970\u7684\u5185\u90e8\u65b9\u6cd5\u4f1a\u65b0\u5f00\u542f\u81ea\u5df1\u7684\u4e8b\u52a1\uff0c\u4e14\u5f00\u542f\u7684\u4e8b\u52a1\u76f8\u4e92\u72ec\u7acb\uff0c\u4e92\u4e0d\u5e72\u6270\u3002<\/p>\n<p><strong>3.<code>TransactionDefinition.PROPAGATION_NESTED<\/code><\/strong><\/p>\n<p>\u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u521b\u5efa\u4e00\u4e2a\u4e8b\u52a1\u4f5c\u4e3a\u5f53\u524d\u4e8b\u52a1\u7684\u5d4c\u5957\u4e8b\u52a1\u6765\u8fd0\u884c\uff1b\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\uff0c\u5219\u8be5\u53d6\u503c\u7b49\u4ef7\u4e8e<code>TransactionDefinition.PROPAGATION_REQUIRED<\/code>\u3002<\/p>\n<p><strong>4.<code>TransactionDefinition.PROPAGATION_MANDATORY<\/code><\/strong><\/p>\n<p>\u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u52a0\u5165\u8be5\u4e8b\u52a1\uff1b\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002\uff08mandatory\uff1a\u5f3a\u5236\u6027\uff09<\/p>\n<p>\u8fd9\u4e2a\u4f7f\u7528\u7684\u5f88\u5c11\u3002<\/p>\n<p>\u82e5\u662f\u9519\u8bef\u7684\u914d\u7f6e\u4ee5\u4e0b 3 \u79cd\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a\uff0c\u4e8b\u52a1\u5c06\u4e0d\u4f1a\u53d1\u751f\u56de\u6eda\uff1a<\/p>\n<ul>\n<li><strong><code>TransactionDefinition.PROPAGATION_SUPPORTS<\/code><\/strong>: \u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u52a0\u5165\u8be5\u4e8b\u52a1\uff1b\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\uff0c\u5219\u4ee5\u975e\u4e8b\u52a1\u7684\u65b9\u5f0f\u7ee7\u7eed\u8fd0\u884c\u3002<\/li>\n<li><strong><code>TransactionDefinition.PROPAGATION_NOT_SUPPORTED<\/code><\/strong>: \u4ee5\u975e\u4e8b\u52a1\u65b9\u5f0f\u8fd0\u884c\uff0c\u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u628a\u5f53\u524d\u4e8b\u52a1\u6302\u8d77\u3002<\/li>\n<li><strong><code>TransactionDefinition.PROPAGATION_NEVER<\/code><\/strong>: \u4ee5\u975e\u4e8b\u52a1\u65b9\u5f0f\u8fd0\u884c\uff0c\u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u629b\u51fa\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<h3>Spring \u4e8b\u52a1\u4e2d\u7684\u9694\u79bb\u7ea7\u522b\u6709\u54ea\u51e0\u79cd?<\/h3>\n<p>\u548c\u4e8b\u52a1\u4f20\u64ad\u884c\u4e3a\u8fd9\u5757\u4e00\u6837\uff0c\u4e3a\u4e86\u65b9\u4fbf\u4f7f\u7528\uff0cSpring \u4e5f\u76f8\u5e94\u5730\u5b9a\u4e49\u4e86\u4e00\u4e2a\u679a\u4e3e\u7c7b\uff1a<code>Isolation<\/code><\/p>\n<pre><code class=\"java\">public enum Isolation {\n\n    DEFAULT(TransactionDefinition.ISOLATION_DEFAULT),\n\n    READ_UNCOMMITTED(TransactionDefinition.ISOLATION_READ_UNCOMMITTED),\n\n    READ_COMMITTED(TransactionDefinition.ISOLATION_READ_COMMITTED),\n\n    REPEATABLE_READ(TransactionDefinition.ISOLATION_REPEATABLE_READ),\n\n    SERIALIZABLE(TransactionDefinition.ISOLATION_SERIALIZABLE);\n\n    private final int value;\n\n    Isolation(int value) {\n        this.value = value;\n    }\n\n    public int value() {\n        return this.value;\n    }\n\n}<\/code><\/pre>\n<p>\u4e0b\u9762\u6211\u4f9d\u6b21\u5bf9\u6bcf\u4e00\u79cd\u4e8b\u52a1\u9694\u79bb\u7ea7\u522b\u8fdb\u884c\u4ecb\u7ecd\uff1a<\/p>\n<ul>\n<li><strong><code>TransactionDefinition.ISOLATION_DEFAULT<\/code><\/strong> :\u4f7f\u7528\u540e\u7aef\u6570\u636e\u5e93\u9ed8\u8ba4\u7684\u9694\u79bb\u7ea7\u522b\uff0cMySQL \u9ed8\u8ba4\u91c7\u7528\u7684 <code>REPEATABLE_READ<\/code> \u9694\u79bb\u7ea7\u522b Oracle \u9ed8\u8ba4\u91c7\u7528\u7684 <code>READ_COMMITTED<\/code> \u9694\u79bb\u7ea7\u522b.<\/li>\n<li><strong><code>TransactionDefinition.ISOLATION_READ_UNCOMMITTED<\/code><\/strong> :\u6700\u4f4e\u7684\u9694\u79bb\u7ea7\u522b\uff0c\u4f7f\u7528\u8fd9\u4e2a\u9694\u79bb\u7ea7\u522b\u5f88\u5c11\uff0c\u56e0\u4e3a\u5b83\u5141\u8bb8\u8bfb\u53d6\u5c1a\u672a\u63d0\u4ea4\u7684\u6570\u636e\u53d8\u66f4\uff0c<strong>\u53ef\u80fd\u4f1a\u5bfc\u81f4\u810f\u8bfb\u3001\u5e7b\u8bfb\u6216\u4e0d\u53ef\u91cd\u590d\u8bfb<\/strong><\/li>\n<li><strong><code>TransactionDefinition.ISOLATION_READ_COMMITTED<\/code><\/strong> : \u5141\u8bb8\u8bfb\u53d6\u5e76\u53d1\u4e8b\u52a1\u5df2\u7ecf\u63d0\u4ea4\u7684\u6570\u636e\uff0c<strong>\u53ef\u4ee5\u963b\u6b62\u810f\u8bfb\uff0c\u4f46\u662f\u5e7b\u8bfb\u6216\u4e0d\u53ef\u91cd\u590d\u8bfb\u4ecd\u6709\u53ef\u80fd\u53d1\u751f<\/strong><\/li>\n<li><strong><code>TransactionDefinition.ISOLATION_REPEATABLE_READ<\/code><\/strong> : \u5bf9\u540c\u4e00\u5b57\u6bb5\u7684\u591a\u6b21\u8bfb\u53d6\u7ed3\u679c\u90fd\u662f\u4e00\u81f4\u7684\uff0c\u9664\u975e\u6570\u636e\u662f\u88ab\u672c\u8eab\u4e8b\u52a1\u81ea\u5df1\u6240\u4fee\u6539\uff0c<strong>\u53ef\u4ee5\u963b\u6b62\u810f\u8bfb\u548c\u4e0d\u53ef\u91cd\u590d\u8bfb\uff0c\u4f46\u5e7b\u8bfb\u4ecd\u6709\u53ef\u80fd\u53d1\u751f\u3002<\/strong><\/li>\n<li><strong><code>TransactionDefinition.ISOLATION_SERIALIZABLE<\/code><\/strong> : \u6700\u9ad8\u7684\u9694\u79bb\u7ea7\u522b\uff0c\u5b8c\u5168\u670d\u4ece ACID \u7684\u9694\u79bb\u7ea7\u522b\u3002\u6240\u6709\u7684\u4e8b\u52a1\u4f9d\u6b21\u9010\u4e2a\u6267\u884c\uff0c\u8fd9\u6837\u4e8b\u52a1\u4e4b\u95f4\u5c31\u5b8c\u5168\u4e0d\u53ef\u80fd\u4ea7\u751f\u5e72\u6270\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c<strong>\u8be5\u7ea7\u522b\u53ef\u4ee5\u9632\u6b62\u810f\u8bfb\u3001\u4e0d\u53ef\u91cd\u590d\u8bfb\u4ee5\u53ca\u5e7b\u8bfb<\/strong>\u3002\u4f46\u662f\u8fd9\u5c06\u4e25\u91cd\u5f71\u54cd\u7a0b\u5e8f\u7684\u6027\u80fd\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\u4e5f\u4e0d\u4f1a\u7528\u5230\u8be5\u7ea7\u522b\u3002<\/li>\n<\/ul>\n<h3>@Transactional(rollbackFor = Exception.class)\u6ce8\u89e3\u4e86\u89e3\u5417\uff1f<\/h3>\n<p><code>Exception<\/code> \u5206\u4e3a\u8fd0\u884c\u65f6\u5f02\u5e38 <code>RuntimeException<\/code> \u548c\u975e\u8fd0\u884c\u65f6\u5f02\u5e38\u3002\u4e8b\u52a1\u7ba1\u7406\u5bf9\u4e8e\u4f01\u4e1a\u5e94\u7528\u6765\u8bf4\u662f\u81f3\u5173\u91cd\u8981\u7684\uff0c\u5373\u4f7f\u51fa\u73b0\u5f02\u5e38\u60c5\u51b5\uff0c\u5b83\u4e5f\u53ef\u4ee5\u4fdd\u8bc1\u6570\u636e\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<p>\u5f53 <code>@Transactional<\/code> \u6ce8\u89e3\u4f5c\u7528\u4e8e\u7c7b\u4e0a\u65f6\uff0c\u8be5\u7c7b\u7684\u6240\u6709 public \u65b9\u6cd5\u5c06\u90fd\u5177\u6709\u8be5\u7c7b\u578b\u7684\u4e8b\u52a1\u5c5e\u6027\uff0c\u540c\u65f6\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5728\u65b9\u6cd5\u7ea7\u522b\u4f7f\u7528\u8be5\u6807\u6ce8\u6765\u8986\u76d6\u7c7b\u7ea7\u522b\u7684\u5b9a\u4e49\u3002\u5982\u679c\u7c7b\u6216\u8005\u65b9\u6cd5\u52a0\u4e86\u8fd9\u4e2a\u6ce8\u89e3\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7c7b\u91cc\u9762\u7684\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38\uff0c\u5c31\u4f1a\u56de\u6eda\uff0c\u6570\u636e\u5e93\u91cc\u9762\u7684\u6570\u636e\u4e5f\u4f1a\u56de\u6eda\u3002<\/p>\n<p>\u5728 <code>@Transactional<\/code> \u6ce8\u89e3\u4e2d\u5982\u679c\u4e0d\u914d\u7f6e<code>rollbackFor<\/code>\u5c5e\u6027,\u90a3\u4e48\u4e8b\u52a1\u53ea\u4f1a\u5728\u9047\u5230<code>RuntimeException<\/code>\u7684\u65f6\u5019\u624d\u4f1a\u56de\u6eda\uff0c\u52a0\u4e0a <code>rollbackFor=Exception.class<\/code>,\u53ef\u4ee5\u8ba9\u4e8b\u52a1\u5728\u9047\u5230\u975e\u8fd0\u884c\u65f6\u5f02\u5e38\u65f6\u4e5f\u56de\u6eda\u3002<\/p>\n<h2>Spring Data JPA<\/h2>\n<p>JPA \u91cd\u8981\u7684\u662f\u5b9e\u6218\uff0c\u8fd9\u91cc\u4ec5\u5bf9\u5c0f\u90e8\u5206\u77e5\u8bc6\u70b9\u8fdb\u884c\u603b\u7ed3\u3002<\/p>\n<h3>\u5982\u4f55\u4f7f\u7528 JPA \u5728\u6570\u636e\u5e93\u4e2d\u975e\u6301\u4e45\u5316\u4e00\u4e2a\u5b57\u6bb5\uff1f<\/h3>\n<p>\u5047\u5982\u6211\u4eec\u6709\u4e0b\u9762\u4e00\u4e2a\u7c7b\uff1a<\/p>\n<pre><code class=\"java\">@Entity(name=&quot;USER&quot;)\npublic class User {\n\n    @Id\n    @GeneratedValue(strategy = GenerationType.AUTO)\n    @Column(name = &quot;ID&quot;)\n    private Long id;\n\n    @Column(name=&quot;USER_NAME&quot;)\n    private String userName;\n\n    @Column(name=&quot;PASSWORD&quot;)\n    private String password;\n\n    private String secrect;\n\n}<\/code><\/pre>\n<p>\u5982\u679c\u6211\u4eec\u60f3\u8ba9<code>secrect<\/code> \u8fd9\u4e2a\u5b57\u6bb5\u4e0d\u88ab\u6301\u4e45\u5316\uff0c\u4e5f\u5c31\u662f\u4e0d\u88ab\u6570\u636e\u5e93\u5b58\u50a8\u600e\u4e48\u529e\uff1f\u6211\u4eec\u53ef\u4ee5\u91c7\u7528\u4e0b\u9762\u51e0\u79cd\u65b9\u6cd5\uff1a<\/p>\n<pre><code class=\"java\">static String transient1; \/\/ not persistent because of static\nfinal String transient2 = &quot;Satish&quot;; \/\/ not persistent because of final\ntransient String transient3; \/\/ not persistent because of transient\n@Transient\nString transient4; \/\/ not persistent because of @Transient<\/code><\/pre>\n<p>\u4e00\u822c\u4f7f\u7528\u540e\u9762\u4e24\u79cd\u65b9\u5f0f\u6bd4\u8f83\u591a\uff0c\u6211\u4e2a\u4eba\u4f7f\u7528\u6ce8\u89e3\u7684\u65b9\u5f0f\u6bd4\u8f83\u591a\u3002<\/p>\n<h3>JPA \u7684\u5ba1\u8ba1\u529f\u80fd\u662f\u505a\u4ec0\u4e48\u7684\uff1f\u6709\u4ec0\u4e48\u7528\uff1f<\/h3>\n<p>\u5ba1\u8ba1\u529f\u80fd\u4e3b\u8981\u662f\u5e2e\u52a9\u6211\u4eec\u8bb0\u5f55\u6570\u636e\u5e93\u64cd\u4f5c\u7684\u5177\u4f53\u884c\u4e3a\u6bd4\u5982\u67d0\u6761\u8bb0\u5f55\u662f\u8c01\u521b\u5efa\u7684\u3001\u4ec0\u4e48\u65f6\u95f4\u521b\u5efa\u7684\u3001\u6700\u540e\u4fee\u6539\u4eba\u662f\u8c01\u3001\u6700\u540e\u4fee\u6539\u65f6\u95f4\u662f\u4ec0\u4e48\u65f6\u5019\u3002<\/p>\n<pre><code class=\"java\">@Data\n@AllArgsConstructor\n@NoArgsConstructor\n@MappedSuperclass\n@EntityListeners(value = AuditingEntityListener.class)\npublic abstract class AbstractAuditBase {\n\n    @CreatedDate\n    @Column(updatable = false)\n    @JsonIgnore\n    private Instant createdAt;\n\n    @LastModifiedDate\n    @JsonIgnore\n    private Instant updatedAt;\n\n    @CreatedBy\n    @Column(updatable = false)\n    @JsonIgnore\n    private String createdBy;\n\n    @LastModifiedBy\n    @JsonIgnore\n    private String updatedBy;\n}<\/code><\/pre>\n<ul>\n<li><code>@CreatedDate<\/code>: \u8868\u793a\u8be5\u5b57\u6bb5\u4e3a\u521b\u5efa\u65f6\u95f4\u5b57\u6bb5\uff0c\u5728\u8fd9\u4e2a\u5b9e\u4f53\u88ab insert \u7684\u65f6\u5019\uff0c\u4f1a\u8bbe\u7f6e\u503c<\/li>\n<li>\n<p><code>@CreatedBy<\/code> :\u8868\u793a\u8be5\u5b57\u6bb5\u4e3a\u521b\u5efa\u4eba\uff0c\u5728\u8fd9\u4e2a\u5b9e\u4f53\u88ab insert \u7684\u65f6\u5019\uff0c\u4f1a\u8bbe\u7f6e\u503c<\/p>\n<p><code>@LastModifiedDate<\/code>\u3001<code>@LastModifiedBy<\/code>\u540c\u7406\u3002<\/p>\n<\/li>\n<\/ul>\n<h3>\u5b9e\u4f53\u4e4b\u95f4\u7684\u5173\u8054\u5173\u7cfb\u6ce8\u89e3\u6709\u54ea\u4e9b\uff1f<\/h3>\n<ul>\n<li><code>@OneToOne<\/code> : \u4e00\u5bf9\u4e00\u3002<\/li>\n<li><code>@ManyToMany<\/code> \uff1a\u591a\u5bf9\u591a\u3002<\/li>\n<li><code>@OneToMany<\/code> : \u4e00\u5bf9\u591a\u3002<\/li>\n<li><code>@ManyToOne<\/code> \uff1a\u591a\u5bf9\u4e00\u3002<\/li>\n<\/ul>\n<p>\u5229\u7528 <code>@ManyToOne<\/code> \u548c <code>@OneToMany<\/code> \u4e5f\u53ef\u4ee5\u8868\u8fbe\u591a\u5bf9\u591a\u7684\u5173\u8054\u5173\u7cfb\u3002<\/p>\n<h2>Spring Security<\/h2>\n<p>Spring Security \u91cd\u8981\u7684\u662f\u5b9e\u6218\uff0c\u8fd9\u91cc\u4ec5\u5bf9\u5c0f\u90e8\u5206\u77e5\u8bc6\u70b9\u8fdb\u884c\u603b\u7ed3\u3002<\/p>\n<h3>\u6709\u54ea\u4e9b\u63a7\u5236\u8bf7\u6c42\u8bbf\u95ee\u6743\u9650\u7684\u65b9\u6cd5\uff1f<\/h3>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254935\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<ul>\n<li><code>permitAll()<\/code> \uff1a\u65e0\u6761\u4ef6\u5141\u8bb8\u4efb\u4f55\u5f62\u5f0f\u8bbf\u95ee\uff0c\u4e0d\u7ba1\u4f60\u767b\u5f55\u8fd8\u662f\u6ca1\u6709\u767b\u5f55\u3002<\/li>\n<li><code>anonymous()<\/code> \uff1a\u5141\u8bb8\u533f\u540d\u8bbf\u95ee\uff0c\u4e5f\u5c31\u662f\u6ca1\u6709\u767b\u5f55\u624d\u53ef\u4ee5\u8bbf\u95ee\u3002<\/li>\n<li><code>denyAll()<\/code> \uff1a\u65e0\u6761\u4ef6\u51b3\u7edd\u4efb\u4f55\u5f62\u5f0f\u7684\u8bbf\u95ee\u3002<\/li>\n<li><code>authenticated()<\/code>\uff1a\u53ea\u5141\u8bb8\u5df2\u8ba4\u8bc1\u7684\u7528\u6237\u8bbf\u95ee\u3002<\/li>\n<li><code>fullyAuthenticated()<\/code> \uff1a\u53ea\u5141\u8bb8\u5df2\u7ecf\u767b\u5f55\u6216\u8005\u901a\u8fc7 remember-me \u767b\u5f55\u7684\u7528\u6237\u8bbf\u95ee\u3002<\/li>\n<li><code>hasRole(String)<\/code> : \u53ea\u5141\u8bb8\u6307\u5b9a\u7684\u89d2\u8272\u8bbf\u95ee\u3002<\/li>\n<li><code>hasAnyRole(String)<\/code> : \u6307\u5b9a\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u89d2\u8272\uff0c\u6ee1\u8db3\u5176\u4e00\u7684\u7528\u6237\u5373\u53ef\u8bbf\u95ee\u3002<\/li>\n<li><code>hasAuthority(String)<\/code> \uff1a\u53ea\u5141\u8bb8\u5177\u6709\u6307\u5b9a\u6743\u9650\u7684\u7528\u6237\u8bbf\u95ee<\/li>\n<li><code>hasAnyAuthority(String)<\/code> \uff1a\u6307\u5b9a\u4e00\u4e2a\u6216\u8005\u591a\u4e2a\u6743\u9650\uff0c\u6ee1\u8db3\u5176\u4e00\u7684\u7528\u6237\u5373\u53ef\u8bbf\u95ee\u3002<\/li>\n<li><code>hasIpAddress(String)<\/code> : \u53ea\u5141\u8bb8\u6307\u5b9a ip \u7684\u7528\u6237\u8bbf\u95ee\u3002<\/li>\n<\/ul>\n<h3>hasRole \u548c hasAuthority \u6709\u533a\u522b\u5417\uff1f<\/h3>\n<p>\u53ef\u4ee5\u770b\u770b\u677e\u54e5\u7684\u8fd9\u7bc7\u6587\u7ae0\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=JLMOyeNOFvOQ39FsuS%2Bq5g%3D%3D.UeIINucL29H%2B6wT8SopWb44i2Q0UzxvBGUsthpBqxU4XV8S%2BmB1bzbYdXbiCoSt5O5JD6hvT4%2FIWxLzF4rwe2g%3D%3D\" rel=\"nofollow\">Spring Security \u4e2d\u7684 hasRole \u548c hasAuthority \u6709\u533a\u522b\u5417\uff1f<\/a>\uff0c\u4ecb\u7ecd\u7684\u6bd4\u8f83\u8be6\u7ec6\u3002<\/p>\n<h3>\u5982\u4f55\u5bf9\u5bc6\u7801\u8fdb\u884c\u52a0\u5bc6\uff1f<\/h3>\n<p>\u5982\u679c\u6211\u4eec\u9700\u8981\u4fdd\u5b58\u5bc6\u7801\u8fd9\u7c7b\u654f\u611f\u6570\u636e\u5230\u6570\u636e\u5e93\u7684\u8bdd\uff0c\u9700\u8981\u5148\u52a0\u5bc6\u518d\u4fdd\u5b58\u3002<\/p>\n<p>Spring Security \u63d0\u4f9b\u4e86\u591a\u79cd\u52a0\u5bc6\u7b97\u6cd5\u7684\u5b9e\u73b0\uff0c\u5f00\u7bb1\u5373\u7528\uff0c\u975e\u5e38\u65b9\u4fbf\u3002\u8fd9\u4e9b\u52a0\u5bc6\u7b97\u6cd5\u5b9e\u73b0\u7c7b\u7684\u7236\u7c7b\u662f <code>PasswordEncoder<\/code> \uff0c\u5982\u679c\u4f60\u60f3\u8981\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a\u52a0\u5bc6\u7b97\u6cd5\u7684\u8bdd\uff0c\u4e5f\u9700\u8981\u7ee7\u627f <code>PasswordEncoder<\/code>\u3002<\/p>\n<p><code>PasswordEncoder<\/code> \u63a5\u53e3\u4e00\u5171\u4e5f\u5c31 3 \u4e2a\u5fc5\u987b\u5b9e\u73b0\u7684\u65b9\u6cd5\u3002<\/p>\n<pre><code class=\"java\">public interface PasswordEncoder {\n    \/\/ \u52a0\u5bc6\u4e5f\u5c31\u662f\u5bf9\u539f\u59cb\u5bc6\u7801\u8fdb\u884c\u7f16\u7801\n    String encode(CharSequence var1);\n    \/\/ \u6bd4\u5bf9\u539f\u59cb\u5bc6\u7801\u548c\u6570\u636e\u5e93\u4e2d\u4fdd\u5b58\u7684\u5bc6\u7801\n    boolean matches(CharSequence var1, String var2);\n    \/\/ \u5224\u65ad\u52a0\u5bc6\u5bc6\u7801\u662f\u5426\u9700\u8981\u518d\u6b21\u8fdb\u884c\u52a0\u5bc6\uff0c\u9ed8\u8ba4\u8fd4\u56de false\n    default boolean upgradeEncoding(String encodedPassword) {\n        return false;\n    }\n}<\/code><\/pre>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042254936\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5b98\u65b9\u63a8\u8350\u4f7f\u7528\u57fa\u4e8e bcrypt \u5f3a\u54c8\u5e0c\u51fd\u6570\u7684\u52a0\u5bc6\u7b97\u6cd5\u5b9e\u73b0\u7c7b\u3002<\/p>\n<h3>\u5982\u4f55\u4f18\u96c5\u66f4\u6362\u7cfb\u7edf\u4f7f\u7528\u7684\u52a0\u5bc6\u7b97\u6cd5\uff1f<\/h3>\n<p>\u5982\u679c\u6211\u4eec\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u7a81\u7136\u53d1\u73b0\u73b0\u6709\u7684\u52a0\u5bc6\u7b97\u6cd5\u65e0\u6cd5\u6ee1\u8db3\u6211\u4eec\u7684\u9700\u6c42\uff0c\u9700\u8981\u66f4\u6362\u6210\u53e6\u5916\u4e00\u4e2a\u52a0\u5bc6\u7b97\u6cd5\uff0c\u8fd9\u4e2a\u65f6\u5019\u5e94\u8be5\u600e\u4e48\u529e\u5462\uff1f<\/p>\n<p>\u63a8\u8350\u7684\u505a\u6cd5\u662f\u901a\u8fc7 <code>DelegatingPasswordEncoder<\/code> \u517c\u5bb9\u591a\u79cd\u4e0d\u540c\u7684\u5bc6\u7801\u52a0\u5bc6\u65b9\u6848\uff0c\u4ee5\u9002\u5e94\u4e0d\u540c\u7684\u4e1a\u52a1\u9700\u6c42\u3002<\/p>\n<p>\u4ece\u540d\u5b57\u4e5f\u80fd\u770b\u51fa\u6765\uff0c<code>DelegatingPasswordEncoder<\/code> \u5176\u5b9e\u5c31\u662f\u4e00\u4e2a\u4ee3\u7406\u7c7b\uff0c\u5e76\u975e\u662f\u4e00\u79cd\u5168\u65b0\u7684\u52a0\u5bc6\u7b97\u6cd5\uff0c\u5b83\u505a\u7684\u4e8b\u60c5\u5c31\u662f\u4ee3\u7406\u4e0a\u9762\u63d0\u5230\u7684\u52a0\u5bc6\u7b97\u6cd5\u5b9e\u73b0\u7c7b\u3002\u5728 Spring Security 5.0 \u4e4b\u540e\uff0c\u9ed8\u8ba4\u5c31\u662f\u57fa\u4e8e <code>DelegatingPasswordEncoder<\/code> \u8fdb\u884c\u5bc6\u7801\u52a0\u5bc6\u7684\u3002<\/p>\n<h2>\u53c2\u8003<\/h2>\n<ul>\n<li>\u300aSpring \u6280\u672f\u5185\u5e55\u300b<\/li>\n<li>\u300a\u4ece\u96f6\u5f00\u59cb\u6df1\u5165\u5b66\u4e60 Spring\u300b\uff1a<a href=\"https:\/\/link.segmentfault.com\/?enc=%2BvTW4%2FoSlw09x3w8QfPvdQ%3D%3D.9cKuNuAbuUZnv1mYmtTykMSjcI6RWk993C0h3X0SdoDAaIAKLQZAUvfhuSl4%2BZgy\" rel=\"nofollow\">https:\/\/juejin.cn\/book\/685791&#8230;<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=hyoxD0jBoFKxbtBtmIZKGA%3D%3D.Ya3j43HEXXrpCsarvem9Ani%2BTEeBXRZ%2FC04vJFBd3Pxwg9xXKQqPxwcWV%2BaXi9RX\" rel=\"nofollow\">http:\/\/www.cnblogs.com\/wmyskx&#8230;<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=d6dnDhQTITkAXg%2FauEaVQg%3D%3D.ZmAArY0a5LKOFItRYVwwQwhdjPI3Vd2B28jE4N%2FAdsW%2BILtUv93M1JnuVIMESj36p5G9%2B2eIU62OGP2OzVZlPb5kXNfgH006JslW0QZZ%2Bhk%3D\" rel=\"nofollow\">https:\/\/www.journaldev.com\/26&#8230;<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=oekc%2Bqo7Jj5xVWh0Okpz%2FA%3D%3D.yfrnknE1ktFmJ1Vc0JC1jFQLRTIde1ouNxvYlBEzLJlsx8ubzgC3s6IWferX3mxa%2FIjnptFHCX4LeBwNQBdbBuurHvIKBOodnAGL6tNOTWY%3D\" rel=\"nofollow\">https:\/\/www.edureka.co\/blog\/i&#8230;<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=JX51vJJXkF9bCkqC%2FZp8WQ%3D%3D.7DxIAXmL2nVoapQ2lbvpEXJMfSUltXDNDf9Hbd5FaMMcntJBOH4vCwtNgg625Oei\" rel=\"nofollow\">https:\/\/www.cnblogs.com\/clwyd&#8230;<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=oMIkh4r5xJMMazT2BxKqxQ%3D%3D.fKPBlP2XVKnN8qDdFAFYCNItC%2FGdyDsW%2BdqJ3uBlbddyQ4EslUyh%2Brpc5Jk%2BgMf9rgs6Z06hWXFae9PMCooYSjATqExWm7xjEeZwDlV2FmDJmHE1bp00%2B5E%2Byz0J8wLW\" rel=\"nofollow\">https:\/\/howtodoinjava.com\/int&#8230;<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=%2B6nyr7emyzoogQ9Gqq1u5Q%3D%3D.Xc0ojF3frW6V7Kds2G7lDCLgY7A9WEFn8UXhLLVvBwUpvMHiWY11CUUHAwDeNwf0TR0Rf272v9V1l1zR%2BfY3VjVd940KpZkFR1VVMlAYW3o%3D\" rel=\"nofollow\">http:\/\/www.tomaszezula.com\/20&#8230;<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=s6FKxEtsqos8xoED7CV6UA%3D%3D.g14GaB%2FUbwQukr5GXuwitL6Kh%2FlALfkaq22TvJb0yHxifwFww%2BSKkAX1aVdYXjt8VTA83hfiyj5WQNCH2yTkgg5W9usTpPzLvUGi151v7EeJntArR4iAWSwOoSAPTkD0\" rel=\"nofollow\">https:\/\/stackoverflow.com\/que&#8230;<\/a><\/li>\n<\/ul>\n<h2>\u540e\u8bb0<\/h2>\n<p>\u4e13\u6ce8 Java \u539f\u521b\u5e72\u8d27\u5206\u4eab\uff0c\u5927\u4e09\u5f00\u6e90 <a href=\"https:\/\/link.segmentfault.com\/?enc=XX3%2FOAlj0Mjx66fhi3TXDQ%3D%3D.6JSywSlJt4JEIHssYoo6xTv5Xuu%2BNlLkIvzLLGXy8XI%3D\" rel=\"nofollow\">JavaGuide<\/a>\u00a0\uff08\u300cJava\u5b66\u4e60+\u9762\u8bd5\u6307\u5357\u300d\u4e00\u4efd\u6db5\u76d6\u5927\u90e8\u5206 Java \u7a0b\u5e8f\u5458\u6240\u9700\u8981\u638c\u63e1\u7684\u6838\u5fc3\u77e5\u8bc6\u3002\u51c6\u5907 Java \u9762\u8bd5\uff0c\u9996\u9009 JavaGuide\uff01\uff09\uff0c\u76ee\u524d\u5df2\u7ecf 120k+ Star\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000042015044\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u5982\u679c\u672c\u6587\u5bf9\u4f60\u6709\u5e2e\u52a9\u7684\u8bdd\uff0c\u6b22\u8fce\u70b9\u8d5e\u5206\u4eab\uff0c\u8fd9\u5bf9\u6211\u7ee7\u7eed\u5206\u4eab&#038;\u521b\u4f5c\u4f18\u8d28\u6587\u7ae0\u975e\u5e38\u91cd\u8981\u3002\u611f\u8c22 \ud83d\ude4f\ud83c\udffb<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042254917\">https:\/\/segmentfault.com\/a\/1190000042254917<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u53d1\u4e8e JavaGuide \u5728\u7ebf\u7f51\u7ad9\uff1aSpring \u5e38\u89c1\u9762\u8bd5\u9898\u603b\u7ed3 \u6700\u8fd1\u5728\u5bf9 JavaGuide \u7684\u5185\u5bb9\u8fdb\u884c\u91cd [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006081","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006081"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006081\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}