Spring Project Configuration Using Java and Annotations

In this article, we will learn to configure the application using only Java annotations, which means no XML! No need to write any XML code anymore.

Let’s get started.

First, create a maven-based Spring application and then add the following dependencies to the pom.xml file:
// pom.xml







<description>a simple spring app</description>

















In this project, we will use mainly these three annotations:

  • @Configuration
  • @ComponentScan
  • @Component

The @Configuration annotation will be used to create a config file, and the @ComponentScan annotation will be used to scan the components, similar to the XML <context:component-scan> tag.

Create a Java Config File

Since we are using the Java annotations, we will create a Java config file rather than an applicationContext.xml file. This config class file will be empty for now but has a @Configuartion annotation.

Here, we don’t need to create any XML files. Rather, we create a simple Java class and mark that by @Configuration annotation. In our case, we created a class that looks like the following:




publicclassSpringConfig {


Create a Java Bean

Let’s create a bean class and mark it with @Component annotation. This annotation specifies this class as a component. When the IOC container starts, it is scanned as a component, and a bean object is created at runtime.




publicclass Employee {
    private String name;

    public String getName() {

    publicvoidsetName(String name) { = name;

    publicintgetId() {

    publicvoidsetId(intid) { = id;

Creating Main Class

Now, create a main class to read the bean properties. Here, we did not change anything. The code is similar to the previous one. It reads the bean by using the getBean() method. The setters and getters are used to set and get bean properties.

Note: The bean ID, passed into the getBean(), should be the same as declared in the @Component annotation.


import org.springframework.context.annotation.AnnotationConfigApplicationContext;

publicclassSpringApp {
    publicstaticvoid main(String[] args) {

        AnnotationConfigApplicationContextac = newAnnotationConfigApplicationContext(SpringConfig.class);
        Employee emp = (Employee) ac.getBean("emp");
        String name = emp.getName();
        System.out.println("Name: "+name);

Project Structure

After creating all of these files, the project should look like the following:

Run the Application

Now, let’s run the application by right-clicking on the project and selecting:

Run As -> Java Application

This will produce the following output to the console tab:


This article showed how to configure an application using Java annotations. We discussed creating a Java bean, a main class, a project structure, and how to run the application. With this article, you will be able to spring project configuration using Java annotations with ease. For more helpful articles, check out more from Linux Hint.

About the author

Irfan Khan

I’m a software programmer having more than 5 years of development experience in Java and related technology. I love to write technical content as well and love to share the technical knowledge to make it available for all.