├── .gitignore ├── LICENSE ├── README.md ├── pom.xml ├── spring-aop-01-@Before ├── pom.xml └── src │ ├── main │ └── java │ │ └── c │ │ └── jbd │ │ └── saop │ │ └── gettingstarted │ │ ├── aspect │ │ └── BeforeAdvice.java │ │ ├── config │ │ └── ApplicationConfig.java │ │ ├── dao │ │ ├── ActorRepository.java │ │ └── MovieRepository.java │ │ └── service │ │ └── MovieService.java │ └── test │ └── java │ └── c │ └── jbd │ └── saop │ └── gettingstarted │ └── TestBeforeAdvice.java ├── spring-aop-02-combine-joinpoints ├── pom.xml └── src │ ├── main │ └── java │ │ └── c │ │ └── jbd │ │ └── saop │ │ └── cjp │ │ ├── ApplicationConfig.java │ │ ├── aspect │ │ └── AnalyticsAdvice.java │ │ └── dao │ │ └── UserRepository.java │ └── test │ └── java │ └── c │ └── jbd │ └── saop │ └── cjp │ └── TestAnalytics.java ├── spring-aop-03-@AfterReturning ├── pom.xml └── src │ ├── main │ └── java │ │ └── c │ │ └── jbd │ │ └── saop │ │ └── ar │ │ ├── ApplicationConfig.java │ │ ├── aspect │ │ ├── DaoExpressions.java │ │ └── UppercaseAspect.java │ │ ├── dao │ │ └── UserRepository.java │ │ └── pojo │ │ └── User.java │ └── test │ └── java │ └── c │ └── jbd │ └── saop │ └── ar │ └── TestAfterReturning.java ├── spring-aop-04-@AfterThrowing ├── pom.xml └── src │ ├── main │ └── java │ │ └── c │ │ └── jbd │ │ └── saop │ │ └── at │ │ ├── ApplicationConfig.java │ │ ├── aspect │ │ └── ExceptionAspect.java │ │ └── dao │ │ └── UserRepository.java │ └── test │ └── java │ └── c │ └── jbd │ └── saop │ └── at │ └── TestAfterThrowing.java ├── spring-aop-05-@After ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── jbd │ │ └── saop │ │ └── after │ │ ├── ApplicationConfig.java │ │ ├── aspect │ │ └── AllAfterAspects.java │ │ └── dao │ │ └── UserRepository.java │ └── test │ └── java │ └── com │ └── jbd │ └── saop │ └── after │ └── TestAfterAdvice.java ├── spring-aop-06-@Around ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── jbd │ │ └── saop │ │ └── around │ │ ├── ApplicationConfig.java │ │ ├── ExecutionTime.java │ │ ├── advice │ │ └── ExecutionTimeLogger.java │ │ └── dao │ │ └── UserRepository.java │ └── test │ └── java │ └── com │ └── jbd │ └── saop │ └── around │ └── TestAroundAdvice.java ├── spring-aop-07-ordering-advice ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── jbd │ │ └── saop │ │ └── order │ │ ├── ApplicationConfig.java │ │ ├── advice │ │ ├── CommonPointcut.java │ │ ├── SecondAdvice.java │ │ ├── ThirdAdvice.java │ │ └── ZeroAdvice.java │ │ ├── dao │ │ ├── EmailRepository.java │ │ └── UserRepository.java │ │ └── service │ │ └── UserService.java │ └── test │ └── java │ └── com │ └── jbd │ └── saop │ └── order │ └── TestAdviceOrdering.java ├── spring-aop-08-advice-parameters └── pom.xml ├── spring-aop-09-custom-annotation └── pom.xml ├── spring-aop-10-inter-type-declarations └── pom.xml ├── spring-core-Import-ImportResource ├── pom.xml └── src │ └── main │ ├── java │ └── com │ │ └── jbd │ │ └── sc │ │ ├── importannotation │ │ └── ApplicationConfig.java │ │ └── items │ │ ├── offline │ │ ├── OfflineOrderProcessor.java │ │ └── OfflineStoreConfig.java │ │ └── online │ │ └── OnlineOrderProcessor.java │ └── resources │ └── online-store-config.xml ├── spring-core-beanlifecycle ├── pom.xml └── src │ └── main │ ├── java │ └── bean │ │ ├── dfault │ │ └── callbacks │ │ │ ├── LifecycleBean.java │ │ │ ├── MyServiceA.java │ │ │ ├── MyServiceB.java │ │ │ └── TestDefaultCallbacks.java │ │ ├── jsr │ │ └── callbacks │ │ │ ├── JsrExampleBean.java │ │ │ ├── JsrExampleConfig.java │ │ │ └── TestJsrExample.java │ │ ├── lifecycle │ │ └── callbacks │ │ │ ├── BLCConfigClass.java │ │ │ ├── ExampleBean1.java │ │ │ ├── ExampleBean2.java │ │ │ └── TestInitDestroy.java │ │ └── start │ │ └── stop │ │ └── callbacks │ │ ├── LifecycleExampleBean.java │ │ └── TestLifecycleBean.java │ └── resources │ └── bean.default.lifecycle.xml ├── spring-core-beanpostprocessor ├── pom.xml └── src │ └── main │ └── java │ └── core │ └── bean │ └── postprocessor │ ├── ExampleBean.java │ ├── ExampleConfig.java │ ├── TestExampleBean.java │ └── TraceBeanPostProcessor.java ├── spring-core-environment-abstraction ├── pom.xml └── src │ ├── main │ ├── java │ │ └── com │ │ │ └── jsbd │ │ │ ├── profiles │ │ │ ├── DataSource.java │ │ │ ├── GraphQlDataSource.java │ │ │ ├── ProfilesApp.java │ │ │ ├── ProfilesConfig.java │ │ │ └── RestDataSource.java │ │ │ └── propertysource │ │ │ ├── AppConfig.java │ │ │ ├── AppLogger.java │ │ │ ├── ConnectionManager.java │ │ │ └── TestClass.java │ └── resources │ │ └── app.properties │ └── test │ └── java │ └── com │ └── jsbd │ └── profiles │ ├── TestDefaultProfile.java │ ├── TestGraphQlProfile.java │ └── TestRestProfile.java ├── spring-core-events ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── jsbd │ │ └── events │ │ ├── AnnEventListener.java │ │ ├── AppConfig.java │ │ ├── EventListener.java │ │ ├── EventPublisher.java │ │ ├── UserEvent.java │ │ └── generic │ │ ├── GenericEvent.java │ │ ├── GenericEventListener.java │ │ └── Person.java │ └── test │ └── java │ └── com │ └── jsbd │ └── events │ └── synchronous │ ├── EventPublisherTest.java │ └── GenericEventTest.java ├── spring-core-ioc ├── pom.xml └── src │ ├── main │ ├── java │ │ └── basic │ │ │ └── ioc │ │ │ ├── autowire │ │ │ ├── AutowireBeanConfig.java │ │ │ ├── Item.java │ │ │ ├── Store.java │ │ │ └── TestAutowireExample.java │ │ │ ├── bean │ │ │ ├── collections │ │ │ │ ├── BeanReferenceCollections.java │ │ │ │ ├── CollectionsConfig.java │ │ │ │ ├── ConstructorInjection.java │ │ │ │ ├── FieldInjection.java │ │ │ │ ├── JbdBean.java │ │ │ │ ├── NameBean.java │ │ │ │ ├── SetterInjection.java │ │ │ │ ├── SortedNamesCollection.java │ │ │ │ └── package-info.java │ │ │ └── scope │ │ │ │ ├── AutowireBeanConfig.java │ │ │ │ ├── Item.java │ │ │ │ ├── Store.java │ │ │ │ └── TestAutowireExample.java │ │ │ ├── beanfactory │ │ │ ├── AutowireBeanConfig.java │ │ │ ├── Item.java │ │ │ ├── Store.java │ │ │ └── TestAutowireExample.java │ │ │ ├── config │ │ │ └── inheritance │ │ │ │ ├── Author.java │ │ │ │ └── BookAuthor.java │ │ │ ├── constructor │ │ │ ├── setter │ │ │ ├── annotation │ │ │ │ ├── BasicIocSetterConfig.java │ │ │ │ ├── Item.java │ │ │ │ ├── Store.java │ │ │ │ └── TestAnnotationSetterInjection.java │ │ │ └── xml │ │ │ │ ├── Item.java │ │ │ │ ├── Store.java │ │ │ │ └── TestXmlSetterInjection.java │ │ │ ├── wiring │ │ │ ├── ConfigWiring.java │ │ │ ├── FileReader.java │ │ │ ├── PdfFileReader.java │ │ │ └── WordFileReader.java │ │ │ └── wiringfinetune │ │ │ ├── ConfigWiring.java │ │ │ ├── FileReader.java │ │ │ ├── Insurance.java │ │ │ ├── PdfFileReader.java │ │ │ └── WordFileReader.java │ └── resources │ │ ├── basic.ioc.bean-config.inheritance.xml │ │ ├── basic.ioc.constructor.bean-config.xml │ │ └── basic.ioc.setter.bean-config.xml │ └── test │ └── java │ └── basic │ └── ioc │ ├── bean │ └── collections │ │ └── TestCollectionsIoC.java │ ├── config │ └── inheritance │ │ └── TestConfigInheritance.java │ ├── wiring │ ├── AutowiredTest.java │ ├── InjectTest.java │ └── ResourceTest.java │ └── wiringfinetune │ └── FineTuneAutowiring.java ├── spring-core-null-safety ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── jbd │ │ └── spring │ │ ├── AppConfig.java │ │ └── fieldlevel │ │ └── Person.java │ └── test │ └── java │ └── com │ └── jbd │ └── spring │ └── TestNullSafety.java ├── spring-core-order-annotation ├── pom.xml └── src │ ├── main │ └── java │ │ └── com │ │ └── jsbd │ │ └── order │ │ ├── ApplicationConfig.java │ │ ├── model │ │ └── NotificationChannel.java │ │ └── service │ │ ├── EmailNotification.java │ │ ├── NotificationHandler.java │ │ ├── SlackNotification.java │ │ ├── SmsNotification.java │ │ └── TwitterNotification.java │ └── test │ └── java │ └── com │ └── jsbd │ └── order │ └── TestOrderOnCollectionInjection.java ├── spring-core-work-with-resources ├── pom.xml └── src │ └── main │ ├── java │ └── c │ │ └── jbd │ │ └── spring │ │ └── resources │ │ ├── DefaultResourceLoaderExample.java │ │ ├── ReaderUtil.java │ │ ├── ResourcesExample.java │ │ └── loader │ │ ├── JbdResourceLoader.java │ │ └── ResourceLoaderDemo.java │ └── resources │ ├── ex-config.xml │ └── sample.md ├── spring-mvc-00-getting-started ├── pom.xml └── src │ └── main │ ├── java │ └── c │ │ └── jbd │ │ └── saop │ │ └── cjp │ │ └── sm │ │ └── gettingstarted │ │ ├── config │ │ ├── ApplicationConfig.java │ │ └── SpringMvcInitializer.java │ │ ├── controller │ │ └── HelloController.java │ │ └── model │ │ └── Message.java │ └── webapp │ └── WEB-INF │ └── views │ └── hello-view.jsp └── spring-tutorial-boms └── pom.xml /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # Maven related 23 | target/ 24 | pom.xml.tag 25 | pom.xml.releaseBackup 26 | pom.xml.versionsBackup 27 | pom.xml.next 28 | release.properties 29 | dependency-reduced-pom.xml 30 | buildNumber.properties 31 | .mvn/timing.properties 32 | .mvn/wrapper/maven-wrapper.jar 33 | 34 | # For MacOS related 35 | # General 36 | .DS_Store 37 | .AppleDouble 38 | .LSOverride 39 | .idea 40 | 41 | # Icon must end with two \r 42 | Icon 43 | 44 | # Thumbnails 45 | ._* 46 | 47 | # Files that might appear in the root of a volume 48 | .DocumentRevisions-V100 49 | .fseventsd 50 | .Spotlight-V100 51 | .TemporaryItems 52 | .Trashes 53 | .VolumeIcon.icns 54 | .com.apple.timemachine.donotpresent 55 | 56 | # Directories potentially created on remote AFP share 57 | .AppleDB 58 | .AppleDesktop 59 | Network Trash Folder 60 | Temporary Items 61 | .apdisk 62 | 63 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 64 | hs_err_pid* 65 | 66 | #Auto-import 67 | *.iml -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Spring Framework 5.x Tutorial - [Jstobigdata.com](https://jstobigdata.com/) 2 | This tutorial is purely on Spring and its ecosystem without using Spring Boot. 3 | 4 | ```javascript 5 | if(you.liked) { 6 | GitHub.star(); 7 | shareWithFriends(this); 8 | } 9 | ``` 10 | 11 | ## Spring Core Tutorial 12 | The following Spring Core Topics are covered in Jstobigdata.com. 13 | 14 | 1. [A high-level introduction to Spring Framework](https://jstobigdata.com/spring/a-high-level-introduction-to-spring-framework/) 15 | 2. [Inversion of Control and Dependency Injection in Spring](https://jstobigdata.com/spring/inversion-of-control-and-dependency-injection-in-spring/) 16 | 3. [Spring Bean Scopes – @Scope annotation](https://jstobigdata.com/spring/spring-bean-scopes-scope-annotation/) 17 | 4. [Use of @Order annotation in Spring framework](https://jstobigdata.com/spring/use-of-order-annotation-in-spring/) 18 | 5. [Mapping and Injecting Collections in Spring](https://jstobigdata.com/spring/mapping-and-injecting-collections-in-spring/) 19 | 6. [Spring bean Lifecycle Callbacks](https://jstobigdata.com/spring/spring-bean-lifecycle-callbacks/) 20 | 7. [Spring bean definition Inheritance](https://jstobigdata.com/spring/spring-bean-definition-inheritance/) 21 | 8. [Spring BeanPostProcessor to customize beans](https://jstobigdata.com/spring/spring-beanpostprocessor-to-customize-beans/) 22 | 9. [Dependency Injection: @Autowired, @Resource and @Inject](https://jstobigdata.com/spring/dependency-injection-autowired-resource-and-inject/) 23 | 10. [Fine-tune auto wiring using @Primary and @Qualifier](https://jstobigdata.com/spring/fine-tune-auto-wiring-using-primary-and-qualifier/) 24 | 11. [IoC Container, Bean Factory vs Application Context in Spring](https://jstobigdata.com/spring/ioc-container-application-context-vs-bean-factory-in-spring/) 25 | 12. [Managed beans using @Component, @Repository, @Service](https://jstobigdata.com/spring/managed-components-in-spring-component-repository-service/) 26 | 13. [Classpath Scanning using @ComponentScan and Filters](https://jstobigdata.com/spring/classpath-scanning-using-componentscan-and-filters/) 27 | 14. [Spring @PropertySource to read property files](https://jstobigdata.com/spring/spring-propertysource-to-read-property-files/) 28 | 15. [Profiles in Spring to register beans conditionally](https://jstobigdata.com/spring/profiles-in-spring-to-register-beans-conditionally/) 29 | 16. [Custom Events and Generic Events in Spring](https://jstobigdata.com/spring/custom-events-and-generic-events-in-spring/) 30 | 17. [Handle Resources in Spring - Resource, ResourceLoader and ResourceLoaderAware interfaces](https://jstobigdata.com/spring/handle-resources-in-spring/) 31 | 18. [Spring @Import and @ImportResource annotations](https://jstobigdata.com/spring/spring-import-and-importresource-annotations/) 32 | 33 | ## Spring AOP Tutorial 34 | This is a complete Spring AOP tutorial without using Spring Boot. This is designed to give you a solid foundation of Spring-AOP fundamentals. 35 | 1. [Spring AOP introduction](https://jstobigdata.com/spring/complete-spring-aop-tutorial/) 36 | 2. [The @Before advice in Spring AOP](https://jstobigdata.com/spring/the-before-advice-in-spring-aop/) 37 | 3. [Pointcut expressions in Spring AOP](https://jstobigdata.com/spring/pointcut-expressions-in-spring-aop/) 38 | 4. [Combine and Reuse pointcut expressions in Spring AOP](https://jstobigdata.com/spring/combine-and-reuse-pointcut-expressions-in-spring-aop/) 39 | 5. [After returning advice in Spring AOP – @AfterReturning](https://jstobigdata.com/spring/after-returning-advice-in-spring-aop-afterreturning/) 40 | 6. [After Throwing advice in Spring AOP – @AfterThrowing](https://jstobigdata.com/spring/after-throwing-advice-in-spring-aop-afterthrowing/) 41 | 7. [After (finally) advice in Spring AOP – @After](https://jstobigdata.com/spring/after-advice-in-spring-aop-after/) 42 | 8. [Around advice in Spring AOP - @Around](https://jstobigdata.com/spring/around-advice-in-spring-aop-around/) 43 | 9. [Ordering advices in Spring AOP - @Order](https://jstobigdata.com/spring/advice-ordering-using-order-in-spring-aop/) 44 | 45 | ----- 46 | ## Spring WebFlux Tutorial - own repo 47 | [Check the Spring WebFlux repo](https://github.com/jstobigdata/Spring-WebFlux-Tutorial) 48 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 4.0.0 7 | 8 | com.jstobigdata 9 | spring-framework-tutorial-parent 10 | pom 11 | 1.0-SNAPSHOT 12 | 13 | 14 | spring-tutorial-boms 15 | spring-core-ioc 16 | spring-core-beanlifecycle 17 | spring-core-beanpostprocessor 18 | spring-core-environment-abstraction 19 | spring-core-events 20 | spring-mvc-00-getting-started 21 | spring-aop-01-@Before 22 | spring-aop-02-combine-joinpoints 23 | spring-aop-03-@AfterReturning 24 | spring-aop-04-@AfterThrowing 25 | spring-aop-05-@After 26 | spring-aop-06-@Around 27 | spring-aop-07-ordering-advice 28 | spring-aop-08-advice-parameters 29 | spring-aop-09-custom-annotation 30 | spring-aop-10-inter-type-declarations 31 | spring-core-null-safety 32 | spring-core-work-with-resources 33 | spring-core-Import-ImportResource 34 | 35 | 36 | 37 | 38 | 11 39 | 11 40 | 11 41 | UTF-8 42 | 43 | 44 | -------------------------------------------------------------------------------- /spring-aop-01-@Before/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | spring-aop-01-Before 12 | Spring AOP - @Before advice example 13 | 14 | 15 | 16 | org.springframework 17 | spring-core 18 | 19 | 20 | 21 | org.springframework 22 | spring-context 23 | 24 | 25 | 26 | 27 | org.springframework 28 | spring-aop 29 | 30 | 31 | 32 | org.aspectj 33 | aspectjweaver 34 | 35 | 36 | 37 | 38 | org.junit.jupiter 39 | junit-jupiter-api 40 | test 41 | 42 | 43 | 44 | org.junit.jupiter 45 | junit-jupiter-engine 46 | test 47 | 48 | 49 | 50 | org.springframework 51 | spring-test 52 | test 53 | 54 | 55 | 56 | 57 | 58 | 59 | com.jstobigdata 60 | spring-tutorial-boms 61 | pom 62 | import 63 | 1.0-SNAPSHOT 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | org.apache.maven.plugins 73 | maven-surefire-plugin 74 | 3.0.0-M3 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /spring-aop-01-@Before/src/main/java/c/jbd/saop/gettingstarted/aspect/BeforeAdvice.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.gettingstarted.aspect; 2 | 3 | import org.aspectj.lang.JoinPoint; 4 | import org.aspectj.lang.annotation.Aspect; 5 | import org.aspectj.lang.annotation.Before; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | @Configuration 9 | @Aspect //Important to say this is a Aspect config class 10 | public class BeforeAdvice { 11 | 12 | //execution(RETURN_TYPE PACKAGE.CLASS.METHOD(..PARAMETER_LIST)) 13 | //execution(* PACKAGE.*.*(..)) 14 | 15 | /** 16 | * Before Interceptor all Repository add() method. 17 | * 18 | * @param joinPoint 19 | */ 20 | @Before("execution(* c.jbd.saop.gettingstarted.dao.*.add(..))") 21 | public void allRepoAddMethods(JoinPoint joinPoint) { 22 | System.out.println("Intercepted method: " + joinPoint); 23 | System.out.println("Arguments: " + joinPoint.getArgs()[0]); 24 | System.out.println(joinPoint.getTarget()); 25 | } 26 | 27 | @Before("execution(* c.jbd.saop.gettingstarted.dao.*.*(..))") 28 | public void allClassAnyMethod(JoinPoint joinPoint) { 29 | System.out.println("Intercepted method: " + joinPoint); 30 | System.out.println("Arguments: " + joinPoint.getArgs()[0]); 31 | System.out.println(joinPoint.getTarget()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /spring-aop-01-@Before/src/main/java/c/jbd/saop/gettingstarted/config/ApplicationConfig.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.gettingstarted.config; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.context.annotation.EnableAspectJAutoProxy; 6 | 7 | /** 8 | * Getting started AppConfig. 9 | */ 10 | @Configuration 11 | //Step-2 - Add the annotation 12 | @EnableAspectJAutoProxy 13 | @ComponentScan("c.jbd.saop.gettingstarted") 14 | public class ApplicationConfig { 15 | } 16 | -------------------------------------------------------------------------------- /spring-aop-01-@Before/src/main/java/c/jbd/saop/gettingstarted/dao/ActorRepository.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.gettingstarted.dao; 2 | 3 | import org.springframework.stereotype.Repository; 4 | 5 | @Repository 6 | public class ActorRepository { 7 | //Add an actor 8 | public ActorRepository add(String actorName) { 9 | if (actorName == null) { 10 | throw new RuntimeException("actorName is null", new NullPointerException()); 11 | } 12 | System.out.println("New Actor added: " + actorName); 13 | return this; 14 | } 15 | 16 | //Delete an actor 17 | public boolean delete(String actorName) { 18 | if (actorName == null) { 19 | throw new RuntimeException("actorName is null", new NullPointerException()); 20 | } 21 | System.out.println("Actor deleted: " + actorName); 22 | return true; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /spring-aop-01-@Before/src/main/java/c/jbd/saop/gettingstarted/dao/MovieRepository.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.gettingstarted.dao; 2 | 3 | import org.springframework.stereotype.Repository; 4 | 5 | @Repository 6 | public class MovieRepository { 7 | //Add a movie method 8 | public MovieRepository add(String movieName) { 9 | if (movieName == null) { 10 | throw new RuntimeException("movieName is null", new NullPointerException()); 11 | } 12 | System.out.println("New movie added: " + movieName); 13 | return this; 14 | } 15 | 16 | //Delete a movie 17 | public boolean delete(String movieName) { 18 | if (movieName == null) { 19 | throw new RuntimeException("movieName is null", new NullPointerException()); 20 | } 21 | System.out.println("Movie deleted: " + movieName); 22 | return true; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /spring-aop-01-@Before/src/main/java/c/jbd/saop/gettingstarted/service/MovieService.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.gettingstarted.service; 2 | 3 | import c.jbd.saop.gettingstarted.dao.ActorRepository; 4 | import c.jbd.saop.gettingstarted.dao.MovieRepository; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | @Service 9 | public class MovieService { 10 | @Autowired 11 | private ActorRepository actorRepository; 12 | 13 | @Autowired 14 | private MovieRepository movieRepository; 15 | 16 | public void addMovie(String movieName, String... actors) { 17 | movieRepository.add(movieName); 18 | for (String actor : actors) { 19 | actorRepository.add(movieName); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /spring-aop-01-@Before/src/test/java/c/jbd/saop/gettingstarted/TestBeforeAdvice.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.gettingstarted; 2 | 3 | import c.jbd.saop.gettingstarted.config.ApplicationConfig; 4 | import c.jbd.saop.gettingstarted.dao.ActorRepository; 5 | import c.jbd.saop.gettingstarted.dao.MovieRepository; 6 | import org.junit.jupiter.api.Assertions; 7 | import org.junit.jupiter.api.Test; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 10 | 11 | @SpringJUnitConfig(ApplicationConfig.class) 12 | public class TestBeforeAdvice { 13 | 14 | @Autowired 15 | private ActorRepository actorRepository; 16 | 17 | @Autowired 18 | private MovieRepository movieRepository; 19 | 20 | @Test 21 | public void notNull() { 22 | Assertions.assertNotNull(actorRepository); 23 | Assertions.assertNotNull(movieRepository); 24 | } 25 | 26 | @Test 27 | public void testAddAspects() { 28 | actorRepository.add("Hrithik Roshan"); 29 | movieRepository.add("Sholey"); 30 | } 31 | 32 | @Test 33 | public void testDeleteAspects() { 34 | actorRepository.delete("John Doe"); 35 | movieRepository.delete("Abc"); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /spring-aop-02-combine-joinpoints/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | spring-aop-02-combine-joinpoints 12 | This tutorial is about combining Pointcuts and reusing them in Spring-AOP 13 | 14 | 15 | 16 | org.springframework 17 | spring-core 18 | 19 | 20 | 21 | org.springframework 22 | spring-context 23 | 24 | 25 | 26 | 27 | org.springframework 28 | spring-aop 29 | 30 | 31 | 32 | org.aspectj 33 | aspectjweaver 34 | 35 | 36 | 37 | 38 | org.junit.jupiter 39 | junit-jupiter-api 40 | test 41 | 42 | 43 | 44 | org.junit.jupiter 45 | junit-jupiter-engine 46 | test 47 | 48 | 49 | 50 | org.springframework 51 | spring-test 52 | test 53 | 54 | 55 | 56 | 57 | 58 | 59 | com.jstobigdata 60 | spring-tutorial-boms 61 | pom 62 | import 63 | 1.0-SNAPSHOT 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | org.apache.maven.plugins 72 | maven-surefire-plugin 73 | 3.0.0-M3 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /spring-aop-02-combine-joinpoints/src/main/java/c/jbd/saop/cjp/ApplicationConfig.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.cjp; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.context.annotation.EnableAspectJAutoProxy; 6 | 7 | @Configuration 8 | @EnableAspectJAutoProxy 9 | @ComponentScan("c.jbd.saop.cjp") 10 | public class ApplicationConfig { 11 | } 12 | -------------------------------------------------------------------------------- /spring-aop-02-combine-joinpoints/src/main/java/c/jbd/saop/cjp/aspect/AnalyticsAdvice.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.cjp.aspect; 2 | 3 | import org.aspectj.lang.JoinPoint; 4 | import org.aspectj.lang.annotation.Aspect; 5 | import org.aspectj.lang.annotation.Before; 6 | import org.aspectj.lang.annotation.Pointcut; 7 | import org.springframework.context.annotation.Configuration; 8 | 9 | @Configuration 10 | @Aspect 11 | public class AnalyticsAdvice { 12 | @Pointcut("within(c.jbd.saop.cjp.dao.*)") 13 | private void allDao() { 14 | } 15 | 16 | @Pointcut("execution(* update(..))") 17 | private void updateMethod() { 18 | } 19 | 20 | //Special analytics for Update methods 21 | @Before("allDao() && updateMethod()") 22 | public void specialAnalytics(JoinPoint joinPoint) { 23 | System.out.println("Call special analytics for: " 24 | + joinPoint.getSignature()); 25 | } 26 | 27 | //general analytics for other methods 28 | @Before("allDao() && !updateMethod()") 29 | public void generalAnalytics(JoinPoint joinPoint) { 30 | System.out.println("Call general analytics for:" 31 | + joinPoint.getSignature()); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /spring-aop-02-combine-joinpoints/src/main/java/c/jbd/saop/cjp/dao/UserRepository.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.cjp.dao; 2 | 3 | import org.springframework.stereotype.Repository; 4 | 5 | //A very stupid demo repository 6 | @Repository 7 | public class UserRepository { 8 | //Add a user 9 | public UserRepository add(String username){ 10 | if(username == null) { 11 | throw new RuntimeException("username is null", new NullPointerException()); 12 | } 13 | System.out.println("New user added: " + username); 14 | return this; 15 | } 16 | 17 | //Update an user 18 | public UserRepository update(String username, String email) { 19 | System.out.println("Update email: " + email); 20 | return this; 21 | } 22 | 23 | //Delete an user 24 | public boolean delete(String username){ 25 | if (username == null) { 26 | throw new RuntimeException("username is null", new NullPointerException()); 27 | } 28 | System.out.println("User deleted: " + username); 29 | return true; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-aop-02-combine-joinpoints/src/test/java/c/jbd/saop/cjp/TestAnalytics.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.cjp; 2 | 3 | import c.jbd.saop.cjp.dao.UserRepository; 4 | import org.junit.jupiter.api.Assertions; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 8 | 9 | @SpringJUnitConfig(ApplicationConfig.class) 10 | public class TestAnalytics { 11 | 12 | @Autowired 13 | UserRepository userRepository; 14 | 15 | @Test 16 | public void notNull(){ 17 | Assertions.assertNotNull(userRepository); 18 | } 19 | 20 | @Test 21 | public void testAddUser(){ 22 | userRepository.add("Alexa"); 23 | } 24 | 25 | @Test 26 | public void testUpdateUser(){ 27 | userRepository.update("Alexa", "alexa@aws.com"); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /spring-aop-03-@AfterReturning/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | spring-aop-03-AfterReturning 12 | Spring AOP - @AfterReturning aspect example 13 | 14 | 15 | 16 | org.springframework 17 | spring-core 18 | 19 | 20 | 21 | org.springframework 22 | spring-context 23 | 24 | 25 | 26 | 27 | org.springframework 28 | spring-aop 29 | 30 | 31 | 32 | org.aspectj 33 | aspectjweaver 34 | 35 | 36 | 37 | 38 | org.junit.jupiter 39 | junit-jupiter-api 40 | test 41 | 42 | 43 | 44 | org.junit.jupiter 45 | junit-jupiter-engine 46 | test 47 | 48 | 49 | 50 | org.springframework 51 | spring-test 52 | test 53 | 54 | 55 | 56 | 57 | 58 | 59 | com.jstobigdata 60 | spring-tutorial-boms 61 | pom 62 | import 63 | 1.0-SNAPSHOT 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | org.apache.maven.plugins 73 | maven-surefire-plugin 74 | 3.0.0-M3 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /spring-aop-03-@AfterReturning/src/main/java/c/jbd/saop/ar/ApplicationConfig.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.ar; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.context.annotation.EnableAspectJAutoProxy; 6 | 7 | @Configuration 8 | @EnableAspectJAutoProxy 9 | @ComponentScan("c.jbd.saop.ar") 10 | public class ApplicationConfig { 11 | } 12 | -------------------------------------------------------------------------------- /spring-aop-03-@AfterReturning/src/main/java/c/jbd/saop/ar/aspect/DaoExpressions.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.ar.aspect; 2 | 3 | import org.aspectj.lang.annotation.Aspect; 4 | import org.aspectj.lang.annotation.Pointcut; 5 | 6 | @Aspect 7 | public class DaoExpressions { 8 | 9 | //Match with only UserRepository 10 | @Pointcut("within(c.jbd.saop.ar.dao.UserRepository)") 11 | public void userDao() {} 12 | 13 | //Match with any find() method 14 | @Pointcut("execution(* find(..))") 15 | public void findMethod() {} 16 | 17 | @Pointcut("userDao() && findMethod()") 18 | public void userDaoFind(){} 19 | } 20 | -------------------------------------------------------------------------------- /spring-aop-03-@AfterReturning/src/main/java/c/jbd/saop/ar/aspect/UppercaseAspect.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.ar.aspect; 2 | 3 | import c.jbd.saop.ar.pojo.User; 4 | import org.aspectj.lang.JoinPoint; 5 | import org.aspectj.lang.annotation.AfterReturning; 6 | import org.aspectj.lang.annotation.Aspect; 7 | import org.springframework.context.annotation.Configuration; 8 | 9 | @Configuration 10 | @Aspect 11 | public class UppercaseAspect { 12 | 13 | /** 14 | * Uppercase advice for username. 15 | * @param joinPoint 16 | * @param result 17 | */ 18 | @AfterReturning( 19 | pointcut = "c.jbd.saop.ar.aspect.DaoExpressions.findMethod()", 20 | returning = "result") 21 | public void uppercaseUsername(JoinPoint joinPoint, User result){ 22 | System.out.println("After method - " + 23 | joinPoint.getSignature().toShortString()); 24 | System.out.println("original result:" + result); 25 | if (result.getUsername() != null){ 26 | result.setUsername(result.getUsername().toUpperCase()); 27 | } 28 | System.out.println("final result: " + result); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /spring-aop-03-@AfterReturning/src/main/java/c/jbd/saop/ar/dao/UserRepository.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.ar.dao; 2 | 3 | import c.jbd.saop.ar.pojo.User; 4 | import org.springframework.stereotype.Repository; 5 | 6 | //A very stupid demo repository 7 | @Repository 8 | public class UserRepository { 9 | 10 | //find an User 11 | public User find(String username) { 12 | if (username == null) { 13 | throw new RuntimeException("username is null", new NullPointerException()); 14 | } 15 | return new User(username, "hello@world.com"); 16 | } 17 | 18 | //Update an user 19 | public UserRepository update(String username, String email) { 20 | System.out.println("Update email: " + email); 21 | return this; 22 | } 23 | 24 | //Delete an user 25 | public boolean delete(String username) { 26 | if (username == null) { 27 | throw new RuntimeException("username is null", new NullPointerException()); 28 | } 29 | System.out.println("User deleted: " + username); 30 | return true; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /spring-aop-03-@AfterReturning/src/main/java/c/jbd/saop/ar/pojo/User.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.ar.pojo; 2 | 3 | import java.util.StringJoiner; 4 | 5 | //The User pojo 6 | public class User { 7 | private String username; 8 | private String email; 9 | 10 | public User(String username, String email){ 11 | this.email = email; 12 | this.username = username; 13 | } 14 | 15 | public String getUsername() { 16 | return username; 17 | } 18 | 19 | public User setUsername(String username) { 20 | this.username = username; 21 | return this; 22 | } 23 | 24 | public String getEmail() { 25 | return email; 26 | } 27 | 28 | public User setEmail(String email) { 29 | this.email = email; 30 | return this; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return new StringJoiner(", ", User.class.getSimpleName() + "[", "]") 36 | .add("username='" + username + "'") 37 | .add("email='" + email + "'") 38 | .toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-aop-03-@AfterReturning/src/test/java/c/jbd/saop/ar/TestAfterReturning.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.ar; 2 | 3 | import c.jbd.saop.ar.dao.UserRepository; 4 | import c.jbd.saop.ar.pojo.User; 5 | import org.junit.jupiter.api.Assertions; 6 | import org.junit.jupiter.api.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 9 | 10 | @SpringJUnitConfig(ApplicationConfig.class) 11 | public class TestAfterReturning { 12 | 13 | @Autowired 14 | private UserRepository userRepository; 15 | 16 | @Test 17 | public void checkNull(){ 18 | Assertions.assertNotNull(userRepository); 19 | } 20 | 21 | @Test 22 | public void testFindUser(){ 23 | String username = "JsTobigdata"; 24 | User user = userRepository.find(username); 25 | Assertions.assertEquals(user.getUsername(), username.toUpperCase()); 26 | Assertions.assertNotEquals(user.getUsername(), username); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-aop-04-@AfterThrowing/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | spring-aop-04-AfterThrowing 12 | Spring AOP - @AfterThrowing advice example 13 | 14 | 15 | 16 | org.springframework 17 | spring-core 18 | 19 | 20 | 21 | org.springframework 22 | spring-context 23 | 24 | 25 | 26 | 27 | org.springframework 28 | spring-aop 29 | 30 | 31 | 32 | org.aspectj 33 | aspectjweaver 34 | 35 | 36 | 37 | 38 | org.junit.jupiter 39 | junit-jupiter-api 40 | test 41 | 42 | 43 | 44 | org.junit.jupiter 45 | junit-jupiter-engine 46 | test 47 | 48 | 49 | 50 | org.springframework 51 | spring-test 52 | test 53 | 54 | 55 | 56 | 57 | 58 | 59 | com.jstobigdata 60 | spring-tutorial-boms 61 | pom 62 | import 63 | 1.0-SNAPSHOT 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | org.apache.maven.plugins 73 | maven-surefire-plugin 74 | 3.0.0-M3 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /spring-aop-04-@AfterThrowing/src/main/java/c/jbd/saop/at/ApplicationConfig.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.at; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.context.annotation.EnableAspectJAutoProxy; 6 | 7 | @Configuration 8 | @EnableAspectJAutoProxy 9 | @ComponentScan("c.jbd.saop.at") 10 | public class ApplicationConfig { 11 | } 12 | -------------------------------------------------------------------------------- /spring-aop-04-@AfterThrowing/src/main/java/c/jbd/saop/at/aspect/ExceptionAspect.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.at.aspect; 2 | 3 | import org.aspectj.lang.JoinPoint; 4 | import org.aspectj.lang.annotation.AfterThrowing; 5 | import org.aspectj.lang.annotation.Aspect; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | @Configuration 9 | @Aspect 10 | public class ExceptionAspect { 11 | 12 | /** 13 | * Match with all dao methods. 14 | * 15 | * @param joinPoint 16 | * @param exception 17 | */ 18 | @AfterThrowing( 19 | pointcut = "execution(* c.jbd.saop.at.dao.*.*(..))", 20 | throwing = "exception") 21 | public void logDaoExceptions(JoinPoint joinPoint, RuntimeException exception) { 22 | System.out.println("Exception at: " + joinPoint.getSignature().toShortString()); 23 | System.out.println("Exception details: " + exception); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /spring-aop-04-@AfterThrowing/src/main/java/c/jbd/saop/at/dao/UserRepository.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.at.dao; 2 | 3 | import org.springframework.stereotype.Repository; 4 | 5 | //A very stupid demo repository 6 | @Repository 7 | public class UserRepository { 8 | //Add a user 9 | public UserRepository add(String username){ 10 | if(username == null) { 11 | throw new RuntimeException("username is null", new NullPointerException()); 12 | } 13 | System.out.println("New user added: " + username); 14 | return this; 15 | } 16 | 17 | //Update an user 18 | public UserRepository update(String username, String email) { 19 | System.out.println("Update email: " + email); 20 | return this; 21 | } 22 | 23 | //Delete an user 24 | public boolean delete(String username){ 25 | if (username == null) { 26 | throw new RuntimeException("username is null", new NullPointerException()); 27 | } 28 | System.out.println("User deleted: " + username); 29 | return true; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-aop-04-@AfterThrowing/src/test/java/c/jbd/saop/at/TestAfterThrowing.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.at; 2 | 3 | import c.jbd.saop.at.dao.UserRepository; 4 | import org.junit.jupiter.api.Assertions; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 8 | 9 | @SpringJUnitConfig(ApplicationConfig.class) 10 | public class TestAfterThrowing { 11 | 12 | @Autowired 13 | private UserRepository userRepository; 14 | 15 | @Test 16 | public void notNull() { 17 | Assertions.assertNotNull(userRepository); 18 | } 19 | 20 | @Test 21 | public void testThrows() { 22 | //Assert throws RuntimeException 23 | Assertions.assertThrows(RuntimeException.class, () -> { 24 | userRepository.add(null); 25 | userRepository.delete(null); 26 | }); 27 | 28 | //Assert does not throw exception 29 | Assertions.assertDoesNotThrow(() -> { 30 | userRepository.delete("alexa"); 31 | }); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /spring-aop-05-@After/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | spring-aop-05-After 12 | Spring AOP - @After advice example 13 | 14 | 15 | 16 | org.springframework 17 | spring-core 18 | 19 | 20 | 21 | org.springframework 22 | spring-context 23 | 24 | 25 | 26 | 27 | org.springframework 28 | spring-aop 29 | 30 | 31 | 32 | org.aspectj 33 | aspectjweaver 34 | 35 | 36 | 37 | 38 | org.junit.jupiter 39 | junit-jupiter-api 40 | test 41 | 42 | 43 | 44 | org.junit.jupiter 45 | junit-jupiter-engine 46 | test 47 | 48 | 49 | 50 | org.springframework 51 | spring-test 52 | test 53 | 54 | 55 | 56 | 57 | 58 | 59 | com.jstobigdata 60 | spring-tutorial-boms 61 | pom 62 | import 63 | 1.0-SNAPSHOT 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | org.apache.maven.plugins 73 | maven-surefire-plugin 74 | 3.0.0-M3 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /spring-aop-05-@After/src/main/java/com/jbd/saop/after/ApplicationConfig.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.after; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.context.annotation.EnableAspectJAutoProxy; 6 | 7 | @Configuration 8 | @EnableAspectJAutoProxy 9 | @ComponentScan("com.jbd.saop.after") 10 | public class ApplicationConfig { 11 | } 12 | -------------------------------------------------------------------------------- /spring-aop-05-@After/src/main/java/com/jbd/saop/after/aspect/AllAfterAspects.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.after.aspect; 2 | 3 | import org.aspectj.lang.JoinPoint; 4 | import org.aspectj.lang.annotation.After; 5 | import org.aspectj.lang.annotation.AfterThrowing; 6 | import org.aspectj.lang.annotation.Aspect; 7 | import org.aspectj.lang.annotation.Pointcut; 8 | import org.springframework.stereotype.Service; 9 | 10 | @Service 11 | @Aspect 12 | public class AllAfterAspects { 13 | 14 | @Pointcut("execution(* com.jbd.saop.after.dao.*.*(..))") 15 | private void allDaoMethods(){} 16 | 17 | @AfterThrowing( 18 | pointcut = "allDaoMethods()", 19 | throwing = "exception") 20 | public void logDaoExceptions(JoinPoint joinPoint, RuntimeException exception) { 21 | System.out.println("After Exception executed: " 22 | + joinPoint.getSignature().toShortString()); 23 | System.out.println("Exception details: " + exception); 24 | } 25 | 26 | @After("allDaoMethods()") 27 | public void logResults(JoinPoint joinPoint){ 28 | System.out.println("\nAfter advice executed: " 29 | + joinPoint.getSignature().toShortString()); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-aop-05-@After/src/main/java/com/jbd/saop/after/dao/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.after.dao; 2 | 3 | import org.springframework.stereotype.Repository; 4 | 5 | //A very stupid demo repository 6 | @Repository 7 | public class UserRepository { 8 | //Add a user 9 | public UserRepository add(String username){ 10 | if(username == null) { 11 | throw new RuntimeException("username is null", new NullPointerException()); 12 | } 13 | System.out.println("New user added: " + username); 14 | return this; 15 | } 16 | 17 | //Update an user 18 | public UserRepository update(String username, String email) { 19 | System.out.println("Update email: " + email); 20 | return this; 21 | } 22 | 23 | //Delete an user 24 | public boolean delete(String username){ 25 | if (username == null) { 26 | throw new RuntimeException("username is null", new NullPointerException()); 27 | } 28 | System.out.println("User deleted: " + username); 29 | return true; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-aop-05-@After/src/test/java/com/jbd/saop/after/TestAfterAdvice.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.after; 2 | 3 | import com.jbd.saop.after.dao.UserRepository; 4 | import org.junit.jupiter.api.Assertions; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 8 | 9 | @SpringJUnitConfig(ApplicationConfig.class) 10 | public class TestAfterAdvice { 11 | 12 | @Autowired 13 | private UserRepository userRepository; 14 | 15 | @Test 16 | public void notNull() { 17 | Assertions.assertNotNull(userRepository); 18 | } 19 | 20 | @Test 21 | public void testAfterThrows() { 22 | //Assert exception 23 | Assertions.assertThrows(RuntimeException.class, () -> { 24 | userRepository.add(null); 25 | }); 26 | } 27 | 28 | @Test 29 | public void testAfter() { 30 | //Assert not exception 31 | Assertions.assertDoesNotThrow(() -> { 32 | userRepository.delete("alexa"); 33 | }); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /spring-aop-06-@Around/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | spring-aop-06-Around 12 | Spring AOP - @Around aspect example 13 | 14 | 15 | 16 | org.springframework 17 | spring-core 18 | 19 | 20 | 21 | org.springframework 22 | spring-context 23 | 24 | 25 | 26 | 27 | org.springframework 28 | spring-aop 29 | 30 | 31 | 32 | org.aspectj 33 | aspectjweaver 34 | 35 | 36 | 37 | 38 | org.junit.jupiter 39 | junit-jupiter-api 40 | test 41 | 42 | 43 | 44 | org.junit.jupiter 45 | junit-jupiter-engine 46 | test 47 | 48 | 49 | 50 | org.springframework 51 | spring-test 52 | test 53 | 54 | 55 | 56 | 57 | 58 | 59 | com.jstobigdata 60 | spring-tutorial-boms 61 | pom 62 | import 63 | 1.0-SNAPSHOT 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | org.apache.maven.plugins 73 | maven-surefire-plugin 74 | 3.0.0-M3 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /spring-aop-06-@Around/src/main/java/com/jbd/saop/around/ApplicationConfig.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.around; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.context.annotation.EnableAspectJAutoProxy; 6 | 7 | @Configuration 8 | @ComponentScan("com.jbd.saop.around") 9 | @EnableAspectJAutoProxy 10 | public class ApplicationConfig { 11 | } 12 | -------------------------------------------------------------------------------- /spring-aop-06-@Around/src/main/java/com/jbd/saop/around/ExecutionTime.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.around; 2 | 3 | import java.lang.annotation.*; 4 | 5 | /** 6 | * Tracks the execution time. 7 | */ 8 | //Allowed to use only on methods 9 | @Target({ElementType.METHOD}) 10 | @Retention(RetentionPolicy.RUNTIME) 11 | @Documented 12 | public @interface ExecutionTime { 13 | } 14 | -------------------------------------------------------------------------------- /spring-aop-06-@Around/src/main/java/com/jbd/saop/around/advice/ExecutionTimeLogger.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.around.advice; 2 | 3 | import org.aspectj.lang.ProceedingJoinPoint; 4 | import org.aspectj.lang.annotation.Around; 5 | import org.aspectj.lang.annotation.Aspect; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | import java.time.LocalTime; 9 | 10 | @Configuration 11 | @Aspect 12 | public class ExecutionTimeLogger { 13 | 14 | @Around("@annotation(com.jbd.saop.around.ExecutionTime)") 15 | public Object logExecTime(ProceedingJoinPoint pJoinPoint){ 16 | System.out.println("Before method: " 17 | + pJoinPoint.getSignature().toShortString()); 18 | long beforeTime = System.currentTimeMillis(); 19 | Object result = null; 20 | try { 21 | result = pJoinPoint.proceed();//Important 22 | //If method throws Exception or any error occurs 23 | } catch (Throwable throwable) { 24 | throwable.printStackTrace(); 25 | } 26 | long afterTime = System.currentTimeMillis(); 27 | System.out.println("Time taken to execute: " 28 | + (afterTime - beforeTime) + "ms"); 29 | return result; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /spring-aop-06-@Around/src/main/java/com/jbd/saop/around/dao/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.around.dao; 2 | 3 | import com.jbd.saop.around.ExecutionTime; 4 | import org.springframework.stereotype.Repository; 5 | 6 | //A very stupid demo repository 7 | @Repository 8 | public class UserRepository { 9 | //Add a user 10 | @ExecutionTime 11 | public UserRepository add(String username) throws InterruptedException { 12 | Thread.sleep(100); 13 | if(username == null) { 14 | throw new RuntimeException("username is null", new NullPointerException()); 15 | } 16 | System.out.println("New user added: " + username); 17 | return this; 18 | } 19 | 20 | //Update an user 21 | public UserRepository update(String username, String email) { 22 | System.out.println("Update email: " + email); 23 | return this; 24 | } 25 | 26 | //Delete an user 27 | @ExecutionTime 28 | public boolean delete(String username){ 29 | if (username == null) { 30 | throw new RuntimeException("username is null", new NullPointerException()); 31 | } 32 | System.out.println("User deleted: " + username); 33 | return true; 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /spring-aop-06-@Around/src/test/java/com/jbd/saop/around/TestAroundAdvice.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.around; 2 | 3 | import com.jbd.saop.around.dao.UserRepository; 4 | import org.junit.jupiter.api.Assertions; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 8 | 9 | @SpringJUnitConfig(ApplicationConfig.class) 10 | public class TestAroundAdvice { 11 | @Autowired 12 | private UserRepository userRepository; 13 | 14 | @Test 15 | public void testNull(){ 16 | Assertions.assertNotNull(userRepository); 17 | } 18 | 19 | @Test 20 | public void testAddUser() throws InterruptedException { 21 | userRepository.add("sample_username"); 22 | } 23 | 24 | @Test 25 | public void testAddUserFailure() throws InterruptedException { 26 | userRepository.add(null); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /spring-aop-07-ordering-advice/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | spring-aop-07-ordering-advice 12 | Ordering advices using @Order annotation 13 | 14 | 15 | 16 | org.springframework 17 | spring-core 18 | 19 | 20 | 21 | org.springframework 22 | spring-context 23 | 24 | 25 | 26 | 27 | org.springframework 28 | spring-aop 29 | 30 | 31 | 32 | org.aspectj 33 | aspectjweaver 34 | 35 | 36 | 37 | 38 | org.junit.jupiter 39 | junit-jupiter-api 40 | test 41 | 42 | 43 | 44 | org.junit.jupiter 45 | junit-jupiter-engine 46 | test 47 | 48 | 49 | 50 | org.springframework 51 | spring-test 52 | test 53 | 54 | 55 | 56 | 57 | 58 | 59 | com.jstobigdata 60 | spring-tutorial-boms 61 | pom 62 | import 63 | 1.0-SNAPSHOT 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | org.apache.maven.plugins 73 | maven-surefire-plugin 74 | 3.0.0-M3 75 | 76 | 77 | 78 | -------------------------------------------------------------------------------- /spring-aop-07-ordering-advice/src/main/java/com/jbd/saop/order/ApplicationConfig.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.order; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.context.annotation.EnableAspectJAutoProxy; 6 | 7 | @Configuration 8 | @EnableAspectJAutoProxy 9 | @ComponentScan("com.jbd.saop.order") 10 | public class ApplicationConfig { 11 | } 12 | -------------------------------------------------------------------------------- /spring-aop-07-ordering-advice/src/main/java/com/jbd/saop/order/advice/CommonPointcut.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.order.advice; 2 | 3 | import org.aspectj.lang.annotation.Aspect; 4 | import org.aspectj.lang.annotation.Pointcut; 5 | 6 | @Aspect 7 | public class CommonPointcut { 8 | 9 | @Pointcut("execution(* com.jbd.saop.order.dao.*.*(..))") 10 | public void anyDaoMethod() {} 11 | } 12 | -------------------------------------------------------------------------------- /spring-aop-07-ordering-advice/src/main/java/com/jbd/saop/order/advice/SecondAdvice.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.order.advice; 2 | 3 | import org.aspectj.lang.JoinPoint; 4 | import org.aspectj.lang.annotation.After; 5 | import org.aspectj.lang.annotation.Aspect; 6 | import org.aspectj.lang.annotation.Before; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.core.annotation.Order; 9 | 10 | @Aspect 11 | @Configuration 12 | @Order(1) 13 | public class SecondAdvice { 14 | 15 | @Before("CommonPointcut.anyDaoMethod()") 16 | public void beforeAdvice(JoinPoint joinPoint) { 17 | System.out.println("\n======= Inside @Before() - 1 ======="); 18 | System.out.println(joinPoint.getSignature().toShortString()); 19 | } 20 | 21 | @After("CommonPointcut.anyDaoMethod()") 22 | public void afterAdvice(JoinPoint joinPoint) { 23 | System.out.println("\n======= Inside @After() - 1 ======="); 24 | System.out.println(joinPoint.getSignature().toShortString()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-aop-07-ordering-advice/src/main/java/com/jbd/saop/order/advice/ThirdAdvice.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.order.advice; 2 | 3 | import org.aspectj.lang.ProceedingJoinPoint; 4 | import org.aspectj.lang.annotation.Around; 5 | import org.aspectj.lang.annotation.Aspect; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.core.annotation.Order; 8 | 9 | @Configuration 10 | @Aspect 11 | @Order(2) 12 | public class ThirdAdvice { 13 | 14 | @Around("CommonPointcut.anyDaoMethod()") 15 | public Object aroundAdvice(ProceedingJoinPoint pJoinPoint) throws Throwable { 16 | System.out.println("\n======= Inside @Around() - 2 ======="); 17 | System.out.println(pJoinPoint.getSignature().toShortString()); 18 | Object result = pJoinPoint.proceed(); 19 | System.out.println("\n======= Inside @Around() - 2 ======="); 20 | return result; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /spring-aop-07-ordering-advice/src/main/java/com/jbd/saop/order/advice/ZeroAdvice.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.order.advice; 2 | 3 | import org.aspectj.lang.JoinPoint; 4 | import org.aspectj.lang.annotation.*; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.core.annotation.Order; 7 | 8 | @Aspect 9 | @Configuration 10 | @Order(0) 11 | public class ZeroAdvice { 12 | 13 | @Before("CommonPointcut.anyDaoMethod()") 14 | public void beforeAdvice(JoinPoint joinPoint) { 15 | System.out.println("\n======= Inside @Before() - 0 ======="); 16 | System.out.println(joinPoint.getSignature().toShortString()); 17 | } 18 | 19 | @After("CommonPointcut.anyDaoMethod()") 20 | public void afterAdvice(JoinPoint joinPoint) { 21 | System.out.println("\n======= Inside @After() - 0 ======="); 22 | System.out.println(joinPoint.getSignature().toShortString()); 23 | } 24 | 25 | @AfterReturning( 26 | pointcut = "CommonPointcut.anyDaoMethod()", 27 | returning = "result") 28 | public void afterReturningAdvice(JoinPoint joinPoint, Object result) { 29 | System.out.println("\n======= Inside @AfterReturning() - 0 ======="); 30 | System.out.println(joinPoint.getSignature().toShortString()); 31 | System.out.println("result: " + result); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /spring-aop-07-ordering-advice/src/main/java/com/jbd/saop/order/dao/EmailRepository.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.order.dao; 2 | 3 | import org.springframework.scheduling.annotation.Async; 4 | import org.springframework.stereotype.Repository; 5 | 6 | @Repository 7 | public class EmailRepository { 8 | 9 | @Async 10 | public void sendRegistrationEmail(String email){ 11 | System.out.println("Registration email will be sent to: " + email); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-aop-07-ordering-advice/src/main/java/com/jbd/saop/order/dao/UserRepository.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.order.dao; 2 | 3 | import org.springframework.stereotype.Repository; 4 | 5 | //A very stupid demo repository 6 | @Repository 7 | public class UserRepository { 8 | //Add a user 9 | public UserRepository add(String username, String password) { 10 | if(username == null) { 11 | throw new RuntimeException("username is null", new NullPointerException()); 12 | } 13 | if (password == null) { 14 | throw new RuntimeException("password is null", new NullPointerException()); 15 | } 16 | System.out.println("New user added: " + username); 17 | return this; 18 | } 19 | 20 | //Update an user 21 | public UserRepository update(String username, String email) { 22 | System.out.println("Update email: " + email); 23 | return this; 24 | } 25 | 26 | //Delete an user 27 | public boolean delete(String username){ 28 | if (username == null) { 29 | throw new RuntimeException("username is null", new NullPointerException()); 30 | } 31 | System.out.println("User deleted: " + username); 32 | return true; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /spring-aop-07-ordering-advice/src/main/java/com/jbd/saop/order/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.order.service; 2 | 3 | import com.jbd.saop.order.dao.EmailRepository; 4 | import com.jbd.saop.order.dao.UserRepository; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.stereotype.Service; 7 | 8 | @Service 9 | public class UserService { 10 | @Autowired 11 | private UserRepository userRepository; 12 | 13 | @Autowired 14 | private EmailRepository emailRepository; 15 | 16 | public void registerUser(String username, String email, String password) { 17 | userRepository.add(username, password); 18 | userRepository.update(username, email); 19 | emailRepository.sendRegistrationEmail(email); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /spring-aop-07-ordering-advice/src/test/java/com/jbd/saop/order/TestAdviceOrdering.java: -------------------------------------------------------------------------------- 1 | package com.jbd.saop.order; 2 | 3 | import com.jbd.saop.order.dao.UserRepository; 4 | import com.jbd.saop.order.service.UserService; 5 | import org.junit.jupiter.api.Test; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 8 | 9 | @SpringJUnitConfig(ApplicationConfig.class) 10 | public class TestAdviceOrdering { 11 | 12 | @Autowired 13 | private UserService userService; 14 | 15 | @Autowired 16 | private UserRepository userRepository; 17 | 18 | @Test 19 | public void testAddUser() { 20 | userRepository.add("abc", "abc@123"); 21 | } 22 | 23 | @Test 24 | public void testRegUser() { 25 | userService.registerUser("abc", "abc@jbd.com", "abc@123"); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /spring-aop-08-advice-parameters/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-aop-08-advice-parameters 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-aop-09-custom-annotation/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-aop-09-custom-annotation 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-aop-10-inter-type-declarations/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-aop-10-inter-type-declarations 13 | 14 | 15 | -------------------------------------------------------------------------------- /spring-core-Import-ImportResource/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | spring-core-Import-ImportResource 12 | Learn about Import and ImportResource annotations 13 | 14 | 15 | 16 | org.springframework 17 | spring-core 18 | 19 | 20 | 21 | org.springframework 22 | spring-context 23 | 24 | 25 | 26 | 27 | 28 | 29 | com.jstobigdata 30 | spring-tutorial-boms 31 | 1.0-SNAPSHOT 32 | pom 33 | import 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /spring-core-Import-ImportResource/src/main/java/com/jbd/sc/importannotation/ApplicationConfig.java: -------------------------------------------------------------------------------- 1 | package com.jbd.sc.importannotation; 2 | 3 | import com.jbd.sc.items.offline.OfflineOrderProcessor; 4 | import com.jbd.sc.items.offline.OfflineStoreConfig; 5 | import com.jbd.sc.items.online.OnlineOrderProcessor; 6 | import org.springframework.context.ApplicationContext; 7 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 8 | import org.springframework.context.annotation.Configuration; 9 | import org.springframework.context.annotation.Import; 10 | import org.springframework.context.annotation.ImportResource; 11 | 12 | @Configuration 13 | @Import(OfflineStoreConfig.class) 14 | @ImportResource("classpath:online-store-config.xml") 15 | public class ApplicationConfig { 16 | 17 | public static void main(String[] args) { 18 | ApplicationContext context 19 | = new AnnotationConfigApplicationContext(ApplicationConfig.class); 20 | 21 | context.getBean(OfflineOrderProcessor.class).processOrder("Book"); 22 | context.getBean(OnlineOrderProcessor.class).processOrder("Mobile Phone"); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /spring-core-Import-ImportResource/src/main/java/com/jbd/sc/items/offline/OfflineOrderProcessor.java: -------------------------------------------------------------------------------- 1 | package com.jbd.sc.items.offline; 2 | 3 | public class OfflineOrderProcessor { 4 | public void processOrder(String item) { 5 | System.out.println("Offline order processed: " + item); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /spring-core-Import-ImportResource/src/main/java/com/jbd/sc/items/offline/OfflineStoreConfig.java: -------------------------------------------------------------------------------- 1 | package com.jbd.sc.items.offline; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | @Configuration 7 | public class OfflineStoreConfig { 8 | 9 | @Bean 10 | public OfflineOrderProcessor offlineOrderProcessor() { 11 | return new OfflineOrderProcessor(); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-core-Import-ImportResource/src/main/java/com/jbd/sc/items/online/OnlineOrderProcessor.java: -------------------------------------------------------------------------------- 1 | package com.jbd.sc.items.online; 2 | 3 | public class OnlineOrderProcessor { 4 | public void processOrder(String item) { 5 | System.out.println("Online order processed: " + item); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /spring-core-Import-ImportResource/src/main/resources/online-store-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | -------------------------------------------------------------------------------- /spring-core-beanlifecycle/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-core-beanlifecycle 13 | 14 | 15 | 16 | org.springframework 17 | spring-core 18 | 19 | 20 | 21 | org.springframework 22 | spring-context 23 | 24 | 25 | 26 | javax.annotation 27 | javax.annotation-api 28 | 29 | 30 | 31 | 32 | 33 | 34 | com.jstobigdata 35 | spring-tutorial-boms 36 | pom 37 | import 38 | 1.0-SNAPSHOT 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /spring-core-beanlifecycle/src/main/java/bean/dfault/callbacks/LifecycleBean.java: -------------------------------------------------------------------------------- 1 | package bean.dfault.callbacks; 2 | 3 | public interface LifecycleBean { 4 | 5 | //initialization callback 6 | public void init(); 7 | 8 | //Destruction callbacks 9 | public void destroy(); 10 | } 11 | -------------------------------------------------------------------------------- /spring-core-beanlifecycle/src/main/java/bean/dfault/callbacks/MyServiceA.java: -------------------------------------------------------------------------------- 1 | package bean.dfault.callbacks; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | @Component 6 | public class MyServiceA implements LifecycleBean { 7 | 8 | @Override 9 | public void init() { 10 | System.out.println("Initialization method of " + this.getClass().getName()); 11 | } 12 | 13 | @Override 14 | public void destroy() { 15 | System.out.println("Destroy method of " + this.getClass().getName()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /spring-core-beanlifecycle/src/main/java/bean/dfault/callbacks/MyServiceB.java: -------------------------------------------------------------------------------- 1 | package bean.dfault.callbacks; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | @Component 6 | public class MyServiceB implements LifecycleBean { 7 | 8 | @Override 9 | public void init() { 10 | System.out.println("Initialization method of " + this.getClass().getName()); 11 | } 12 | 13 | @Override 14 | public void destroy() { 15 | System.out.println("Destroy method of " + this.getClass().getName()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /spring-core-beanlifecycle/src/main/java/bean/dfault/callbacks/TestDefaultCallbacks.java: -------------------------------------------------------------------------------- 1 | package bean.dfault.callbacks; 2 | 3 | import org.springframework.context.ConfigurableApplicationContext; 4 | import org.springframework.context.support.ClassPathXmlApplicationContext; 5 | 6 | public class TestDefaultCallbacks { 7 | public static void main(String[] args) { 8 | ConfigurableApplicationContext context 9 | = new ClassPathXmlApplicationContext("bean.default.lifecycle.xml"); 10 | 11 | //This is important 12 | context.registerShutdownHook(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-core-beanlifecycle/src/main/java/bean/jsr/callbacks/JsrExampleBean.java: -------------------------------------------------------------------------------- 1 | package bean.jsr.callbacks; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import javax.annotation.PostConstruct; 6 | import javax.annotation.PreDestroy; 7 | 8 | @Component 9 | public class JsrExampleBean { 10 | 11 | @PostConstruct 12 | public void initCacheData() { 13 | System.out.println("Your custom initialization code goes here..."); 14 | } 15 | 16 | @PreDestroy 17 | public void clearCacheData() { 18 | System.out.println("Your custom destroy code goes here..."); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /spring-core-beanlifecycle/src/main/java/bean/jsr/callbacks/JsrExampleConfig.java: -------------------------------------------------------------------------------- 1 | package bean.jsr.callbacks; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | @Configuration 7 | @ComponentScan("bean.jsr.callbacks") 8 | public class JsrExampleConfig { 9 | //Extra config goes 10 | } -------------------------------------------------------------------------------- /spring-core-beanlifecycle/src/main/java/bean/jsr/callbacks/TestJsrExample.java: -------------------------------------------------------------------------------- 1 | package bean.jsr.callbacks; 2 | 3 | import org.springframework.context.ConfigurableApplicationContext; 4 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 | 6 | public class TestJsrExample { 7 | public static void main(String[] args) { 8 | 9 | ConfigurableApplicationContext context 10 | = new AnnotationConfigApplicationContext(JsrExampleConfig.class); 11 | context.getBean(JsrExampleBean.class); 12 | 13 | //This is important 14 | context.registerShutdownHook(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-core-beanlifecycle/src/main/java/bean/lifecycle/callbacks/BLCConfigClass.java: -------------------------------------------------------------------------------- 1 | package bean.lifecycle.callbacks; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.ComponentScan; 5 | import org.springframework.context.annotation.Configuration; 6 | 7 | @Configuration 8 | @ComponentScan("bean.lifecycle.callbacks") 9 | public class BLCConfigClass { 10 | 11 | @Bean(initMethod = "init", destroyMethod = "destroy") 12 | public ExampleBean1 exampleBean1() { 13 | return new ExampleBean1(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /spring-core-beanlifecycle/src/main/java/bean/lifecycle/callbacks/ExampleBean1.java: -------------------------------------------------------------------------------- 1 | package bean.lifecycle.callbacks; 2 | 3 | public class ExampleBean1 { 4 | 5 | public void init() { 6 | System.out.println("Initialization method of " + this.getClass().getName()); 7 | } 8 | 9 | public void destroy() { 10 | System.out.println("Destroy method of " + this.getClass().getName()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /spring-core-beanlifecycle/src/main/java/bean/lifecycle/callbacks/ExampleBean2.java: -------------------------------------------------------------------------------- 1 | package bean.lifecycle.callbacks; 2 | 3 | import org.springframework.beans.factory.DisposableBean; 4 | import org.springframework.beans.factory.InitializingBean; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component 8 | public class ExampleBean2 implements InitializingBean, DisposableBean { 9 | 10 | @Override 11 | public void afterPropertiesSet() throws Exception { 12 | System.out.println("Initialization method of " + this.getClass().getName()); 13 | } 14 | 15 | @Override 16 | public void destroy() throws Exception { 17 | System.out.println("Destroy method of " + this.getClass().getName()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /spring-core-beanlifecycle/src/main/java/bean/lifecycle/callbacks/TestInitDestroy.java: -------------------------------------------------------------------------------- 1 | package bean.lifecycle.callbacks; 2 | 3 | import org.springframework.context.ConfigurableApplicationContext; 4 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 | 6 | public class TestInitDestroy { 7 | public static void main(String[] args) { 8 | ConfigurableApplicationContext context 9 | = new AnnotationConfigApplicationContext(BLCConfigClass.class); 10 | 11 | //This is important 12 | context.registerShutdownHook(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-core-beanlifecycle/src/main/java/bean/start/stop/callbacks/LifecycleExampleBean.java: -------------------------------------------------------------------------------- 1 | package bean.start.stop.callbacks; 2 | 3 | import org.springframework.context.Lifecycle; 4 | import org.springframework.context.event.ContextRefreshedEvent; 5 | import org.springframework.context.event.EventListener; 6 | import org.springframework.stereotype.Component; 7 | 8 | @Component 9 | public class LifecycleExampleBean implements Lifecycle { 10 | private boolean status = false; 11 | 12 | @Override 13 | public void start() { 14 | System.out.println("Inside the LifeCycle start method"); 15 | this.status = true; 16 | } 17 | 18 | @Override 19 | public void stop() { 20 | System.out.println("Inside the LifeCycle stop method"); 21 | this.status = false; 22 | } 23 | 24 | @Override 25 | public boolean isRunning() { 26 | return this.status; 27 | } 28 | 29 | /*@EventListener 30 | public void onAppEvent(ContextRefreshedEvent event) { 31 | System.out.println(event.getSource()); 32 | }*/ 33 | } 34 | -------------------------------------------------------------------------------- /spring-core-beanlifecycle/src/main/java/bean/start/stop/callbacks/TestLifecycleBean.java: -------------------------------------------------------------------------------- 1 | package bean.start.stop.callbacks; 2 | 3 | import org.springframework.context.ConfigurableApplicationContext; 4 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 | 6 | public class TestLifecycleBean { 7 | public static void main(String[] args) { 8 | ConfigurableApplicationContext context 9 | = new AnnotationConfigApplicationContext(LifecycleExampleBean.class); 10 | 11 | context.start(); 12 | context.getBean(LifecycleExampleBean.class); 13 | context.stop(); 14 | context.start(); 15 | //This is important - shutdown gracefully 16 | context.registerShutdownHook(); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /spring-core-beanlifecycle/src/main/resources/bean.default.lifecycle.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /spring-core-beanpostprocessor/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-core-beanpostprocessor 13 | 14 | 15 | 16 | org.springframework 17 | spring-core 18 | 19 | 20 | 21 | org.springframework 22 | spring-context 23 | 24 | 25 | 26 | javax.annotation 27 | javax.annotation-api 28 | 29 | 30 | 31 | 32 | 33 | 34 | com.jstobigdata 35 | spring-tutorial-boms 36 | pom 37 | import 38 | 1.0-SNAPSHOT 39 | 40 | 41 | 42 | -------------------------------------------------------------------------------- /spring-core-beanpostprocessor/src/main/java/core/bean/postprocessor/ExampleBean.java: -------------------------------------------------------------------------------- 1 | package core.bean.postprocessor; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | import javax.annotation.PostConstruct; 6 | import javax.annotation.PreDestroy; 7 | 8 | @Component 9 | public class ExampleBean { 10 | 11 | @PostConstruct 12 | public void initData() { 13 | System.out.println("Your custom initialization code goes here..."); 14 | } 15 | 16 | @PreDestroy 17 | public void clearData() { 18 | System.out.println("Your custom destroy code goes here..."); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /spring-core-beanpostprocessor/src/main/java/core/bean/postprocessor/ExampleConfig.java: -------------------------------------------------------------------------------- 1 | package core.bean.postprocessor; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | @Configuration 7 | @ComponentScan("core.bean.postprocessor") 8 | public class ExampleConfig { 9 | //Extra config goes 10 | } -------------------------------------------------------------------------------- /spring-core-beanpostprocessor/src/main/java/core/bean/postprocessor/TestExampleBean.java: -------------------------------------------------------------------------------- 1 | package core.bean.postprocessor; 2 | 3 | import org.springframework.context.ConfigurableApplicationContext; 4 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 | 6 | public class TestExampleBean { 7 | public static void main(String[] args) { 8 | 9 | ConfigurableApplicationContext context 10 | = new AnnotationConfigApplicationContext(ExampleConfig.class); 11 | context.getBean(ExampleBean.class); 12 | 13 | //This is important 14 | context.registerShutdownHook(); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-core-beanpostprocessor/src/main/java/core/bean/postprocessor/TraceBeanPostProcessor.java: -------------------------------------------------------------------------------- 1 | package core.bean.postprocessor; 2 | 3 | import org.springframework.beans.BeansException; 4 | import org.springframework.beans.factory.config.BeanPostProcessor; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component 8 | public class TraceBeanPostProcessor implements BeanPostProcessor { 9 | 10 | @Override 11 | public Object postProcessBeforeInitialization(Object bean, String beanName) 12 | throws BeansException { 13 | System.out.println("Inside postProcessBeforeInitialization - " + beanName); 14 | return bean; 15 | } 16 | 17 | @Override 18 | public Object postProcessAfterInitialization(Object bean, String beanName) 19 | throws BeansException { 20 | System.out.println("Inside postProcessAfterInitialization - " + beanName); 21 | return bean; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-core-environment-abstraction/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-core-environment-abstraction 13 | 14 | 15 | 16 | org.springframework 17 | spring-core 18 | 19 | 20 | 21 | org.springframework 22 | spring-context 23 | 24 | 25 | 26 | org.junit.jupiter 27 | junit-jupiter-api 28 | test 29 | 30 | 31 | 32 | org.junit.platform 33 | junit-platform-runner 34 | test 35 | 36 | 37 | 38 | org.springframework 39 | spring-test 40 | test 41 | 42 | 43 | 44 | 45 | 46 | 47 | com.jstobigdata 48 | spring-tutorial-boms 49 | 1.0-SNAPSHOT 50 | pom 51 | import 52 | 53 | 54 | 55 | 56 | -------------------------------------------------------------------------------- /spring-core-environment-abstraction/src/main/java/com/jsbd/profiles/DataSource.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.profiles; 2 | 3 | public interface DataSource { 4 | public String getName(); 5 | } 6 | -------------------------------------------------------------------------------- /spring-core-environment-abstraction/src/main/java/com/jsbd/profiles/GraphQlDataSource.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.profiles; 2 | 3 | import org.springframework.context.annotation.Profile; 4 | import org.springframework.stereotype.Component; 5 | 6 | @Component 7 | @Profile("graphQl") 8 | public class GraphQlDataSource implements DataSource { 9 | @Override 10 | public String getName() { 11 | return "GraphQlDataSource"; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-core-environment-abstraction/src/main/java/com/jsbd/profiles/ProfilesApp.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.profiles; 2 | 3 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 4 | 5 | public class ProfilesApp { 6 | public static void main(String[] args) { 7 | AnnotationConfigApplicationContext context 8 | = new AnnotationConfigApplicationContext(); 9 | context.getEnvironment().setActiveProfiles("graphQl"); 10 | context.register(ProfilesConfig.class); 11 | context.refresh(); 12 | DataSource dataSource = context.getBean(DataSource.class); 13 | System.out.println(dataSource.getClass()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /spring-core-environment-abstraction/src/main/java/com/jsbd/profiles/ProfilesConfig.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.profiles; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | @Configuration 7 | @ComponentScan("com.jsbd.profiles") 8 | public class ProfilesConfig { 9 | } 10 | -------------------------------------------------------------------------------- /spring-core-environment-abstraction/src/main/java/com/jsbd/profiles/RestDataSource.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.profiles; 2 | 3 | import org.springframework.context.annotation.Profile; 4 | import org.springframework.stereotype.Component; 5 | 6 | @Component 7 | @Profile({"rest", "default"}) 8 | public class RestDataSource implements DataSource { 9 | 10 | //Logic to fetch data from API 11 | @Override 12 | public String getName() { 13 | return "RestDataSource"; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /spring-core-environment-abstraction/src/main/java/com/jsbd/propertysource/AppConfig.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.propertysource; 2 | 3 | import org.springframework.context.annotation.*; 4 | 5 | @Configuration 6 | @PropertySource("classpath:app.properties") 7 | @ComponentScan("com.jsbd.propertysource") 8 | //@ImportResource("classpth:${app.name}.xml") 9 | public class AppConfig { 10 | 11 | } 12 | -------------------------------------------------------------------------------- /spring-core-environment-abstraction/src/main/java/com/jsbd/propertysource/AppLogger.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.propertysource; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.core.env.Environment; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component 8 | public class AppLogger { 9 | 10 | @Autowired 11 | private Environment env; 12 | 13 | public void printLogLevel() { 14 | String logLevel = env.getProperty("app.log.level"); 15 | System.out.println(logLevel); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /spring-core-environment-abstraction/src/main/java/com/jsbd/propertysource/ConnectionManager.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.propertysource; 2 | 3 | import org.springframework.beans.factory.annotation.Value; 4 | import org.springframework.stereotype.Component; 5 | 6 | import java.util.StringJoiner; 7 | 8 | @Component 9 | public class ConnectionManager { 10 | 11 | @Value("${app.database.url}") 12 | private String url; 13 | 14 | @Value("${app.database.username}") 15 | private String username; 16 | 17 | @Value("${app.database.password}") 18 | private String password; 19 | 20 | @Value("${no.value}") 21 | private String noValue; 22 | 23 | @Override 24 | public String toString() { 25 | return new StringJoiner(", ", ConnectionManager.class.getSimpleName() + "[", "]") 26 | .add("url='" + url + "'") 27 | .add("username='" + username + "'") 28 | .add("password='" + password + "'") 29 | .add("noValue='" + noValue +"'") 30 | .toString(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /spring-core-environment-abstraction/src/main/java/com/jsbd/propertysource/TestClass.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.propertysource; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 | 6 | public class TestClass { 7 | public static void main(String[] args) { 8 | ApplicationContext context 9 | = new AnnotationConfigApplicationContext(AppConfig.class); 10 | System.out.println(context.getBean(ConnectionManager.class)); 11 | 12 | context.getBean(AppLogger.class).printLogLevel(); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-core-environment-abstraction/src/main/resources/app.properties: -------------------------------------------------------------------------------- 1 | app.database.url=http://random-url 2 | app.database.password=abcd@123 3 | app.database.username=root 4 | app.log.level=DEBUG -------------------------------------------------------------------------------- /spring-core-environment-abstraction/src/test/java/com/jsbd/profiles/TestDefaultProfile.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.profiles; 2 | 3 | import org.junit.Assert; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 7 | 8 | @SpringJUnitConfig(ProfilesConfig.class) 9 | public class TestDefaultProfile { 10 | @Autowired 11 | private DataSource dataSource; 12 | 13 | @Test 14 | public void testDefaultProfile() { 15 | Assert.assertNotNull(dataSource); 16 | Assert.assertEquals(dataSource.getClass(), RestDataSource.class); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /spring-core-environment-abstraction/src/test/java/com/jsbd/profiles/TestGraphQlProfile.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.profiles; 2 | 3 | import org.junit.Assert; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.test.context.ActiveProfiles; 7 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 8 | 9 | @SpringJUnitConfig(ProfilesConfig.class) 10 | @ActiveProfiles("graphQl") 11 | public class TestGraphQlProfile { 12 | 13 | @Autowired 14 | private DataSource dataSource; 15 | 16 | @Test 17 | public void testGraphQlProfile() { 18 | System.out.println(dataSource); 19 | Assert.assertNotNull(dataSource); 20 | Assert.assertEquals(dataSource.getClass(), GraphQlDataSource.class); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /spring-core-environment-abstraction/src/test/java/com/jsbd/profiles/TestRestProfile.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.profiles; 2 | 3 | import org.junit.Assert; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.test.context.ActiveProfiles; 7 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 8 | 9 | @SpringJUnitConfig(ProfilesConfig.class) 10 | @ActiveProfiles("rest") 11 | public class TestRestProfile { 12 | 13 | @Autowired 14 | private DataSource dataSource; 15 | 16 | @Test 17 | public void testDataSource(){ 18 | Assert.assertNotNull(dataSource); 19 | Assert.assertEquals(dataSource.getClass(), RestDataSource.class); 20 | System.out.println(dataSource.getClass()); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /spring-core-events/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | spring-framework-tutorial-parent 8 | com.jstobigdata 9 | 1.0-SNAPSHOT 10 | 11 | 4.0.0 12 | 13 | spring-core-events 14 | 15 | 16 | 17 | org.springframework 18 | spring-core 19 | 20 | 21 | 22 | org.springframework 23 | spring-context 24 | 25 | 26 | 27 | 28 | org.junit.jupiter 29 | junit-jupiter-api 30 | test 31 | 32 | 33 | 34 | org.junit.platform 35 | junit-platform-runner 36 | test 37 | 38 | 39 | 40 | org.springframework 41 | spring-test 42 | test 43 | 44 | 45 | 46 | 47 | 48 | 49 | com.jstobigdata 50 | spring-tutorial-boms 51 | 1.0-SNAPSHOT 52 | pom 53 | import 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /spring-core-events/src/main/java/com/jsbd/events/AnnEventListener.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.events; 2 | 3 | import org.springframework.context.event.EventListener; 4 | import org.springframework.scheduling.annotation.Async; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component 8 | public class AnnEventListener { 9 | 10 | //Event Listener with a Spring SpEL expression 11 | @EventListener(condition = "#userEvent.eventId == 102") 12 | public void processEvent(UserEvent userEvent) { 13 | System.out.println("====== Conditional UserEvent Listener ====="); 14 | System.out.println(userEvent); 15 | } 16 | 17 | @EventListener 18 | public void genericListener(UserEvent userEvent) { 19 | System.out.println("\n===== General UserEvent Listener ====="); 20 | System.out.println(userEvent); 21 | } 22 | 23 | //Async Event listener 24 | @EventListener 25 | @Async 26 | public void asyncListener(UserEvent userEvent) { 27 | System.out.println("===== Async UserEvent Listener ====="); 28 | System.out.println(userEvent); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /spring-core-events/src/main/java/com/jsbd/events/AppConfig.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.events; 2 | 3 | import org.springframework.context.ApplicationEventPublisher; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.ComponentScan; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.context.annotation.Scope; 8 | import org.springframework.context.event.ApplicationEventMulticaster; 9 | import org.springframework.context.event.SimpleApplicationEventMulticaster; 10 | import org.springframework.core.task.SimpleAsyncTaskExecutor; 11 | import org.springframework.scheduling.annotation.EnableAsync; 12 | import org.springframework.scheduling.support.TaskUtils; 13 | 14 | @Configuration 15 | @ComponentScan("com.jsbd.events") 16 | @EnableAsync 17 | public class AppConfig { 18 | 19 | //Enable async event listener globally for ApplicationContext. 20 | /*@Bean 21 | //@Scope("singleton") 22 | ApplicationEventMulticaster eventMulticaster() { 23 | SimpleApplicationEventMulticaster eventMulticaster = new SimpleApplicationEventMulticaster(); 24 | eventMulticaster.setTaskExecutor(new SimpleAsyncTaskExecutor()); 25 | //optionally set the ErrorHandler 26 | eventMulticaster.setErrorHandler(TaskUtils.LOG_AND_PROPAGATE_ERROR_HANDLER); 27 | return eventMulticaster; 28 | }*/ 29 | } 30 | -------------------------------------------------------------------------------- /spring-core-events/src/main/java/com/jsbd/events/EventListener.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.events; 2 | 3 | import org.springframework.context.ApplicationListener; 4 | import org.springframework.stereotype.Component; 5 | 6 | @Component 7 | public class EventListener implements ApplicationListener { 8 | 9 | @Override 10 | public void onApplicationEvent(UserEvent event) { 11 | System.out.println("======= UserEvent Listener ======="); 12 | System.out.println(event); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-core-events/src/main/java/com/jsbd/events/EventPublisher.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.events; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.ApplicationEventPublisher; 5 | import org.springframework.stereotype.Component; 6 | 7 | @Component 8 | public class EventPublisher { 9 | 10 | //TODO - Use setter based injection 11 | @Autowired 12 | private ApplicationEventPublisher applicationEventPublisher; 13 | 14 | public void publishEvent(String message, Integer eventId) { 15 | applicationEventPublisher.publishEvent( 16 | new UserEvent(this, message, eventId) 17 | ); 18 | } 19 | 20 | 21 | } 22 | -------------------------------------------------------------------------------- /spring-core-events/src/main/java/com/jsbd/events/UserEvent.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.events; 2 | 3 | import org.springframework.context.ApplicationEvent; 4 | 5 | import java.util.StringJoiner; 6 | 7 | public class UserEvent extends ApplicationEvent { 8 | 9 | //Custom property 10 | private String message; 11 | 12 | //Custom property 13 | private Integer eventId; 14 | 15 | /** 16 | * Create a new {@code ApplicationEvent}. 17 | * 18 | * @param source the object on which the event initially occurred or with 19 | * which the event is associated (never {@code null}) 20 | */ 21 | public UserEvent(Object source) { 22 | super(source); 23 | } 24 | 25 | /** 26 | * Constructor to set the properties. 27 | * 28 | * @param source 29 | * @param message 30 | */ 31 | public UserEvent(Object source, String message, 32 | Integer eventId) { 33 | super(source); 34 | this.message = message; 35 | this.eventId = eventId; 36 | } 37 | 38 | public String getMessage() { 39 | return this.message; 40 | } 41 | 42 | public Integer getEventId() { 43 | return this.eventId; 44 | } 45 | 46 | @Override 47 | public String toString() { 48 | return new StringJoiner(", ", UserEvent.class.getSimpleName() + "[", "]") 49 | .add("message='" + message + "'") 50 | .add("source=" + source) 51 | .add("eventId=" + eventId) 52 | .toString(); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /spring-core-events/src/main/java/com/jsbd/events/generic/GenericEvent.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.events.generic; 2 | 3 | import org.springframework.core.ResolvableType; 4 | import org.springframework.core.ResolvableTypeProvider; 5 | 6 | import java.util.StringJoiner; 7 | 8 | /** 9 | * Recommended to extend {@link ResolvableTypeProvider}. 10 | * 11 | * @param - The generic message class. 12 | */ 13 | public class GenericEvent implements ResolvableTypeProvider { 14 | private T message; 15 | private Object source; 16 | 17 | public GenericEvent(Object source, T message) { 18 | this.source = source; 19 | this.message = message; 20 | } 21 | 22 | @Override 23 | public ResolvableType getResolvableType() { 24 | return ResolvableType.forClassWithGenerics( 25 | getClass(), ResolvableType.forInstance(getSource()) 26 | ); 27 | } 28 | 29 | public T getMessage() { 30 | return message; 31 | } 32 | 33 | public Object getSource() { 34 | return source; 35 | } 36 | 37 | @Override 38 | public String toString() { 39 | return new StringJoiner(", ", GenericEvent.class.getSimpleName() + "[", "]") 40 | .add("message=" + message) 41 | .add("source=" + source) 42 | .toString(); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /spring-core-events/src/main/java/com/jsbd/events/generic/GenericEventListener.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.events.generic; 2 | 3 | import org.springframework.context.event.EventListener; 4 | import org.springframework.stereotype.Component; 5 | 6 | @Component 7 | public class GenericEventListener { 8 | 9 | @EventListener 10 | public void listenEvent(GenericEvent event) { 11 | System.out.println(event); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-core-events/src/main/java/com/jsbd/events/generic/Person.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.events.generic; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public class Person { 6 | private String name; 7 | 8 | public Person(String name) { 9 | this.name = name; 10 | } 11 | 12 | public String getName() { 13 | return name; 14 | } 15 | 16 | public void setName(String name) { 17 | this.name = name; 18 | } 19 | 20 | @Override 21 | public String toString() { 22 | return new StringJoiner(", ", Person.class.getSimpleName() + "[", "]") 23 | .add("name='" + name + "'") 24 | .toString(); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-core-events/src/test/java/com/jsbd/events/synchronous/EventPublisherTest.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.events.synchronous; 2 | 3 | import com.jsbd.events.AppConfig; 4 | import com.jsbd.events.EventPublisher; 5 | import com.jsbd.events.UserEvent; 6 | import org.junit.jupiter.api.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.context.event.ApplicationEventMulticaster; 9 | import org.springframework.scheduling.annotation.EnableAsync; 10 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 11 | 12 | @SpringJUnitConfig(AppConfig.class) 13 | //@EnableAsync 14 | class EventPublisherTest { 15 | 16 | @Autowired 17 | EventPublisher eventPublisher; 18 | 19 | @Test 20 | public void sendSynchronousEvent() { 21 | eventPublisher.publishEvent("User registered", 101); 22 | } 23 | 24 | @Test 25 | public void conditionalEvent() { 26 | eventPublisher.publishEvent("User logged in", 102); 27 | } 28 | 29 | @Test 30 | public void sendAsyncEvent() throws InterruptedException { 31 | //eventMulticaster.multicastEvent(new UserEvent(this, "User Logged out", 103)); 32 | Thread.sleep(10000); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /spring-core-events/src/test/java/com/jsbd/events/synchronous/GenericEventTest.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.events.synchronous; 2 | 3 | import com.jsbd.events.AppConfig; 4 | import com.jsbd.events.generic.GenericEvent; 5 | import com.jsbd.events.generic.Person; 6 | import org.junit.jupiter.api.Test; 7 | import org.springframework.beans.factory.annotation.Autowired; 8 | import org.springframework.context.ApplicationEventPublisher; 9 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 10 | 11 | @SpringJUnitConfig(AppConfig.class) 12 | public class GenericEventTest { 13 | @Autowired 14 | private ApplicationEventPublisher eventPublisher; 15 | 16 | @Test 17 | public void publishEvent() { 18 | GenericEvent event1 = new GenericEvent<>(this, new Person("John")); 19 | GenericEvent event2 = new GenericEvent<>(this, "Hello"); 20 | eventPublisher.publishEvent(event1); 21 | eventPublisher.publishEvent(event2); 22 | } 23 | } -------------------------------------------------------------------------------- /spring-core-ioc/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | spring-framework-tutorial-parent 8 | com.jstobigdata 9 | 1.0-SNAPSHOT 10 | 11 | 4.0.0 12 | 13 | spring-core-ioc 14 | 15 | 16 | 17 | org.springframework 18 | spring-core 19 | 20 | 21 | 22 | org.springframework 23 | spring-context 24 | 25 | 26 | 27 | 28 | javax.inject 29 | javax.inject 30 | 1 31 | 32 | 33 | 34 | 35 | javax.annotation 36 | javax.annotation-api 37 | 38 | 39 | 40 | org.junit.jupiter 41 | junit-jupiter-api 42 | test 43 | 44 | 45 | 46 | org.junit.platform 47 | junit-platform-runner 48 | test 49 | 50 | 51 | 52 | org.springframework 53 | spring-test 54 | test 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | com.jstobigdata 64 | spring-tutorial-boms 65 | pom 66 | import 67 | 1.0-SNAPSHOT 68 | 69 | 70 | 71 | 72 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/autowire/AutowireBeanConfig.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.autowire; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.ComponentScan; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.context.annotation.Scope; 7 | 8 | import java.util.Random; 9 | import java.util.UUID; 10 | 11 | @Configuration 12 | @ComponentScan("basic.ioc.autowire") 13 | public class AutowireBeanConfig { 14 | 15 | @Bean 16 | @Scope("prototype") 17 | public Item item(){ 18 | return new Item ( 19 | new Random().nextLong(), 20 | UUID.randomUUID().toString() 21 | ); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/autowire/Item.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.autowire; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public class Item { 6 | private Long id; 7 | private String name; 8 | 9 | public Item() { 10 | } 11 | 12 | public Item(Long id, String name) { 13 | this.id = id; 14 | this.name = name; 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return new StringJoiner(", ", Item.class.getSimpleName() + "[", "]") 36 | .add("id=" + id) 37 | .add("name='" + name + "'") 38 | .toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/autowire/Store.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.autowire; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Scope; 5 | import org.springframework.stereotype.Component; 6 | 7 | import java.util.StringJoiner; 8 | import java.util.UUID; 9 | 10 | @Component 11 | @Scope("prototype") 12 | public class Store { 13 | private String id; 14 | 15 | /* Field injection is not recommended. Instead use, Constructor 16 | or Setter injection 17 | */ 18 | @Autowired 19 | private Item item; 20 | 21 | public Store() { 22 | id = UUID.randomUUID().toString(); 23 | } 24 | 25 | public Store(String id, Item item) { 26 | this.id = id; 27 | this.item = item; 28 | } 29 | 30 | public String getId() { 31 | return id; 32 | } 33 | 34 | public void setId(String id) { 35 | this.id = id; 36 | } 37 | 38 | public Item getItem() { 39 | return item; 40 | } 41 | 42 | public void setItem(Item item) { 43 | this.item = item; 44 | } 45 | 46 | @Override 47 | public String toString() { 48 | return new StringJoiner(", ", Store.class.getSimpleName() + "[", "]") 49 | .add("id=" + id) 50 | .add("item=" + item) 51 | .toString(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/autowire/TestAutowireExample.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.autowire; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 | 6 | public class TestAutowireExample { 7 | public static void main(String[] args) { 8 | ApplicationContext context = new AnnotationConfigApplicationContext(AutowireBeanConfig.class); 9 | 10 | System.out.println(context.getBean(Store.class)); 11 | System.out.println(context.getBean(Store.class)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/bean/collections/BeanReferenceCollections.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.bean.collections; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | 6 | import java.util.List; 7 | import java.util.StringJoiner; 8 | 9 | @Component 10 | public class BeanReferenceCollections { 11 | 12 | private List list; 13 | 14 | @Autowired 15 | public void setList(List list) { 16 | this.list = list; 17 | } 18 | 19 | @Override 20 | public String toString() { 21 | return new StringJoiner(", ", BeanReferenceCollections.class.getSimpleName() + "[", "]") 22 | .add("list=" + list) 23 | .toString(); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/bean/collections/CollectionsConfig.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.bean.collections; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.ComponentScan; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.core.annotation.Order; 7 | 8 | import java.util.*; 9 | 10 | @Configuration 11 | @ComponentScan("basic.ioc.bean.collections") 12 | public class CollectionsConfig { 13 | 14 | @Bean 15 | public List namesList() { 16 | List names = new ArrayList(); 17 | names.add("Salman Khan"); 18 | names.add("Hrithik Roshan"); 19 | return names; 20 | } 21 | 22 | @Bean 23 | public Set numbersBean() { 24 | Set numbers = new HashSet<>(); 25 | numbers.add(2222222222L); 26 | numbers.add(1212121212L); 27 | return numbers; 28 | } 29 | 30 | @Bean 31 | public Map phoneNameMapBean() { 32 | Map phoneNames = new HashMap<>(); 33 | phoneNames.put(888888888888L, "Ram"); 34 | phoneNames.put(777777777777L, "Bhim"); 35 | return phoneNames; 36 | } 37 | 38 | @Bean 39 | public JbdBean jbdBean1(){ 40 | JbdBean bean = new JbdBean(); 41 | bean.setUuid(UUID.randomUUID().toString()); 42 | return bean; 43 | } 44 | 45 | @Bean 46 | public JbdBean jbdBean2(){ 47 | JbdBean bean = new JbdBean(); 48 | bean.setUuid(UUID.randomUUID().toString()); 49 | return bean; 50 | } 51 | 52 | @Bean @Order(3) 53 | public NameBean name1(){ 54 | return new NameBean("John"); 55 | } 56 | 57 | @Bean @Order(1) 58 | public NameBean name2(){ 59 | return new NameBean("Ram"); 60 | } 61 | 62 | @Bean @Order(2) 63 | public NameBean name3(){ 64 | return new NameBean("Shyam"); 65 | } 66 | 67 | } 68 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/bean/collections/ConstructorInjection.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.bean.collections; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Set; 9 | import java.util.StringJoiner; 10 | 11 | @Component 12 | public class ConstructorInjection { 13 | 14 | private List names; 15 | private Set phones; 16 | private Map phoneNameMap; 17 | 18 | @Autowired 19 | public ConstructorInjection(List names, Set phones, 20 | Map phoneNameMap) { 21 | this.names = names; 22 | this.phones = phones; 23 | this.phoneNameMap = phoneNameMap; 24 | } 25 | 26 | public List getNames() { 27 | return names; 28 | } 29 | 30 | public void setNames(List names) { 31 | this.names = names; 32 | } 33 | 34 | public Set getPhones() { 35 | return phones; 36 | } 37 | 38 | public void setPhones(Set phones) { 39 | this.phones = phones; 40 | } 41 | 42 | public Map getPhoneNameMap() { 43 | return phoneNameMap; 44 | } 45 | 46 | public void setPhoneNameMap(Map phoneNameMap) { 47 | this.phoneNameMap = phoneNameMap; 48 | } 49 | 50 | @Override 51 | public String toString() { 52 | return new StringJoiner(", ", ConstructorInjection.class.getSimpleName() + "[", "]") 53 | .add("names=" + names) 54 | .add("phones=" + phones) 55 | .add("phoneNameMap=" + phoneNameMap) 56 | .toString(); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/bean/collections/FieldInjection.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.bean.collections; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Set; 9 | import java.util.StringJoiner; 10 | 11 | @Component 12 | public class FieldInjection { 13 | @Autowired //Avoid using property injection 14 | private List names; 15 | 16 | @Autowired //Avoid using property injection 17 | private Set phones; 18 | 19 | @Autowired //Avoid using property injection 20 | private Map phoneNameMap; 21 | 22 | @Override 23 | public String toString() { 24 | return new StringJoiner(", ", FieldInjection.class.getSimpleName() + "[", "]") 25 | .add("names=" + names) 26 | .add("phones=" + phones) 27 | .add("phoneNameMap=" + phoneNameMap) 28 | .toString(); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/bean/collections/JbdBean.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.bean.collections; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public class JbdBean { 6 | 7 | private String uuid; 8 | 9 | public void setUuid(String uuid) { 10 | this.uuid = uuid; 11 | } 12 | 13 | @Override 14 | public String toString() { 15 | return new StringJoiner(", ", JbdBean.class.getSimpleName() + "[", "]") 16 | .add("uuid='" + uuid + "'") 17 | .toString(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/bean/collections/NameBean.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.bean.collections; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public class NameBean { 6 | 7 | private String name; 8 | 9 | public NameBean(String name) { 10 | this.name = name; 11 | } 12 | 13 | @Override 14 | public String toString() { 15 | return new StringJoiner(", ", NameBean.class.getSimpleName() + "[", "]") 16 | .add("name='" + name + "'") 17 | .toString(); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/bean/collections/SetterInjection.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.bean.collections; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | 6 | import java.util.List; 7 | import java.util.Map; 8 | import java.util.Set; 9 | import java.util.StringJoiner; 10 | 11 | @Component 12 | public class SetterInjection { 13 | 14 | private List names; 15 | private Set phones; 16 | private Map phoneNameMap; 17 | 18 | @Autowired 19 | public void setNames(List names) { 20 | this.names = names; 21 | } 22 | 23 | @Autowired 24 | public void setPhones(Set phones) { 25 | this.phones = phones; 26 | } 27 | 28 | @Autowired 29 | public void setPhoneNameMap(Map phoneNameMap) { 30 | this.phoneNameMap = phoneNameMap; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return new StringJoiner(", ", SetterInjection.class.getSimpleName() + "[", "]") 36 | .add("names=" + names) 37 | .add("phones=" + phones) 38 | .add("phoneNameMap=" + phoneNameMap) 39 | .toString(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/bean/collections/SortedNamesCollection.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.bean.collections; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Component; 5 | 6 | import java.util.List; 7 | import java.util.StringJoiner; 8 | 9 | @Component 10 | public class SortedNamesCollection { 11 | private List names; 12 | 13 | @Autowired 14 | public void setNames(List names) { 15 | this.names = names; 16 | } 17 | 18 | @Override 19 | public String toString() { 20 | return new StringJoiner(", ", SortedNamesCollection.class.getSimpleName() + "[", "]") 21 | .add("names=" + names) 22 | .toString(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/bean/collections/package-info.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.bean.collections; 2 | /** 3 | * Spring IoC configuration for collections 4 | */ -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/bean/scope/AutowireBeanConfig.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.bean.scope; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.ComponentScan; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.context.annotation.Scope; 7 | 8 | import java.util.Random; 9 | import java.util.UUID; 10 | 11 | @Configuration 12 | @ComponentScan("basic.ioc.autowire") 13 | public class AutowireBeanConfig { 14 | 15 | @Bean 16 | @Scope("prototype") 17 | public Item item(){ 18 | return new Item( 19 | new Random().nextLong(), 20 | UUID.randomUUID().toString() 21 | ); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/bean/scope/Item.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.bean.scope; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public class Item { 6 | private Long id; 7 | private String name; 8 | 9 | public Item() { 10 | } 11 | 12 | public Item(Long id, String name) { 13 | this.id = id; 14 | this.name = name; 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return new StringJoiner(", ", Item.class.getSimpleName() + "[", "]") 36 | .add("id=" + id) 37 | .add("name='" + name + "'") 38 | .toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/bean/scope/Store.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.bean.scope; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Scope; 5 | import org.springframework.stereotype.Component; 6 | 7 | import java.util.StringJoiner; 8 | import java.util.UUID; 9 | 10 | @Component 11 | @Scope("prototype") 12 | public class Store { 13 | private String id; 14 | 15 | /* Field injection is not recommended. Instead use, Constructor 16 | or Setter injection 17 | */ 18 | @Autowired 19 | private Item item; 20 | 21 | public Store() { 22 | id = UUID.randomUUID().toString(); 23 | } 24 | 25 | public Store(String id, Item item) { 26 | this.id = id; 27 | this.item = item; 28 | } 29 | 30 | public String getId() { 31 | return id; 32 | } 33 | 34 | public void setId(String id) { 35 | this.id = id; 36 | } 37 | 38 | public Item getItem() { 39 | return item; 40 | } 41 | 42 | public void setItem(Item item) { 43 | this.item = item; 44 | } 45 | 46 | @Override 47 | public String toString() { 48 | return new StringJoiner(", ", Store.class.getSimpleName() + "[", "]") 49 | .add("id=" + id) 50 | .add("item=" + item) 51 | .toString(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/bean/scope/TestAutowireExample.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.bean.scope; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 | 6 | public class TestAutowireExample { 7 | public static void main(String[] args) { 8 | ApplicationContext context = new AnnotationConfigApplicationContext(AutowireBeanConfig.class); 9 | 10 | System.out.println(context.getBean(Store.class)); 11 | System.out.println(context.getBean(Store.class)); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/beanfactory/AutowireBeanConfig.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.beanfactory; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.ComponentScan; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.context.annotation.Scope; 7 | 8 | import java.util.Random; 9 | import java.util.UUID; 10 | 11 | @Configuration 12 | @ComponentScan("basic.ioc.autowire") 13 | public class AutowireBeanConfig { 14 | 15 | @Bean 16 | @Scope("prototype") 17 | public Item item(){ 18 | return new Item( 19 | new Random().nextLong(), 20 | UUID.randomUUID().toString() 21 | ); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/beanfactory/Item.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.beanfactory; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public class Item { 6 | private Long id; 7 | private String name; 8 | 9 | public Item() { 10 | } 11 | 12 | public Item(Long id, String name) { 13 | this.id = id; 14 | this.name = name; 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return new StringJoiner(", ", Item.class.getSimpleName() + "[", "]") 36 | .add("id=" + id) 37 | .add("name='" + name + "'") 38 | .toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/beanfactory/Store.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.beanfactory; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.context.annotation.Scope; 5 | import org.springframework.stereotype.Component; 6 | 7 | import java.util.StringJoiner; 8 | import java.util.UUID; 9 | 10 | @Component 11 | @Scope("prototype") 12 | public class Store { 13 | private String id; 14 | 15 | /* Field injection is not recommended. Instead use, Constructor 16 | or Setter injection 17 | */ 18 | @Autowired 19 | private Item item; 20 | 21 | public Store() { 22 | id = UUID.randomUUID().toString(); 23 | } 24 | 25 | public Store(String id, Item item) { 26 | this.id = id; 27 | this.item = item; 28 | } 29 | 30 | public String getId() { 31 | return id; 32 | } 33 | 34 | public void setId(String id) { 35 | this.id = id; 36 | } 37 | 38 | public Item getItem() { 39 | return item; 40 | } 41 | 42 | public void setItem(Item item) { 43 | this.item = item; 44 | } 45 | 46 | @Override 47 | public String toString() { 48 | return new StringJoiner(", ", Store.class.getSimpleName() + "[", "]") 49 | .add("id=" + id) 50 | .add("item=" + item) 51 | .toString(); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/beanfactory/TestAutowireExample.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.beanfactory; 2 | 3 | import org.springframework.beans.factory.BeanFactory; 4 | import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition; 5 | import org.springframework.beans.factory.xml.XmlBeanFactory; 6 | import org.springframework.context.ApplicationContext; 7 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 8 | import org.springframework.core.io.Resource; 9 | 10 | public class TestAutowireExample { 11 | public static void main(String[] args) { 12 | BeanFactory beanFactory = new AnnotationConfigApplicationContext(AutowireBeanConfig.class); 13 | 14 | System.out.println(beanFactory.getBean(Store.class)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/config/inheritance/Author.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.config.inheritance; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public abstract class Author { 6 | 7 | private String name; 8 | private Integer age; 9 | 10 | public String getName() { 11 | return name; 12 | } 13 | 14 | public void setName(String name) { 15 | this.name = name; 16 | } 17 | 18 | public Integer getAge() { 19 | return age; 20 | } 21 | 22 | public void setAge(Integer age) { 23 | this.age = age; 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | return new StringJoiner(", ", Author.class.getSimpleName() + "[", "]") 29 | .add("name='" + name + "'") 30 | .add("age=" + age) 31 | .toString(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/config/inheritance/BookAuthor.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.config.inheritance; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public class BookAuthor extends Author { 6 | 7 | private String bookName; 8 | 9 | public String getBookName() { 10 | return bookName; 11 | } 12 | 13 | public void setBookName(String bookName) { 14 | this.bookName = bookName; 15 | } 16 | 17 | @Override 18 | public String toString() { 19 | return new StringJoiner(", ", BookAuthor.class.getSimpleName() + "[", "]") 20 | .add(super.toString()) 21 | .toString(); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/constructor/annotation/BasicIocConfig.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.constructor.annotation; 2 | 3 | import org.springframework.beans.factory.config.ConfigurableBeanFactory; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.context.annotation.Scope; 7 | 8 | import java.util.UUID; 9 | 10 | @Configuration 11 | public class BasicIocConfig { 12 | 13 | @Bean 14 | @Scope("prototype") 15 | public Item item() { 16 | return new Item(); 17 | } 18 | 19 | @Bean 20 | @Scope("prototype") 21 | public Store store() { 22 | return new Store( 23 | UUID.randomUUID().toString(), this.item() 24 | ); 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/constructor/annotation/Item.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.constructor.annotation; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public class Item { 6 | private Long id; 7 | private String name; 8 | 9 | public Item() { 10 | } 11 | 12 | public Item(Long id, String name) { 13 | this.id = id; 14 | this.name = name; 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return new StringJoiner(", ", Item.class.getSimpleName() + "[", "]") 36 | .add("id=" + id) 37 | .add("name='" + name + "'") 38 | .toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/constructor/annotation/Store.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.constructor.annotation; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public class Store { 6 | private String id; 7 | private Item item; 8 | 9 | public Store() { 10 | } 11 | 12 | public Store(String id, Item item) { 13 | this.id = id; 14 | this.item = item; 15 | } 16 | 17 | public String getId() { 18 | return id; 19 | } 20 | 21 | public void setId(String id) { 22 | this.id = id; 23 | } 24 | 25 | public Item getItem() { 26 | return item; 27 | } 28 | 29 | public void setItem(Item item) { 30 | this.item = item; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return new StringJoiner(", ", Store.class.getSimpleName() + "[", "]") 36 | .add("id=" + id) 37 | .add("item=" + item) 38 | .toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/constructor/annotation/TestAnnotationConstructorInjection.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.constructor.annotation; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 | 6 | public class TestAnnotationConstructorInjection { 7 | public static void main(String[] args) { 8 | ApplicationContext context = new AnnotationConfigApplicationContext(BasicIocConfig.class); 9 | Store store = context.getBean(Store.class); 10 | System.out.println(store); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/constructor/xml/Item.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.constructor.xml; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public class Item { 6 | private Long id; 7 | private String name; 8 | 9 | public Item() { 10 | } 11 | 12 | public Item(Long id, String name) { 13 | this.id = id; 14 | this.name = name; 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return new StringJoiner(", ", Item.class.getSimpleName() + "[", "]") 36 | .add("id=" + id) 37 | .add("name='" + name + "'") 38 | .toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/constructor/xml/Store.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.constructor.xml; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public class Store { 6 | private String id; 7 | private Item item; 8 | 9 | public Store() { 10 | } 11 | 12 | public Store(String id, Item item) { 13 | this.id = id; 14 | this.item = item; 15 | } 16 | 17 | public String getId() { 18 | return id; 19 | } 20 | 21 | public void setId(String id) { 22 | this.id = id; 23 | } 24 | 25 | public Item getItem() { 26 | return item; 27 | } 28 | 29 | public void setItem(Item item) { 30 | this.item = item; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return new StringJoiner(", ", Store.class.getSimpleName() + "[", "]") 36 | .add("id=" + id) 37 | .add("item=" + item) 38 | .toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/constructor/xml/TestXMLConstructorInjection.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.constructor.xml; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.support.ClassPathXmlApplicationContext; 5 | 6 | public class TestXMLConstructorInjection { 7 | public static void main(String[] args) { 8 | ApplicationContext context = new ClassPathXmlApplicationContext("basic.ioc.constructor.bean-config.xml"); 9 | System.out.println(context.getBean(Store.class)); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/setter/annotation/BasicIocSetterConfig.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.setter.annotation; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.Configuration; 5 | import org.springframework.context.annotation.Scope; 6 | 7 | import java.util.UUID; 8 | 9 | @Configuration 10 | public class BasicIocSetterConfig { 11 | 12 | @Bean 13 | @Scope("prototype") 14 | public Item item() { 15 | return new Item(); 16 | } 17 | 18 | @Bean 19 | @Scope("prototype") 20 | public Store store() { 21 | Store storeObj = new Store(); 22 | storeObj.setId(UUID.randomUUID().toString()); 23 | storeObj.setItem(this.item()); 24 | return storeObj; 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/setter/annotation/Item.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.setter.annotation; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public class Item { 6 | private Long id; 7 | private String name; 8 | 9 | public Item() { 10 | } 11 | 12 | public Item(Long id, String name) { 13 | this.id = id; 14 | this.name = name; 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return new StringJoiner(", ", Item.class.getSimpleName() + "[", "]") 36 | .add("id=" + id) 37 | .add("name='" + name + "'") 38 | .toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/setter/annotation/Store.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.setter.annotation; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public class Store { 6 | private String id; 7 | private Item item; 8 | 9 | public Store() { 10 | } 11 | 12 | public Store(String id, Item item) { 13 | this.id = id; 14 | this.item = item; 15 | } 16 | 17 | public String getId() { 18 | return id; 19 | } 20 | 21 | public void setId(String id) { 22 | this.id = id; 23 | } 24 | 25 | public Item getItem() { 26 | return item; 27 | } 28 | 29 | public void setItem(Item item) { 30 | this.item = item; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return new StringJoiner(", ", Store.class.getSimpleName() + "[", "]") 36 | .add("id=" + id) 37 | .add("item=" + item) 38 | .toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/setter/annotation/TestAnnotationSetterInjection.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.setter.annotation; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 | 6 | public class TestAnnotationSetterInjection { 7 | public static void main(String[] args) { 8 | ApplicationContext context 9 | = new AnnotationConfigApplicationContext(BasicIocSetterConfig.class); 10 | Store store = context.getBean(Store.class); 11 | System.out.println(store); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/setter/xml/Item.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.setter.xml; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public class Item { 6 | private Long id; 7 | private String name; 8 | 9 | public Item() { 10 | } 11 | 12 | public Item(Long id, String name) { 13 | this.id = id; 14 | this.name = name; 15 | } 16 | 17 | public Long getId() { 18 | return id; 19 | } 20 | 21 | public void setId(Long id) { 22 | this.id = id; 23 | } 24 | 25 | public String getName() { 26 | return name; 27 | } 28 | 29 | public void setName(String name) { 30 | this.name = name; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return new StringJoiner(", ", Item.class.getSimpleName() + "[", "]") 36 | .add("id=" + id) 37 | .add("name='" + name + "'") 38 | .toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/setter/xml/Store.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.setter.xml; 2 | 3 | import java.util.StringJoiner; 4 | 5 | public class Store { 6 | private String id; 7 | private Item item; 8 | 9 | public Store() { 10 | } 11 | 12 | public Store(String id, Item item) { 13 | this.id = id; 14 | this.item = item; 15 | } 16 | 17 | public String getId() { 18 | return id; 19 | } 20 | 21 | public void setId(String id) { 22 | this.id = id; 23 | } 24 | 25 | public Item getItem() { 26 | return item; 27 | } 28 | 29 | public void setItem(Item item) { 30 | this.item = item; 31 | } 32 | 33 | @Override 34 | public String toString() { 35 | return new StringJoiner(", ", Store.class.getSimpleName() + "[", "]") 36 | .add("id=" + id) 37 | .add("item=" + item) 38 | .toString(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/setter/xml/TestXmlSetterInjection.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.setter.xml; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.support.ClassPathXmlApplicationContext; 5 | 6 | public class TestXmlSetterInjection { 7 | public static void main(String[] args) { 8 | ApplicationContext context 9 | = new ClassPathXmlApplicationContext("basic.ioc.setter.bean-config.xml"); 10 | Store store = context.getBean(Store.class); 11 | System.out.println(store); 12 | } 13 | } -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/wiring/ConfigWiring.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.wiring; 2 | 3 | import org.springframework.context.annotation.ComponentScan; 4 | import org.springframework.context.annotation.Configuration; 5 | 6 | @ComponentScan("basic.ioc.wiring") 7 | @Configuration 8 | public class ConfigWiring { 9 | //extra configs goes here 10 | } 11 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/wiring/FileReader.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.wiring; 2 | 3 | public abstract class FileReader { 4 | public void print() { 5 | System.out.println("Inside FileReader"); 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/wiring/PdfFileReader.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.wiring; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | @Component 6 | public class PdfFileReader extends FileReader { 7 | 8 | @Override 9 | public void print() { 10 | System.out.println("Inside PdfFileReader"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/wiring/WordFileReader.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.wiring; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | @Component 6 | public class WordFileReader extends FileReader { 7 | 8 | @Override 9 | public void print() { 10 | System.out.println("Inside WordFileReader"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/wiringfinetune/ConfigWiring.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.wiringfinetune; 2 | 3 | import org.springframework.beans.factory.annotation.Qualifier; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.ComponentScan; 6 | import org.springframework.context.annotation.Configuration; 7 | import org.springframework.context.annotation.Primary; 8 | 9 | @ComponentScan("basic.ioc.wiringfinetune") 10 | @Configuration 11 | public class ConfigWiring { 12 | 13 | //bean name = insurance1 14 | @Bean 15 | @Primary 16 | public Insurance insurance1(){ 17 | Insurance healthInsurance = new Insurance(); 18 | healthInsurance.setType("Health Insurance"); 19 | return healthInsurance; 20 | } 21 | 22 | //bean name = insurance2 23 | @Bean 24 | public Insurance insurance2(){ 25 | Insurance termInsurance = new Insurance(); 26 | termInsurance.setType("Term Insurance"); 27 | return termInsurance; 28 | } 29 | 30 | //bean name = insurance3 31 | @Bean 32 | @Qualifier("generalInsurance") 33 | public Insurance insurance3() { 34 | Insurance generalInsurance = new Insurance(); 35 | generalInsurance.setType("General Insurance"); 36 | return generalInsurance; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/wiringfinetune/FileReader.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.wiringfinetune; 2 | 3 | public interface FileReader { 4 | void print(); 5 | } 6 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/wiringfinetune/Insurance.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.wiringfinetune; 2 | 3 | public class Insurance { 4 | private String type; 5 | 6 | public String getType() { 7 | return type; 8 | } 9 | 10 | public void setType(String type) { 11 | this.type = type; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/wiringfinetune/PdfFileReader.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.wiringfinetune; 2 | 3 | import org.springframework.context.annotation.Primary; 4 | import org.springframework.stereotype.Component; 5 | 6 | @Primary 7 | @Component 8 | public class PdfFileReader implements FileReader { 9 | 10 | @Override 11 | public void print() { 12 | System.out.println("Inside PdfFileReader"); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/java/basic/ioc/wiringfinetune/WordFileReader.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.wiringfinetune; 2 | 3 | import org.springframework.stereotype.Component; 4 | 5 | @Component 6 | public class WordFileReader implements FileReader { 7 | 8 | @Override 9 | public void print() { 10 | System.out.println("Inside WordFileReader"); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/resources/basic.ioc.bean-config.inheritance.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 9 | 10 | 11 | 12 | 13 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/resources/basic.ioc.constructor.bean-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /spring-core-ioc/src/main/resources/basic.ioc.setter.bean-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /spring-core-ioc/src/test/java/basic/ioc/bean/collections/TestCollectionsIoC.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.bean.collections; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.context.ApplicationContext; 5 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 6 | 7 | public class TestCollectionsIoC { 8 | 9 | @Test 10 | public void testCollectionsMapping(){ 11 | ApplicationContext aContext 12 | = new AnnotationConfigApplicationContext(CollectionsConfig.class); 13 | 14 | ConstructorInjection cInjection = aContext.getBean(ConstructorInjection.class); 15 | System.out.println(cInjection); 16 | 17 | FieldInjection pInjection = aContext.getBean(FieldInjection.class); 18 | System.out.println(pInjection); 19 | 20 | SetterInjection setterInjection = aContext.getBean(SetterInjection.class); 21 | System.out.println(setterInjection); 22 | } 23 | 24 | @Test 25 | public void testBeanReferenceCollections(){ 26 | ApplicationContext aContext 27 | = new AnnotationConfigApplicationContext(CollectionsConfig.class); 28 | 29 | BeanReferenceCollections beanReferCollections = aContext.getBean(BeanReferenceCollections.class); 30 | System.out.println(beanReferCollections); 31 | } 32 | 33 | @Test 34 | public void testSortedCollection(){ 35 | ApplicationContext aContext 36 | = new AnnotationConfigApplicationContext(CollectionsConfig.class); 37 | 38 | SortedNamesCollection collection = aContext.getBean(SortedNamesCollection.class); 39 | System.out.println(collection); 40 | } 41 | } 42 | 43 | -------------------------------------------------------------------------------- /spring-core-ioc/src/test/java/basic/ioc/config/inheritance/TestConfigInheritance.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.config.inheritance; 2 | 3 | import org.junit.jupiter.api.Test; 4 | import org.springframework.context.ApplicationContext; 5 | import org.springframework.context.support.ClassPathXmlApplicationContext; 6 | 7 | public class TestConfigInheritance { 8 | 9 | @Test 10 | public void testBeanConfigInheritance(){ 11 | ApplicationContext aContext 12 | = new ClassPathXmlApplicationContext("basic.ioc.bean-config.inheritance.xml"); 13 | 14 | Author author = aContext.getBean(Author.class); 15 | BookAuthor bookAuthor = aContext.getBean(BookAuthor.class); 16 | System.out.println(author); 17 | System.out.println(bookAuthor); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /spring-core-ioc/src/test/java/basic/ioc/wiring/AutowiredTest.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.wiring; 2 | 3 | import org.junit.Assert; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.beans.factory.annotation.Qualifier; 7 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 8 | 9 | @SpringJUnitConfig(ConfigWiring.class) 10 | public class AutowiredTest { 11 | 12 | //Inject by Type 13 | @Autowired 14 | private PdfFileReader pdfFileReader; 15 | 16 | @Test 17 | public void injectByType() { 18 | Assert.assertNotNull(pdfFileReader); 19 | Assert.assertEquals(pdfFileReader.getClass(), PdfFileReader.class); 20 | } 21 | 22 | //Inject by @Qualifier 23 | @Autowired 24 | @Qualifier("wordFileReader") 25 | private FileReader fileReader; 26 | 27 | @Test 28 | public void injectByQualifier() { 29 | Assert.assertNotNull(fileReader); 30 | Assert.assertEquals(fileReader.getClass(), WordFileReader.class); 31 | } 32 | 33 | //Inject by Field name 34 | @Autowired 35 | private FileReader wordFileReader; 36 | 37 | @Test 38 | public void injectByFieldName() { 39 | Assert.assertNotNull(wordFileReader); 40 | Assert.assertEquals(wordFileReader.getClass(), WordFileReader.class); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /spring-core-ioc/src/test/java/basic/ioc/wiring/InjectTest.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.wiring; 2 | 3 | import org.junit.Assert; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Qualifier; 6 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 7 | 8 | import javax.inject.Inject; 9 | import javax.inject.Named; 10 | 11 | @SpringJUnitConfig(ConfigWiring.class) 12 | public class InjectTest { 13 | 14 | //Inject by Type 15 | @Inject 16 | private PdfFileReader fileReader; 17 | 18 | @Test 19 | public void injectByType() { 20 | Assert.assertNotNull(fileReader); 21 | Assert.assertEquals(fileReader.getClass(), PdfFileReader.class); 22 | } 23 | 24 | //Inject by @Qualifier 25 | @Inject 26 | @Qualifier("wordFileReader") 27 | private FileReader fileReader3; 28 | 29 | @Test 30 | public void injectByQualifier() { 31 | Assert.assertNotNull(fileReader3); 32 | Assert.assertEquals(fileReader3.getClass(), WordFileReader.class); 33 | } 34 | 35 | //Inject by name with @Named annotation 36 | @Inject 37 | @Named("pdfFileReader") 38 | private FileReader fileReader2; 39 | 40 | @Test 41 | public void injectByName() { 42 | Assert.assertNotNull(fileReader2); 43 | Assert.assertEquals(fileReader2.getClass(), PdfFileReader.class); 44 | } 45 | 46 | //Inject by field Name 47 | @Inject 48 | private FileReader wordFileReader; 49 | 50 | @Test 51 | public void injectByName2() { 52 | Assert.assertNotNull(wordFileReader); 53 | Assert.assertEquals(wordFileReader.getClass(), WordFileReader.class); 54 | } 55 | 56 | //Check precedence - Don't do this 57 | //TODO - Uncomment the below line and check what happens 58 | /*@Inject 59 | @Qualifier("pdfFileReader") 60 | @Named("wordFileReader") 61 | private FileReader pdfFileReader; 62 | 63 | @Test 64 | public void checkPrecedence() { 65 | Assert.assertNotNull(pdfFileReader); 66 | Assert.assertEquals(pdfFileReader.getClass(), PdfFileReader.class); 67 | }*/ 68 | } 69 | -------------------------------------------------------------------------------- /spring-core-ioc/src/test/java/basic/ioc/wiring/ResourceTest.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.wiring; 2 | 3 | import org.junit.Assert; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Qualifier; 6 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 7 | 8 | import javax.annotation.Resource; 9 | 10 | @SpringJUnitConfig(ConfigWiring.class) 11 | public class ResourceTest { 12 | 13 | //Uncomment and show the exception that gets reproduced 14 | //@Resource 15 | //private FileReader fReader; 16 | 17 | //Resolve by property name 18 | @Resource 19 | private FileReader pdfFileReader; 20 | 21 | @Test 22 | public void resolveByPropertyName() { 23 | Assert.assertNotNull(pdfFileReader); 24 | Assert.assertEquals(pdfFileReader.getClass(), PdfFileReader.class); 25 | } 26 | 27 | //Resolve by explicit name 28 | @Resource(name = "wordFileReader") 29 | private FileReader reader; 30 | 31 | @Test 32 | public void resolveByExplicitName() { 33 | Assert.assertNotNull(reader); 34 | Assert.assertEquals(reader.getClass(), WordFileReader.class); 35 | } 36 | 37 | //Resolve by type auto detection 38 | @Resource 39 | private WordFileReader fileReader; 40 | 41 | @Test 42 | public void resolveByAutoType(){ 43 | Assert.assertNotNull(fileReader); 44 | Assert.assertEquals(fileReader.getClass(), WordFileReader.class); 45 | } 46 | 47 | //Resolve by explicit Type 48 | @Resource(type = PdfFileReader.class) 49 | private FileReader fileReader2; 50 | 51 | @Test 52 | public void resolveByExplicitType() { 53 | Assert.assertNotNull(fileReader2); 54 | Assert.assertEquals(fileReader2.getClass(), PdfFileReader.class); 55 | } 56 | 57 | //Resolve by Qualifier 58 | @Qualifier("pdfFileReader") 59 | @Resource 60 | private FileReader myFileReader; 61 | 62 | @Test 63 | public void resolveByQualifier(){ 64 | Assert.assertNotNull(myFileReader); 65 | Assert.assertEquals(myFileReader.getClass(), PdfFileReader.class); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /spring-core-ioc/src/test/java/basic/ioc/wiringfinetune/FineTuneAutowiring.java: -------------------------------------------------------------------------------- 1 | package basic.ioc.wiringfinetune; 2 | 3 | import org.junit.Assert; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.beans.factory.annotation.Qualifier; 7 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 8 | 9 | @SpringJUnitConfig(ConfigWiring.class) 10 | public class FineTuneAutowiring { 11 | 12 | @Autowired 13 | private FileReader fileReader; 14 | 15 | @Test 16 | public void testFileReader() { 17 | Assert.assertNotNull(fileReader); 18 | Assert.assertEquals(fileReader.getClass(), PdfFileReader.class); 19 | } 20 | 21 | @Autowired 22 | private Insurance insurance; 23 | 24 | @Test 25 | public void testInsuranceBean(){ 26 | Assert.assertNotNull(insurance); 27 | Assert.assertEquals(insurance.getType(), "Health Insurance"); 28 | } 29 | 30 | //With Qualifier 31 | @Autowired 32 | @Qualifier("wordFileReader") 33 | private FileReader fileReader2; 34 | 35 | @Test 36 | public void testWordFileReader() { 37 | Assert.assertNotNull(fileReader2); 38 | Assert.assertEquals(fileReader2.getClass(), WordFileReader.class); 39 | } 40 | 41 | @Autowired 42 | //@Qualifier("insurance3") 43 | @Qualifier("generalInsurance") 44 | private Insurance myInsurance; 45 | 46 | @Test 47 | public void testInsuranceWithQualifier(){ 48 | Assert.assertNotNull(myInsurance); 49 | Assert.assertEquals("General Insurance", myInsurance.getType()); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /spring-core-null-safety/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-core-null-safety 13 | 14 | 15 | 16 | org.springframework 17 | spring-core 18 | 19 | 20 | 21 | org.springframework 22 | spring-context 23 | 24 | 25 | 26 | 27 | org.junit.jupiter 28 | junit-jupiter-api 29 | test 30 | 31 | 32 | 33 | org.junit.jupiter 34 | junit-jupiter-engine 35 | test 36 | 37 | 38 | 39 | org.springframework 40 | spring-test 41 | test 42 | 43 | 44 | 45 | 46 | 47 | 48 | com.jstobigdata 49 | spring-tutorial-boms 50 | pom 51 | import 52 | 1.0-SNAPSHOT 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | org.apache.maven.plugins 62 | maven-surefire-plugin 63 | 3.0.0-M3 64 | 65 | 66 | 67 | -------------------------------------------------------------------------------- /spring-core-null-safety/src/main/java/com/jbd/spring/AppConfig.java: -------------------------------------------------------------------------------- 1 | package com.jbd.spring; 2 | 3 | import com.jbd.spring.fieldlevel.Person; 4 | import org.springframework.context.annotation.Bean; 5 | import org.springframework.context.annotation.ComponentScan; 6 | import org.springframework.context.annotation.Configuration; 7 | 8 | @Configuration 9 | @ComponentScan("com.jbd.spring.nullsafety") 10 | public class AppConfig { 11 | @Bean 12 | public Person person(){ 13 | return new Person(100L, null,null); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /spring-core-null-safety/src/main/java/com/jbd/spring/fieldlevel/Person.java: -------------------------------------------------------------------------------- 1 | package com.jbd.spring.fieldlevel; 2 | 3 | import org.springframework.lang.NonNull; 4 | import org.springframework.lang.Nullable; 5 | 6 | public class Person { 7 | @NonNull 8 | private Long id; 9 | @NonNull 10 | private String name; 11 | @Nullable 12 | private String email; 13 | 14 | public Person(@NonNull Long id, @NonNull String name, @Nullable String email) { 15 | this.id = id; 16 | this.name = name; 17 | this.email = email; 18 | } 19 | 20 | @NonNull 21 | public Long getId() { 22 | return id; 23 | } 24 | 25 | public Person setId(@NonNull Long id) { 26 | this.id = id; 27 | return this; 28 | } 29 | 30 | @NonNull 31 | public String getName() { 32 | return name; 33 | } 34 | 35 | public Person setName(@NonNull String name) { 36 | this.name = name; 37 | return this; 38 | } 39 | 40 | @Nullable 41 | public String getEmail() { 42 | return email; 43 | } 44 | 45 | public Person setEmail(@Nullable String email) { 46 | this.email = email; 47 | return this; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /spring-core-null-safety/src/test/java/com/jbd/spring/TestNullSafety.java: -------------------------------------------------------------------------------- 1 | package com.jbd.spring; 2 | 3 | import com.jbd.spring.fieldlevel.Person; 4 | import org.junit.jupiter.api.Test; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 7 | 8 | @SpringJUnitConfig(AppConfig.class) 9 | public class TestNullSafety { 10 | 11 | @Autowired 12 | private Person person; 13 | 14 | @Test 15 | public void testNullSafety(){ 16 | System.out.println(person.toString()); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /spring-core-order-annotation/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | spring-core-order-annotation 12 | Use of Spring's @Order annotation 13 | 14 | 15 | 16 | 17 | org.springframework 18 | spring-core 19 | 20 | 21 | 22 | org.springframework 23 | spring-context 24 | 25 | 26 | 27 | 28 | org.junit.jupiter 29 | junit-jupiter-api 30 | test 31 | 32 | 33 | 34 | org.junit.jupiter 35 | junit-jupiter-engine 36 | test 37 | 38 | 39 | 40 | org.springframework 41 | spring-test 42 | test 43 | 44 | 45 | 46 | 47 | 48 | 49 | com.jstobigdata 50 | spring-tutorial-boms 51 | pom 52 | import 53 | 1.0-SNAPSHOT 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | org.apache.maven.plugins 63 | maven-surefire-plugin 64 | 3.0.0-M3 65 | 66 | 67 | 68 | -------------------------------------------------------------------------------- /spring-core-order-annotation/src/main/java/com/jsbd/order/ApplicationConfig.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.order; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.ComponentScan; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.core.annotation.Order; 7 | 8 | import com.jsbd.order.model.NotificationChannel; 9 | 10 | @Configuration 11 | @ComponentScan("com.jsbd.order") 12 | public class ApplicationConfig { 13 | 14 | 15 | @Bean 16 | @Order(2) 17 | public NotificationChannel email(){ 18 | // System.out.println("Notification Channel - Email"); 19 | return new NotificationChannel("Email"); 20 | } 21 | 22 | @Bean 23 | @Order(4) 24 | public NotificationChannel twitter(){ 25 | // System.out.println("Notification Channel - Twitter"); 26 | return new NotificationChannel("Twitter"); 27 | } 28 | 29 | @Bean 30 | @Order(3) 31 | public NotificationChannel slack(){ 32 | // System.out.println("Notification Channel - Slack"); 33 | return new NotificationChannel("Slack"); 34 | } 35 | 36 | @Bean 37 | @Order(1) 38 | public NotificationChannel sms(){ 39 | // System.out.println("Notification Channel - Sms"); 40 | return new NotificationChannel("Sms"); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /spring-core-order-annotation/src/main/java/com/jsbd/order/model/NotificationChannel.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.order.model; 2 | 3 | public class NotificationChannel { 4 | 5 | private String name; 6 | 7 | public NotificationChannel(String channelName) { 8 | this.name = channelName; 9 | } 10 | 11 | public String getName() { 12 | return name; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /spring-core-order-annotation/src/main/java/com/jsbd/order/service/EmailNotification.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.order.service; 2 | 3 | import org.springframework.core.annotation.Order; 4 | import org.springframework.stereotype.Service; 5 | 6 | @Service 7 | @Order(3) 8 | public class EmailNotification implements NotificationHandler { 9 | 10 | public EmailNotification() { 11 | System.out.println("EmailNotification Service Created."); 12 | } 13 | 14 | @Override 15 | public void send() { 16 | System.out.println("Email Notification Handler"); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /spring-core-order-annotation/src/main/java/com/jsbd/order/service/NotificationHandler.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.order.service; 2 | 3 | public interface NotificationHandler { 4 | void send(); 5 | } -------------------------------------------------------------------------------- /spring-core-order-annotation/src/main/java/com/jsbd/order/service/SlackNotification.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.order.service; 2 | 3 | import org.springframework.core.annotation.Order; 4 | import org.springframework.stereotype.Service; 5 | 6 | @Service 7 | @Order(2) 8 | class SlackNotification implements NotificationHandler { 9 | 10 | public SlackNotification() { 11 | System.out.println("SlackNotification Service created."); 12 | } 13 | 14 | @Override 15 | public void send() { 16 | System.out.println("Slack Notification Handler"); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /spring-core-order-annotation/src/main/java/com/jsbd/order/service/SmsNotification.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.order.service; 2 | 3 | import org.springframework.core.annotation.Order; 4 | import org.springframework.stereotype.Service; 5 | 6 | @Service 7 | @Order(1) 8 | class SmsNotification implements NotificationHandler { 9 | 10 | public SmsNotification() { 11 | System.out.println("SmsNotification Service created."); 12 | } 13 | 14 | @Override 15 | public void send() { 16 | System.out.println("SMS Notification Handler"); 17 | } 18 | 19 | } -------------------------------------------------------------------------------- /spring-core-order-annotation/src/main/java/com/jsbd/order/service/TwitterNotification.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.order.service; 2 | 3 | import org.springframework.stereotype.Service; 4 | 5 | @Service 6 | // Not an Ordered Service, will be the last in sequence 7 | class TwitterNotification implements NotificationHandler { 8 | 9 | public TwitterNotification() { 10 | System.out.println("TwitterNotification Service created."); 11 | } 12 | 13 | @Override 14 | public void send() { 15 | System.out.println("Twitter Direct Message Notification Handler"); 16 | } 17 | 18 | } -------------------------------------------------------------------------------- /spring-core-order-annotation/src/test/java/com/jsbd/order/TestOrderOnCollectionInjection.java: -------------------------------------------------------------------------------- 1 | package com.jsbd.order; 2 | 3 | import java.util.List; 4 | 5 | import com.jsbd.order.model.NotificationChannel; 6 | import com.jsbd.order.service.NotificationHandler; 7 | 8 | import org.junit.jupiter.api.Test; 9 | import org.springframework.beans.factory.annotation.Autowired; 10 | import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; 11 | 12 | @SpringJUnitConfig(ApplicationConfig.class) 13 | public class TestOrderOnCollectionInjection { 14 | 15 | @Autowired 16 | private List notificationHandlers; 17 | 18 | @Autowired 19 | private List notificationChannels; 20 | 21 | @Test 22 | public void testAllChannelSendingOrder() { 23 | notificationHandlers.forEach(NotificationHandler::send); 24 | } 25 | 26 | @Test 27 | public void testNotificationChannelBeanOrder(){ 28 | notificationChannels.forEach(channel -> System.out.println(channel.getName())); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /spring-core-work-with-resources/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-core-work-with-resources 13 | Spring example to work with resources 14 | 15 | 16 | 17 | org.springframework 18 | spring-core 19 | 20 | 21 | 22 | org.springframework 23 | spring-context 24 | 25 | 26 | 27 | 28 | 29 | 30 | com.jstobigdata 31 | spring-tutorial-boms 32 | 1.0-SNAPSHOT 33 | pom 34 | import 35 | 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /spring-core-work-with-resources/src/main/java/c/jbd/spring/resources/DefaultResourceLoaderExample.java: -------------------------------------------------------------------------------- 1 | package c.jbd.spring.resources; 2 | 3 | import org.springframework.core.io.DefaultResourceLoader; 4 | import org.springframework.core.io.Resource; 5 | import org.springframework.core.io.ResourceLoader; 6 | 7 | import java.io.IOException; 8 | 9 | public class DefaultResourceLoaderExample { 10 | public static void main(String[] args) throws IOException { 11 | ResourceLoader resourceLoader = new DefaultResourceLoader(); 12 | Resource r = resourceLoader.getResource("sample.md"); 13 | ReaderUtil.readAndPrint(r.getInputStream()); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /spring-core-work-with-resources/src/main/java/c/jbd/spring/resources/ReaderUtil.java: -------------------------------------------------------------------------------- 1 | package c.jbd.spring.resources; 2 | 3 | import java.io.IOException; 4 | import java.io.InputStream; 5 | 6 | public class ReaderUtil { 7 | public static void readAndPrint(InputStream stream) throws IOException { 8 | int content = stream.read(); 9 | System.out.println("\n============ start ==========="); 10 | while (content != -1) { 11 | System.out.print((char) content); 12 | content = stream.read(); 13 | } 14 | System.out.println("\n============ End ============"); 15 | stream.close(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /spring-core-work-with-resources/src/main/java/c/jbd/spring/resources/ResourcesExample.java: -------------------------------------------------------------------------------- 1 | package c.jbd.spring.resources; 2 | 3 | import org.springframework.core.io.ClassPathResource; 4 | import org.springframework.core.io.Resource; 5 | import org.springframework.core.io.UrlResource; 6 | 7 | import java.io.IOException; 8 | 9 | public class ResourcesExample { 10 | public static void main(String[] args) throws IOException { 11 | Resource urlResource 12 | = new UrlResource("https://google.com"); 13 | ReaderUtil.readAndPrint(urlResource.getInputStream()); 14 | 15 | Resource classPathResource = new ClassPathResource("sample.md"); 16 | ReaderUtil.readAndPrint(classPathResource.getInputStream()); 17 | 18 | //FileSystemResource fileResource = new FileSystemResource("PathToFile/filename"); 19 | //readAndPrint(fileResource.getInputStream()); 20 | } 21 | 22 | 23 | } 24 | -------------------------------------------------------------------------------- /spring-core-work-with-resources/src/main/java/c/jbd/spring/resources/loader/JbdResourceLoader.java: -------------------------------------------------------------------------------- 1 | package c.jbd.spring.resources.loader; 2 | 3 | import c.jbd.spring.resources.ReaderUtil; 4 | import org.springframework.context.ResourceLoaderAware; 5 | import org.springframework.core.io.Resource; 6 | import org.springframework.core.io.ResourceLoader; 7 | import org.springframework.stereotype.Component; 8 | 9 | import java.io.IOException; 10 | 11 | @Component("jbdResourceLoader") 12 | public class JbdResourceLoader implements ResourceLoaderAware { 13 | private ResourceLoader resourceLoader; 14 | 15 | public void setResourceLoader(ResourceLoader resourceLoader) { 16 | this.resourceLoader = resourceLoader; 17 | } 18 | 19 | public void loadResource(String resourcePath) throws IOException { 20 | Resource resource = resourceLoader.getResource(resourcePath); 21 | ReaderUtil.readAndPrint(resource.getInputStream()); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-core-work-with-resources/src/main/java/c/jbd/spring/resources/loader/ResourceLoaderDemo.java: -------------------------------------------------------------------------------- 1 | package c.jbd.spring.resources.loader; 2 | 3 | import org.springframework.context.ApplicationContext; 4 | import org.springframework.context.annotation.AnnotationConfigApplicationContext; 5 | 6 | import java.io.IOException; 7 | 8 | public class ResourceLoaderDemo { 9 | public static void main(String[] args) throws IOException { 10 | 11 | ApplicationContext ctx = new AnnotationConfigApplicationContext(JbdResourceLoader.class); 12 | JbdResourceLoader loader = (JbdResourceLoader) ctx.getBean("jbdResourceLoader"); 13 | loader.loadResource("classpath:sample.md"); 14 | System.out.println("*** Resource loader using classpath ***"); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /spring-core-work-with-resources/src/main/resources/ex-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /spring-core-work-with-resources/src/main/resources/sample.md: -------------------------------------------------------------------------------- 1 | ## This is a Sample Markdown file -------------------------------------------------------------------------------- /spring-mvc-00-getting-started/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | 12 | spring-mvc-00-getting-started 13 | war 14 | 15 | 16 | 17 | false 18 | 19 | 20 | 21 | 22 | 23 | org.springframework 24 | spring-webmvc 25 | 26 | 27 | 28 | 29 | javax.servlet 30 | javax.servlet-api 31 | provided 32 | 33 | 34 | 35 | javax.servlet 36 | jstl 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | com.jstobigdata 45 | spring-tutorial-boms 46 | 1.0-SNAPSHOT 47 | import 48 | pom 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /spring-mvc-00-getting-started/src/main/java/c/jbd/saop/cjp/sm/gettingstarted/config/ApplicationConfig.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.cjp.sm.gettingstarted.config; 2 | 3 | import org.springframework.context.annotation.Bean; 4 | import org.springframework.context.annotation.ComponentScan; 5 | import org.springframework.context.annotation.Configuration; 6 | import org.springframework.web.servlet.config.annotation.EnableWebMvc; 7 | import org.springframework.web.servlet.view.InternalResourceViewResolver; 8 | import org.springframework.web.servlet.view.JstlView; 9 | 10 | /** 11 | * Getting started AppConfig. 12 | */ 13 | @Configuration 14 | @EnableWebMvc 15 | @ComponentScan("c.jbd.sm.gettingstarted") 16 | public class ApplicationConfig { 17 | @Bean 18 | public InternalResourceViewResolver viewResolver(){ 19 | InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); 20 | viewResolver.setViewClass(JstlView.class); 21 | viewResolver.setPrefix("/WEB-INF/views/"); 22 | viewResolver.setSuffix(".jsp"); 23 | return viewResolver; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /spring-mvc-00-getting-started/src/main/java/c/jbd/saop/cjp/sm/gettingstarted/config/SpringMvcInitializer.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.cjp.sm.gettingstarted.config; 2 | 3 | import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 4 | 5 | /** 6 | * 7 | */ 8 | public class SpringMvcInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 9 | @Override 10 | protected Class[] getRootConfigClasses() { 11 | return new Class[0]; 12 | } 13 | 14 | @Override 15 | protected Class[] getServletConfigClasses() { 16 | return new Class[]{ApplicationConfig.class}; 17 | } 18 | 19 | @Override 20 | protected String[] getServletMappings() { 21 | return new String[]{"/"}; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /spring-mvc-00-getting-started/src/main/java/c/jbd/saop/cjp/sm/gettingstarted/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.cjp.sm.gettingstarted.controller; 2 | 3 | import c.jbd.saop.cjp.sm.gettingstarted.model.Message; 4 | import org.springframework.stereotype.Controller; 5 | import org.springframework.ui.Model; 6 | import org.springframework.web.bind.annotation.GetMapping; 7 | 8 | @Controller 9 | public class HelloController { 10 | 11 | private final static String HELLO_VIEW = "hello-view"; 12 | 13 | @GetMapping("/") 14 | public String homePage(Model model){ 15 | Message message = new Message() 16 | .setTitle("Getting started with Spring MVC ") 17 | .setSubTitle("Java based configuration - without web.xml") 18 | .setBody("Brought to you by - Jstobigdata.com"); 19 | model.addAttribute("message", message); 20 | return HELLO_VIEW; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /spring-mvc-00-getting-started/src/main/java/c/jbd/saop/cjp/sm/gettingstarted/model/Message.java: -------------------------------------------------------------------------------- 1 | package c.jbd.saop.cjp.sm.gettingstarted.model; 2 | 3 | /** 4 | * @author Jstobigdata.com 5 | * The Message model used by home controller. 6 | */ 7 | public class Message { 8 | 9 | private String title; 10 | private String subTitle; 11 | private String body; 12 | 13 | public Message() { 14 | } 15 | 16 | public String getTitle() { 17 | return title; 18 | } 19 | 20 | public Message setTitle(String title) { 21 | this.title = title; 22 | return this; 23 | } 24 | 25 | public String getSubTitle() { 26 | return subTitle; 27 | } 28 | 29 | public Message setSubTitle(String subTitle) { 30 | this.subTitle = subTitle; 31 | return this; 32 | } 33 | 34 | public String getBody() { 35 | return body; 36 | } 37 | 38 | public Message setBody(String body) { 39 | this.body = body; 40 | return this; 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /spring-mvc-00-getting-started/src/main/webapp/WEB-INF/views/hello-view.jsp: -------------------------------------------------------------------------------- 1 | <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 2 | 3 | 4 | 5 | 6 | Spring MVC Tutorials | Jstobigdata.com 7 | 8 | 9 |

${message.title}

10 |

${message.subTitle}

11 |

${message.body}

12 | 13 | -------------------------------------------------------------------------------- /spring-tutorial-boms/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | spring-framework-tutorial-parent 7 | com.jstobigdata 8 | 1.0-SNAPSHOT 9 | 10 | 4.0.0 11 | pom 12 | spring-tutorial-boms 13 | 14 | Spring Framework Tutorial Bill of Materials 15 | Bill of materials for entire Spring-Framework Tutorials 16 | 17 | 18 | JstoBigdata 19 | https://jstobigdata.com 20 | 21 | 22 | 23 | 24 | 11 25 | 11 26 | 11 27 | 28 | 29 | 2.2.2.RELEASE 30 | 31 | 32 | 33 | 34 | 35 | org.springframework.boot 36 | spring-boot-dependencies 37 | ${spring.boot.version} 38 | pom 39 | import 40 | 41 | 42 | 43 | 44 | --------------------------------------------------------------------------------