Java

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

<projectxmlns="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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.linuxhint</groupId>

<artifactId>springapp</artifactId>

<version>0.0.1-SNAPSHOT</version>

<name>springapp</name>

<description>a simple spring app</description>

<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>

<properties>

<spring.version>5.2.8.RELEASE</spring.version>

</properties>

</project>

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 SpringConfig.java class that looks like the following:

// SpringConfig.java

packagecom.linuxhint;

importorg.springframework.context.annotation.ComponentScan;
importorg.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan("com.linuxhint")
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.

// employee.java

packagecom.linuxhint.beans;

importorg.springframework.stereotype.Component;

@Component("emp")
publicclass Employee {
   
    privateintid;
    private String name;


    public String getName() {
        returnname;
    }

    publicvoidsetName(String name) {
        this.name = name;
    }

    publicintgetId() {
        returnid;
    }

    publicvoidsetId(intid) {
        this.id = 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.

// SpringApp.java

packagecom.linuxhint;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
importcom.linuxhint.beans.Employee;

publicclassSpringApp {
    publicstaticvoid main(String[] args) {

        AnnotationConfigApplicationContextac = newAnnotationConfigApplicationContext(SpringConfig.class);
        Employee emp = (Employee) ac.getBean("emp");
        emp.setName("Rohan");
        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:

Conclusion

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.