A Plain Spring Project, No Boot: Your First Bean With spring-context
One dependency, one recipe, one getBean: a Maven project with spring-context 7.0.9, a Blog record, an AppConfig with @Bean, and a main that asks the container for the Blog. Then delete @Bean and meet NoSuchBeanDefinitionException.
What you will learn
- Create a Maven project with only the spring-context dependency
- Write a @Configuration class with one @Bean method
- Start the container with AnnotationConfigApplicationContext and get the bean
- Read NoSuchBeanDefinitionException when the @Bean is missing
Before you start
- Why Spring in 2026
- Java records
Beans are the foundation of everything Spring does. In this article we create one bean and run it, in a plain Maven project with no Spring Boot at all.
pom.xml: one dependency
A new Maven project in IntelliJ called plain-spring. It compiles for Java 25 and has exactly one dependency:
<properties>
<maven.compiler.target>25</maven.compiler.target>
<maven.compiler.source>25</maven.compiler.source>
</properties>
<dependencies>
<!-- Spring Framework 7: the core container - no Spring Boot -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>7.0.9</version>
</dependency>
</dependencies>
spring-context is the Spring container. Maven downloads it plus what it needs itself: nine jars, about 5 MB in total. There's no Spring Boot anywhere.
The Blog record
package org.studyeasy.spring;
// A plain Java object - nothing Spring about it
public record Blog(String name, String author) {
}
A blog has a name and an author. Because it's a record, Java writes the constructor, the accessors and toString. No annotations, nothing Spring about it.
Just Java: new Blog
package org.studyeasy.spring;
public class Ex00_PlainJava {
public static void main(String[] args) {
// No Spring at all: we make the Blog ourselves
Blog blog = new Blog("StudyEasy Blog", "Emma Clarke");
System.out.println(blog);
}
}
Blog[name=StudyEasy Blog, author=Emma Clarke]
It works. The catch: every class that needs a Blog has to call new Blog(...) itself, with the same values.
Why a bean?
A bean is just an object that Spring creates and manages for you. @Bean is how you hand Spring the recipe. If the Blog changes tomorrow, you change one method instead of three classes.
AppConfig: the recipe
package org.studyeasy.spring;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
// Tells Spring which objects (beans) to create
@Configuration
public class AppConfig {
@Bean
public Blog blog() {
return new Blog("StudyEasy Blog", "Emma Clarke");
}
}
@Configuration tells Spring this class says which objects to make. Each @Bean method is one bean: Spring calls blog() and keeps the result. It's the same new Blog(...) as before, now written once.
One container, one bean
package org.studyeasy.spring;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.SpringVersion;
public class Ex01_HelloSpring {
public static void main(String[] args) {
// Start the Spring container - no Spring Boot, just spring-context
try (var context =
new AnnotationConfigApplicationContext(AppConfig.class)) {
String version = SpringVersion.getVersion();
System.out.println("Spring " + version + " is running");
// Ask the container for the bean it created
Blog blog = context.getBean(Blog.class);
System.out.println(blog);
}
}
}
Spring 7.0.9 is running
Blog[name=StudyEasy Blog, author=Emma Clarke]
new AnnotationConfigApplicationContext(AppConfig.class) starts the container from the recipe. Inside try-with-resources, it's closed nicely at the end. Then, instead of new Blog, we ask the container: context.getBean(Blog.class). Both output lines are ours; plain Spring prints no banner or big log on a normal run.
Delete @Bean, run again
Delete only the @Bean line above blog() and run Ex01 again:
Spring 7.0.9 is running
NoSuchBeanDefinitionException: No qualifying bean of type 'org.studyeasy.spring.Blog' available
The first line still prints because the container starts fine. Then getBean asks for a Blog and the container has none. Without @Bean, blog() is just a normal method that Spring never calls. Spring hands out only the beans it knows about. You'll see this exception many times in your career, so it's good to meet it on day one. Then put the @Bean back.
Quick recap
| Step | Code |
|---|---|
| Add the container | spring-context in pom.xml |
| Write the recipe | @Configuration class with a @Bean method |
| Start the container | new AnnotationConfigApplicationContext(AppConfig.class) |
| Get the bean | context.getBean(Blog.class) |
Why this way of working matters comes next, in inversion of control and dependency injection.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
Why Spring in 2026: What the Container Does and What Spring Framework 7 Changed
Spring in one line: a container that creates your classes and hands each one what it needs. Plus the Spring Framework 7 baseline (JDK 17 to 25, Jakarta EE 11, Tomcat 11, JUnit 6), what got better than Spring 6, and a two-line version check.
Inversion of Control and Dependency Injection in Spring: One Program, Built Three Ways
A PostService needs a PostRepository. Build it three ways: the service calls new itself (tight coupling), main hands it in through the constructor (DI by hand), and the Spring container does the wiring from a @Configuration recipe.
Spring Component Scanning: @Component and @ComponentScan Instead of @Bean Methods
Instead of one @Bean method per class, put @Component on EmailNotifier and PostService and @ComponentScan on an empty BlogConfig. Spring walks the package, finds the stickers and builds the beans; main doesn't change.
Spring Autowiring: Spring Fills the Constructor, and When @Autowired Is Needed
PostService asks for an EmailNotifier in its constructor and Spring plugs in the matching bean. With one constructor that's all. Add a second, empty constructor and Spring picks it, the notifier stays null, and only @Autowired fixes the choice.
