Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

How to Create a Simple Spring Application?

Creating a simple Spring application involves a series of steps. Here's a basic guide to help you create a basic Spring Boot application.

  1. Setting Up the Environment:

    • Install the Java Development Kit (JDK). Spring Boot 2.5 requires JDK 8, 11, or 16 to build and run.
    • Install Maven or Gradle, as they are the most commonly used build tools for Spring applications.
  2. Initialize the Spring Boot Project:

    • Visit the Spring Initializr website.
    • Choose your project type (Maven or Gradle), language (Java, Kotlin, or Groovy), and the version of Spring Boot.
    • Add dependencies. For a web application, you'd typically select "Spring Web."
    • Click "Generate" and download the project zip.
    • Extract the downloaded zip file.
  3. Exploring the Project Structure:

    • The project structure will look somewhat like this:
      ������ src
      ��   ������ main
      ��   ��   ������ java
      ��   ��   ��   ������ com
      ��   ��   ��       ������ example
      ��   ��   ��           ������ demo
      ��   ��   ��               ������ DemoApplication.java
      ��   ��   ������ resources
      ��   ��       ������ application.properties
      ��   ������ test
      ��       ������ java
      ������ pom.xml (or build.gradle if you chose Gradle)
      
  4. Write Your First Endpoint:

    • Navigate to DemoApplication.java.
    • Create a simple REST endpoint inside this class:
      @RestController
      public class HelloController {
          
          @GetMapping("/hello")
          public String sayHello() {
              return "Hello, Spring!";
          }
      }
      
  5. Running Your Application:

    • If you're using Maven, you can run your application using:
      ./mvnw spring-boot:run
      
    • If you're using Gradle, use:
      ./gradlew bootRun
      
  6. Test Your Application:

    • Once your application is running, visit http://localhost:8080/hello in your browser. You should see "Hello, Spring!" displayed.
  7. Further Customization:

    • Use the application.properties file inside the resources folder to customize various aspects of your application, like server port, database connections, etc.
  8. Add More Dependencies and Features:

    • Spring Boot has a plethora of features and integrations. You can add dependencies for data access (like JPA), messaging, security, etc.
  9. Building and Packaging:

    • With Maven:
      ./mvnw clean package
      
    • With Gradle:
      ./gradlew clean build
      
    • This will produce a .jar file inside the target or build directory, which you can run using the Java -jar command.
  10. Continuous Learning:

With these steps, you have a basic Spring Boot application up and running. The Spring ecosystem is vast, so depending on your requirements, there's much more to explore!