Maven and Spring 3 Example

I have installed JDK, Maven, and STS before starting this project. Please refer below links to know how to install these technologies.
  1. How to install JDK.
  2. How to install Maven.
  3. How to install STS.
This tutorial shows you how to create a simple project in Spring 3 using Maven.
  1. First generate a project structure using Maven. Type below code in command prompt. Here name of my project is bsnl. You can give any name as you wish in DartifactId. If you are trying to create the project first time in maven, then it will take more time because maven will take time to download the set up.
    mvn archetype:generate -DgroupId=com.bsnl.core -DartifactId=bsnl -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
    Then this project will be created in a folder where command prompt is pointed. In my case it is D:/bsnlapp. Thus it created the project structure successfully.
  2. Then Import this project to STS. Open STS and go to File -> Import -> Maven -> Existing Maven Projects. Then select your maven project which is created in first step. Thus I have Imported my project successfully.
  3. Then update pom.xml file.
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.bsnl.core</groupId>
      <artifactId>bsnl</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>bsnl</name>
      <url>http://maven.apache.org</url>
      <properties>
    <spring.version>3.0.5.RELEASE</spring.version>
    </properties>

      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
     
        <!-- Spring 3 dependencies -->
    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${spring.version}</version>
    </dependency>

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
    </dependency>
      </dependencies>
    </project>
  4. Then add simple spring bean.
    package com.bsnl.core;

    public class AppController {
    private String name;

    public void setName(String name) {
    this.name = name;
    }

    public void printHello() {
    System.out.println("Spring 3 : Hello ! " + name);
    }
    }
  5. Create a Spring configuration file, and declare all the available Spring beans. File:Spring-Module.xml
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

    <bean id="helloBean" class="com.bsnl.core.AppController">
    <property name="name" value="Bsnl" />
    </bean>

    </beans>
  6. Update App.java
    package com.bsnl.core;

    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;

    /**
     * Hello world!
     *
     */
    public class App
    {
    public static void main(String[] args) {
    ApplicationContext context = new ClassPathXmlApplicationContext(
    "Spring-Module.xml");

    AppController obj = (AppController) context.getBean("helloBean");
    obj.printHello();
    }
    }
  7. Structure of this project look like this.
  8. Then run this project. Right click on project -> Run As -> Java Application.
  9. After this I tried to develop a web application using Maven. Please go through below link to know more about this. https://www.mkyong.com/maven/how-to-create-a-web-application-project-with-maven/

How to Install STS in Windows


  1. Download STS from https://spring.io/tools/sts/all 
  2. I have downloaded the 64bit version. Then Unzip the downloaded zip file to any folder.
  3. Then start STS by double clicking on STS exe file in the unzipped folder.
  4. Then I got an error message like.
  5. Then I checked the log file specified in error message. The log file shown error message like org.eclipse.e4.core.di.InjectionException: java.lang.NoClassDefFoundError: javax/annotation/PostConstruct. Then I got solution for this issue by searching in internet. If you are using Java 9 or 10, then you may also have same issue. To resolve this issue add --add-modules=ALL-SYSTEM in STS.ini file.
  6. Then again open STS by double clicking STS.exe file. Thus STS opened for me successfully.

How to Install Maven in Windows


  1. First, install JDK. Click here to know how to install JDK.
  2. Then visit Maven Official Site and download apache-maven-3.5.3-bin.zip.
  3. Then unzip this file to any folder.
  4. Goto Control panel -> System -> Advanced System Settings -> Environment Variables. Then add M2_HOME and MAVEN_HOME. Give value of both as the path of the unzipped folder.
  5. Then update PATH variable, append %M2_HOME%\bin in Path variable, so that you can run the Maven’s command everywhere.
  6. Then test whether your maven installation is proper by typing mvn -version in command prompt. Then I got an error as shown below.
  7. To remove this error, add JAVA_HOME. Give the path of your JDK here.
  8. Then again test whether your installation is proper by typing mvn -version in command prompt. Then I got the message as shown below. Thus I have completed the maven installation successfully.

How to install JDK in Windows.

  1. Download JDK 1.8
  2. Then install JDK by double clicking on the downloaded file.
  3. Select Development Tools and click Next.
  4. Then JDK and JRE will install in C:\Program Files\Java.
  5. Then test whether java installed properly by typing 'java -version' in command prompt. Then it's shown version correctly for me. Then again type 'javac -version'. Then I got an error like 'javac is not recognized as an internal or external command'
  6. To resolve this error, Goto Control panel -> System -> Advanced System Settings -> Environment Variables. Then select 'Path' in the System variables and click 'Edit'.
  7. Then put variable value 'C:\Program Files\Java\jdk-10.0.1\bin' after a semicolon. Do not remove any variable value which already available in Path variable.
  8. Then again test whether java installed properly by typing 'javac -version' in command prompt. Now it's shown the version correctly for me. So I have completed the JDK installation successfully.