{"id":75398,"date":"2018-04-02T22:00:58","date_gmt":"2018-04-02T19:00:58","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/?p=75398"},"modified":"2023-12-11T10:32:31","modified_gmt":"2023-12-11T08:32:31","slug":"spring-with-maven-tutorial","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html","title":{"rendered":"Spring Framework with Maven Tutorial"},"content":{"rendered":"<p>In this post, we shall demonstrate how to use Maven dependencies for Spring framework for very specific use-cases. The latest versions of all the libraries we use can be found on the <a href=\"http:\/\/search.maven.org\/#search%7Cga%7C1%7Cg%3A%22org.springframework%22\" target=\"_blank\" rel=\"noopener\">Maven Central<\/a>.<\/p>\n<h2 class=\"wp-block-heading\">1. Introduction<\/h2>\n<p>Understanding how Maven dependencies work and how they are managed, is important in a project for an effective build cycle and the clear concepts about what versions match between various libraries we use in our project. This is due to the reason that we often repeat the set of dependencies in multiple projects. When we don\u2019t understand why we\u2019re using a specific library version, we are ought to do mistakes. Let\u2019s understand the relationship between different Spring Framework dependencies.<\/p>\n<h2 class=\"wp-block-heading\">2. Project Setup<\/h2>\n<p>We will be using one of the many Maven archetypes to create a sample project for our example. To create the project execute the following command in a directory that you will use as workspace:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Creating Sample Project<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:bash\">mvn archetype:generate -DgroupId=com.javacodegeeks.example -DartifactId=JCG-SpringMaven-Example -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false\n<\/pre>\n<p>If you are running maven for the first time, it will take a few seconds to accomplish the generate command because maven has to download all the required plugins and artifacts in order to make the generation task.<\/p>\n<div class=\"wp-block-image wp-image-75436\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"860\" height=\"417\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-project-setup.png\" alt=\"maven spring framework - project setup\" class=\"wp-image-75436\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-project-setup.png 860w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-project-setup-300x145.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-project-setup-768x372.png 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><figcaption>Spring Maven Project Setup<\/figcaption><\/figure>\n<\/div>\n<p>Notice that now, you will have a new directory with the same name as the artifactId inside the chosen directory. Now, feel free to open the project in your favourite IDE. Also, <strong>this project is only required to demonstrate various dependency tree made when we add appropriate Spring dependencies<\/strong>.<\/p>\n<p>Finally, instead of using an IDE to make this project, we used a simple maven command. This helps us to make project setup and initialisation free from any specific IDE you may use.<\/p>\n<h2 class=\"wp-block-heading\">3. Spring Dependencies with Maven<\/h2>\n<p>Due to a highly modular nature of Spring Framework, adding one dependency doesn&#8217;t create a requirement for another dependency. For instance, the Spring Context dependency doesn&#8217;t need any Spring MVC dependency or Spring Data libraries. Due to this, we will cover each of the dependencies in their own, separate sections.<\/p>\n<p>To add the Spring Context support, here is the dependency which is needed:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Spring Context<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:xml\">&lt;properties&gt;\n  &lt;org.springframework.version&gt;5.0.4.RELEASE&lt;\/org.springframework.version&gt;\n&lt;\/properties&gt;\n&lt;dependency&gt;\n  &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\n  &lt;artifactId&gt;spring-context&lt;\/artifactId&gt;\n  &lt;version&gt;${org.springframework.version}&lt;\/version&gt;\n  &lt;scope&gt;runtime&lt;\/scope&gt;\n&lt;\/dependency&gt;\n<\/pre>\n<p>Note that we have used the latest available Spring version which was released at the time this post was published. Again, the latest versions of all the libraries we use can be found on the <a href=\"http:\/\/search.maven.org\/#search%7Cga%7C1%7Cg%3A%22org.springframework%22\" target=\"_blank\" rel=\"noopener\">Maven Central<\/a>.<\/p>\n<p>To understand all the dependencies which are added to the project when we add a specific dependency, we can run a simple Maven command which allows us to see a complete Dependency Tree for the project. Here is the Maven command which we can use to achieve the same:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Check Dependency Tree<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:bash\">mvn dependency:tree<\/pre>\n<p>When we run this command, it will show us the following Dependency Tree:<\/p>\n<div class=\"wp-block-image wp-image-75437\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"860\" height=\"348\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-context-tree.png\" alt=\"maven spring framework - dependency tree\" class=\"wp-image-75437\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-context-tree.png 860w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-context-tree-300x121.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-context-tree-768x311.png 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><figcaption>Spring Context Dependency Tree<\/figcaption><\/figure>\n<\/div>\n<p>The <code>spring-context<\/code> library <strong>bring the actual Spring Injection Container<\/strong> into the project and needs few more JARs like <code>spring-aop<\/code>, <code>spring-beans<\/code>, <code>spring-core<\/code>, <code>spring-expression<\/code> and <code>spring-jcl<\/code>. When we ran the command above, before presenting to us the Dependency Tree, maven first downloaded the JARs again to confirm if something has changed.<\/p>\n<p>Note that if one of the dependency needed another dependency to run, it was also downloaded and was shown as a sub-branch of the tree in the dependency tree above. The method of sub-branched representation of an indirect dependency clears out when some dependency brings another dependency into the dependency pool of the build system.<\/p>\n<p>Finally, <code>spring-context<\/code> dependency we added here is <strong>runtime scope<\/strong>. This is because this JAR is only needed at runtime to provide Spring related classes only and not used in the project directly, at least not used for simple projects.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<h2 class=\"wp-block-heading\">4. Spring Persistence Maven Dependencies<\/h2>\n<p>Now, let\u2019s look at the core dependencies which are needed for Spring Persistence implementations:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Spring Persistence<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:xml\">&lt;dependency&gt;\n  &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-orm&lt;\/artifactId&gt;\n  &lt;version&gt;${org.springframework.version}&lt;\/version&gt;\n&lt;\/dependency&gt;\n<\/pre>\n<p>When we look at the dependency tree for this library, we will observe that JDBC and Transaction support is also included in this dependency:<\/p>\n<div class=\"wp-block-image wp-image-75438\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"860\" height=\"348\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-persistence-tree.png\" alt=\"\" class=\"wp-image-75438\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-persistence-tree.png 860w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-persistence-tree-300x121.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-persistence-tree-768x311.png 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><figcaption>Spring Persistence Dependency Tree<\/figcaption><\/figure>\n<\/div>\n<h2 class=\"wp-block-heading\">5. Spring MVC Maven Dependencies<\/h2>\n<p>Spring MVC Maven dependency is the main dependency you will use when you start to work on Web projects with Spring. Here is the maven dependency to setup Spring MVC in your project:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Spring MVC<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:xml\">&lt;dependency&gt;\n  &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-web&lt;\/artifactId&gt;\n  &lt;version&gt;${org.springframework.version}&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n  &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-webmvc&lt;\/artifactId&gt;\n  &lt;version&gt;${org.springframework.version}&lt;\/version&gt;\n&lt;\/dependency&gt;\n<\/pre>\n<p>This is one of the most used dependencies in Spring Framework Maven dependency family. Let\u2019s look at the dependency tree for the <code>spring-web<\/code> and <code>spring-webmvc<\/code> library now:<\/p>\n<div class=\"wp-block-image wp-image-75439\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"860\" height=\"367\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-mvc-tree.png\" alt=\"maven spring framework - Spring MVC Dependency Tree\" class=\"wp-image-75439\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-mvc-tree.png 860w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-mvc-tree-300x128.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-mvc-tree-768x328.png 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><figcaption>Spring MVC Dependency Tree<\/figcaption><\/figure>\n<\/div>\n<p>The <code>spring-web<\/code> library contains most common web utilities for a <strong>Servlet Environment<\/strong>. The second library, <code>spring-webmvc<\/code> brings the <strong>MVC support<\/strong> for the Servlet Environment.<\/p>\n<h2 class=\"wp-block-heading\">6. Providing testing support with Maven<\/h2>\n<p>Testing is always an integral part of a project. In Spring, we have a dependency for the libraries that we can test our application as well. Its dependencies are defined as:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Spring Testing<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:xml\">&lt;dependency&gt;\n  &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-test&lt;\/artifactId&gt;\n  &lt;version&gt;${org.springframework.version}&lt;\/version&gt;\n&lt;\/dependency&gt;\n<\/pre>\n<p>Let&#8217;s look what all libraries contain with its dependency tree:<\/p>\n<div class=\"wp-block-image wp-image-75440\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"860\" height=\"298\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-test-tree.png\" alt=\"\" class=\"wp-image-75440\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-test-tree.png 860w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-test-tree-300x104.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/spring-maven-test-tree-768x266.png 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><figcaption>Spring Testing Dependency Tree<\/figcaption><\/figure>\n<\/div>\n<p>Even this is all the test dependency we need right now, we will look at two most commonly used test libraries used in Spring Framework here, <a href=\"https:\/\/www.javacodegeeks.com\/2014\/11\/junit-tutorial-unit-testing.html\" target=\"_blank\" rel=\"noopener\">JUnit<\/a> and <a href=\"https:\/\/www.javacodegeeks.com\/2015\/11\/testing-with-mockito.html\" target=\"_blank\" rel=\"noopener\">Mockito<\/a>.<\/p>\n<h2 class=\"wp-block-heading\">7. Spring Security Maven Dependencies<\/h2>\n<p>Maven dependencies related to <strong>Spring Security<\/strong> support are discussed in detail in the <a href=\"https:\/\/www.javacodegeeks.com\/2018\/03\/spring-security-with-maven-tutorial.html\" target=\"_blank\" rel=\"noopener\">Spring Security with Maven<\/a> lesson.<\/p>\n<h2 class=\"wp-block-heading\">8. Using JUnit with Maven<\/h2>\n<p>To add JUnit dependency in your Spring based project, just add a simple dependency with <code>test<\/code> scope:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>JUnit<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:xml\">&lt;dependency&gt;\n  &lt;groupId&gt;junit&lt;\/groupId&gt;\n  &lt;artifactId&gt;junit&lt;\/artifactId&gt;\n  &lt;version&gt;4.12&lt;\/version&gt;\n  &lt;scope&gt;test&lt;\/scope&gt;\n&lt;\/dependency&gt;\n<\/pre>\n<p>Let&#8217;s look what all libraries contain with its dependency tree:<\/p>\n<div class=\"wp-block-image wp-image-75441\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"860\" height=\"283\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/junit-maven-tree.png\" alt=\"\" class=\"wp-image-75441\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/junit-maven-tree.png 860w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/junit-maven-tree-300x99.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/junit-maven-tree-768x253.png 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><figcaption>JUnit Dependency Tree<\/figcaption><\/figure>\n<\/div>\n<p>JUnit brings along the Hamcrest library with it too. To use Hamcrest matchers in JUnit, we use the <code>assertThat<\/code> statement followed by one or several matchers. To read about the available matchers in Hamcrest, read <a href=\"https:\/\/www.javacodegeeks.com\/2015\/11\/hamcrest-matchers-tutorial.html\" target=\"_blank\" rel=\"noopener\">Hamcrest matchers tutorial<\/a>.<\/p>\n<h2 class=\"wp-block-heading\">9. Using Mockito with Maven<\/h2>\n<p>Mockito is one of the most widely used Java Testing Dependency which can be used to mock objects and their behaviour as well:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>Mockito<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:xml\">&lt;dependency&gt;\n  &lt;groupId&gt;org.mockito&lt;\/groupId&gt;\n  &lt;artifactId&gt;mockito-all&lt;\/artifactId&gt;\n  &lt;version&gt;1.10.19&lt;\/version&gt;\n  &lt;scope&gt;test&lt;\/scope&gt;\n&lt;\/dependency&gt;\n<\/pre>\n<p>Let&#8217;s look what all libraries contain with its dependency tree:<\/p>\n<div class=\"wp-block-image wp-image-75442\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"860\" height=\"327\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mockito-maven-tree.png\" alt=\"\" class=\"wp-image-75442\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mockito-maven-tree.png 860w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mockito-maven-tree-300x114.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/03\/mockito-maven-tree-768x292.png 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><figcaption>Mockito Dependency Tree<\/figcaption><\/figure>\n<\/div>\n<p>[ulp id=&#8217;oiCtu6x3AwpaVNPI&#8217;]<br \/>\n&nbsp;<\/p>\n<h2 class=\"wp-block-heading\">10. Project: Hello World<\/h2>\n<p>Just as a simple demonstration for Spring MVC, we will show how a simple controller can be made with Spring MVC 5. For this, we used the dependencies we mentioned in Spring MVC section along with some Servlet Container dependencies. Here are a list of all the dependencies we used in the project:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>pom.xml<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:xml\">&lt;dependency&gt;\n&lt;dependency&gt;\n  &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\n  &lt;artifactId&gt;spring-web&lt;\/artifactId&gt;\n  &lt;version&gt;${org.springframework.version}&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n  &lt;groupId&gt;org.springframework&lt;\/groupId&gt;\n  &lt;artifactId&gt;spring-webmvc&lt;\/artifactId&gt;\n  &lt;version&gt;${org.springframework.version}&lt;\/version&gt;\n&lt;\/dependency&gt;\n\n&lt;dependency&gt;\n  &lt;groupId&gt;javax.servlet&lt;\/groupId&gt;\n  &lt;artifactId&gt;servlet-api&lt;\/artifactId&gt;\n  &lt;version&gt;2.5&lt;\/version&gt;\n  &lt;scope&gt;provided&lt;\/scope&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n  &lt;groupId&gt;javax.servlet.jsp&lt;\/groupId&gt;\n  &lt;artifactId&gt;jsp-api&lt;\/artifactId&gt;\n  &lt;version&gt;2.1&lt;\/version&gt;\n  &lt;scope&gt;provided&lt;\/scope&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n  &lt;groupId&gt;javax.servlet&lt;\/groupId&gt;\n  &lt;artifactId&gt;jstl&lt;\/artifactId&gt;\n  &lt;version&gt;1.2&lt;\/version&gt;\n&lt;\/dependency&gt;\n<\/pre>\n<p>To configure a front Controller in the project, we will sub-class the <code>AbstractAnnotationConfigDispatcherServletInitializer<\/code> class:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>AppConfig.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java\">package com.javacodegeeks.example.config;\n\nimport org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;\n\npublic class AppConfig extends AbstractAnnotationConfigDispatcherServletInitializer {\n\n    protected Class&lt;?&gt;[] getRootConfigClasses() {\n        return new Class[] { RootConfig.class };\n    }\n\n    protected Class&lt;?&gt;[] getServletConfigClasses() {\n        return new Class[] { WebMvcConfig.class };\n    }\n\n    protected String[] getServletMappings() {\n        return new String[] { \"\/\" };\n    }\n}\n<\/pre>\n<p>Now we need to make a Root configuration class which will be emptry as we aren&#8217;t configuring any beans in the project for now:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>RootConfig.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java\">package com.javacodegeeks.example.config;\n\nimport org.springframework.context.annotation.ComponentScan;\nimport org.springframework.context.annotation.Configuration;\n\n@Configuration\n@ComponentScan(basePackages = { \"com.javacodegeeks.example.*\" })\npublic class RootConfig {\n}\n<\/pre>\n<p>To make this project as a MVC project, we need to make a <code>WebMvcConfigurer<\/code> class which also marks itself with <code>@EnableWebMvc<\/code> annotation. This enables the MVC nature of the project:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>WebMvcConfig.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java\">package com.javacodegeeks.example.config;\n\nimport org.springframework.context.annotation.ComponentScan;\nimport org.springframework.context.annotation.Configuration;\nimport org.springframework.web.servlet.config.annotation.EnableWebMvc;\nimport org.springframework.web.servlet.config.annotation.WebMvcConfigurer;\n\n@Configuration\n@EnableWebMvc\n@ComponentScan(basePackages = { \"com.javacodegeeks.example.*\" })\npublic class WebMvcConfig implements WebMvcConfigurer {\n}\n<\/pre>\n<p>Once we&#8217;re ready with the configuration of the project, we can now add a Controller as a final step. To demonstrate, we will make a simple GET API which just returns a String as &#8220;Hello World&#8221;:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>HomeController.java<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:java\">package com.javacodegeeks.example.controller;\n\nimport org.springframework.stereotype.Controller;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.ResponseBody;\n\n@Controller\npublic class HomeController {\n\n    @GetMapping(\"\/hello\")\n    @ResponseBody\n    public String helloWorld() {\n        return \"Hello World\";\n    }\n}\n<\/pre>\n<p>When we run this project, we&#8217;ll see a simple message in our browser. Open the URL we configured, which is:<\/p>\n<p><span style=\"text-decoration: underline;\"><em>URL<\/em><\/span><\/p>\n<pre class=\"wp-block-preformatted brush:bash\">localhost:8080\/hello\n<\/pre>\n<p>We will see the following output:<\/p>\n<div class=\"wp-block-image wp-image-75491\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"860\" height=\"504\" src=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/spring-mvc-hello.png\" alt=\"\" class=\"wp-image-75491\" srcset=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/spring-mvc-hello.png 860w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/spring-mvc-hello-300x176.png 300w, https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/spring-mvc-hello-768x450.png 768w\" sizes=\"(max-width: 860px) 100vw, 860px\" \/><figcaption>Spring MVC Hello World<\/figcaption><\/figure>\n<\/div>\n<p>For a detailed explanation of why the configuration classes were needed, read <a href=\"https:\/\/www.javacodegeeks.com\/2013\/05\/spring-mvc-tutorial.html\" target=\"_blank\" rel=\"noopener\">Spring MVC Tutorial<\/a>.<\/p>\n<h2 class=\"wp-block-heading\">11. Conclusion<\/h2>\n<p>In this post, we talked about various Maven Dependencies related to Spring Framework which makes things crystal clear about the modularity of each dependency and how they&#8217;re independent of each other. This lesson is an excellent starting point to understand the required dependencies. We studied several parts of the Spring Framework dependencies and dependencies for JUNit and Mockito as well, even though they aren&#8217;t part of the Spring Framework itself but are often used with Spring-based projects to provide extensive Testing support for various layers of Spring Framework.<\/p>\n<p>Finally, we learned how we can show a dependency tree of a project by using a simple maven command and observe which dependencies of a project depends on what other dependencies.<\/p>\n<p>To learn more, read our <a href=\"https:\/\/www.javacodegeeks.com\/java-spring-tutorial\">Java Spring Tutorials<\/a>.<\/p>\n<h2 class=\"wp-block-heading\">12. Download the Source Code<\/h2>\n<p>This lesson explained how we can use Maven Dependencies for Spring Framework and present sepcific use-cases by keeping the dependencies separate and modular.<\/p>\n<div class=\"download\"><strong>Download<\/strong><br \/>You can download the full source code of this example here: <strong><a href=\"http:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2018\/04\/JCG-SpringMaven-Example.zip\" target=\"_blank\" rel=\"noopener\">Spring Framework with Maven Tutorial<\/a><\/strong><\/div>\n<p><strong>Last updated on Apr. 29th, 2021<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we shall demonstrate how to use Maven dependencies for Spring framework for very specific use-cases. The latest versions of all the libraries we use can be found on the Maven Central. 1. Introduction Understanding how Maven dependencies work and how they are managed, is important in a project for an effective build &hellip;<\/p>\n","protected":false},"author":20016,"featured_media":240,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[274,670,460,30],"class_list":["post-75398","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-enterprise-java","tag-junit","tag-maven","tag-mockito","tag-spring"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Spring Framework with Maven Tutorial - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"In this post, we shall demonstrate how to use Maven dependencies for Spring framework for very specific use-cases. The latest versions of all the\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring Framework with Maven Tutorial - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"In this post, we shall demonstrate how to use Maven dependencies for Spring framework for very specific use-cases. The latest versions of all the\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2018-04-02T19:00:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-11T08:32:31+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Shubham Aggarwal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Shubham Aggarwal\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-with-maven-tutorial.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-with-maven-tutorial.html\"},\"author\":{\"name\":\"Shubham Aggarwal\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/0953481a8babbb7a63907edb41f357ad\"},\"headline\":\"Spring Framework with Maven Tutorial\",\"datePublished\":\"2018-04-02T19:00:58+00:00\",\"dateModified\":\"2023-12-11T08:32:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-with-maven-tutorial.html\"},\"wordCount\":1357,\"commentCount\":2,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-with-maven-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"keywords\":[\"JUnit\",\"Maven\",\"Mockito\",\"Spring\"],\"articleSection\":[\"Enterprise Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-with-maven-tutorial.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-with-maven-tutorial.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-with-maven-tutorial.html\",\"name\":\"Spring Framework with Maven Tutorial - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-with-maven-tutorial.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-with-maven-tutorial.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"datePublished\":\"2018-04-02T19:00:58+00:00\",\"dateModified\":\"2023-12-11T08:32:31+00:00\",\"description\":\"In this post, we shall demonstrate how to use Maven dependencies for Spring framework for very specific use-cases. The latest versions of all the\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-with-maven-tutorial.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-with-maven-tutorial.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-with-maven-tutorial.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/spring-logo.jpg\",\"width\":150,\"height\":150,\"caption\":\"spring-interview-questions-answers\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/spring-with-maven-tutorial.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Enterprise Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/enterprise-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Spring Framework with Maven Tutorial\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/0953481a8babbb7a63907edb41f357ad\",\"name\":\"Shubham Aggarwal\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3f2c210fd210e1cafb930887d5f4c29613eb8183e62743a99e0cb93dcaec1a2b?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3f2c210fd210e1cafb930887d5f4c29613eb8183e62743a99e0cb93dcaec1a2b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3f2c210fd210e1cafb930887d5f4c29613eb8183e62743a99e0cb93dcaec1a2b?s=96&d=mm&r=g\",\"caption\":\"Shubham Aggarwal\"},\"description\":\"Shubham is a Java EE Engineer with about 3 years of experience in building quality products with Spring Boot, Spring Data, AWS, Kafka, PrestoDB.\",\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/sbmaggarwal\\\/\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/shubham-aggarwal\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Spring Framework with Maven Tutorial - Java Code Geeks","description":"In this post, we shall demonstrate how to use Maven dependencies for Spring framework for very specific use-cases. The latest versions of all the","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html","og_locale":"en_US","og_type":"article","og_title":"Spring Framework with Maven Tutorial - Java Code Geeks","og_description":"In this post, we shall demonstrate how to use Maven dependencies for Spring framework for very specific use-cases. The latest versions of all the","og_url":"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2018-04-02T19:00:58+00:00","article_modified_time":"2023-12-11T08:32:31+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","type":"image\/jpeg"}],"author":"Shubham Aggarwal","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Shubham Aggarwal","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html"},"author":{"name":"Shubham Aggarwal","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/0953481a8babbb7a63907edb41f357ad"},"headline":"Spring Framework with Maven Tutorial","datePublished":"2018-04-02T19:00:58+00:00","dateModified":"2023-12-11T08:32:31+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html"},"wordCount":1357,"commentCount":2,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","keywords":["JUnit","Maven","Mockito","Spring"],"articleSection":["Enterprise Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html","url":"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html","name":"Spring Framework with Maven Tutorial - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","datePublished":"2018-04-02T19:00:58+00:00","dateModified":"2023-12-11T08:32:31+00:00","description":"In this post, we shall demonstrate how to use Maven dependencies for Spring framework for very specific use-cases. The latest versions of all the","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/spring-logo.jpg","width":150,"height":150,"caption":"spring-interview-questions-answers"},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/spring-with-maven-tutorial.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Enterprise Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/enterprise-java"},{"@type":"ListItem","position":4,"name":"Spring Framework with Maven Tutorial"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/0953481a8babbb7a63907edb41f357ad","name":"Shubham Aggarwal","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3f2c210fd210e1cafb930887d5f4c29613eb8183e62743a99e0cb93dcaec1a2b?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3f2c210fd210e1cafb930887d5f4c29613eb8183e62743a99e0cb93dcaec1a2b?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3f2c210fd210e1cafb930887d5f4c29613eb8183e62743a99e0cb93dcaec1a2b?s=96&d=mm&r=g","caption":"Shubham Aggarwal"},"description":"Shubham is a Java EE Engineer with about 3 years of experience in building quality products with Spring Boot, Spring Data, AWS, Kafka, PrestoDB.","sameAs":["https:\/\/www.linkedin.com\/in\/sbmaggarwal\/"],"url":"https:\/\/www.javacodegeeks.com\/author\/shubham-aggarwal"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/75398","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/20016"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=75398"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/75398\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/240"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=75398"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=75398"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=75398"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}