LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 519 min read

Setting Up a Spring Project

Set up a Spring project from scratch using Maven, add the spring-context dependency, and understand a minimal project structure.

Introduction

Now that you understand what Spring is and why it matters, it is time to build a real project. In this lesson, we set up a plain Spring Core application (no Spring Boot yet) using a build tool, so you can see exactly what dependencies are needed and how a minimal project is structured.

Prerequisites

Before You Start
  • A Java Development Kit (JDK) 17 or later installed on your machine.
  • Either Maven or Gradle installed, or an IDE (IntelliJ IDEA, Eclipse, VS Code) that bundles them.
  • Basic familiarity with the command line for running build commands.

Project Structure

A standard Maven or Gradle Java project follows a conventional folder layout. Source code lives under src/main/java, and tests live under src/test/java, mirroring your package structure.

Project layout
spring-demo/
├── pom.xml
└── src/
└── main/
└── java/
└── com/
└── programinds/
└── demo/
├── AppConfig.java
├── Greeter.java
└── MainApp.java

Setting Up with Maven

Create a pom.xml file at the root of your project and add the spring-context dependency, which pulls in Spring Core (the IoC container) along with its required transitive dependencies.

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>com.programinds</groupId>
<artifactId>spring-demo</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<spring.version>6.1.13</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
Why spring-context?

The spring-context artifact transitively pulls in spring-core, spring-beans, and spring-aop — everything needed for the IoC container and annotation-based configuration.

Setting Up with Gradle

If you prefer Gradle, the equivalent setup goes in build.gradle (or build.gradle.kts for Kotlin DSL).

build.gradle
plugins {
id 'java'
}
group = 'com.programinds'
version = '1.0.0'
java {
sourceCompatibility = JavaVersion.VERSION_17
}
repositories {
mavenCentral()
}
dependencies {
implementation 'org.springframework:spring-context:6.1.13'
}

Writing Your First Class

With the dependency in place, write a simple bean and a configuration class, following the same pattern from Lesson 1.

src/main/java/com/programinds/demo/Greeter.java
package com.programinds.demo;
import org.springframework.stereotype.Component;
@Component
public class Greeter {
public String greet(String name) {
return "Hello from a freshly set up Spring project, " + name + "!";
}
}
src/main/java/com/programinds/demo/AppConfig.java
package com.programinds.demo;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "com.programinds.demo")
public class AppConfig {
}

Running the Application

src/main/java/com/programinds/demo/MainApp.java
package com.programinds.demo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
Greeter greeter = context.getBean(Greeter.class);
System.out.println(greeter.greet("Team"));
}
}
Running with Maven
mvn compile exec:java -Dexec.mainClass="com.programinds.demo.MainApp"
Console Output

Click Run to see what this code prints.

Common Mistakes

Avoid These Mistakes
  • Forgetting to set @ComponentScan's basePackages to match where your classes actually live, causing "NoSuchBeanDefinitionException".
  • Mixing Spring versions across dependencies, which can cause obscure runtime errors.
  • Placing classes outside the package scanned by @ComponentScan and wondering why they are never picked up.

Best Practices

  • Pin a single Spring version using a properties block or a BOM so all Spring dependencies stay in sync.
  • Keep your @ComponentScan base package at or above the root package of your application.
  • Use your IDE's built-in Maven/Gradle support to avoid typos in dependency coordinates.

Frequently Asked Questions

No. This course intentionally uses plain Spring Core with spring-context so you understand the fundamentals before Spring Boot automates them.

Yes. IntelliJ IDEA (including the free Community edition) can create and run Maven or Gradle projects directly through its UI.

spring-context transitively includes spring-core, spring-beans, and spring-aop, which together are enough to run the IoC container.

Key Takeaways

  • A minimal Spring project needs only the spring-context dependency.
  • Standard Maven/Gradle folder conventions apply: source under src/main/java.
  • @ComponentScan tells Spring which packages to search for annotated classes.
  • You can run a plain Spring application from a simple main() method — no application server required.

Summary

You now have a working Spring project structure with the correct dependency and a running application. With the setup out of the way, the next lessons dig into the theory that makes all of this work: Inversion of Control.

Next Lesson →

Inversion of Control (IoC) Explained