Swiftorial Logo
Home
Swift Lessons
Tutorials
Learn More
Career
Resources

Spring Annotation-based Configuration

Spring's annotation-based configuration allows developers to configure the Spring container using annotations on Java classes and methods. This approach enhances readability and reduces the need for XML configuration files. This overview covers the key concepts and usage of annotation-based configuration in Spring.

Key Concepts of Annotation-based Configuration

  • @Component: Indicates that a class is a Spring-managed component.
  • @Service: Specialization of @Component, used to denote a service layer component.
  • @Repository: Specialization of @Component, used to denote a data access layer component.
  • @Controller: Specialization of @Component, used to denote a Spring MVC controller.
  • @Autowired: Marks a dependency to be injected by Spring's dependency injection.
  • @Value: Injects property values from external configuration.

@Component, @Service, @Repository, and @Controller Annotations

The @Component annotation indicates that a class is a Spring-managed component. @Service, @Repository, and @Controller are specializations of @Component, used to indicate service layer, data access layer, and Spring MVC controller components, respectively. Here are examples:

MyComponent.java

// MyComponent.java
package com.example.springannotationconfig;

import org.springframework.stereotype.Component;

@Component
public class MyComponent {
    public void performTask() {
        System.out.println("Performing a task in MyComponent.");
    }
}

MyService.java

// MyService.java
package com.example.springannotationconfig;

import org.springframework.stereotype.Service;

@Service
public class MyService {
    public void performService() {
        System.out.println("Performing service in MyService.");
    }
}

MyRepository.java

// MyRepository.java
package com.example.springannotationconfig;

import org.springframework.stereotype.Repository;

@Repository
public class MyRepository {
    public void performDatabaseOperation() {
        System.out.println("Performing database operation in MyRepository.");
    }
}

MyController.java

// MyController.java
package com.example.springannotationconfig;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class MyController {
    @GetMapping("/hello")
    @ResponseBody
    public String sayHello() {
        return "Hello from MyController!";
    }
}

@Autowired Annotation

The @Autowired annotation is used to mark a field, constructor, or setter method for dependency injection. Here are examples of each usage:

Field Injection

// MyService.java
package com.example.springannotationconfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyService {
    @Autowired
    private MyRepository myRepository;

    public void performService() {
        myRepository.performDatabaseOperation();
        System.out.println("Performing service in MyService.");
    }
}

Constructor Injection

// MyService.java
package com.example.springannotationconfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyService {
    private final MyRepository myRepository;

    @Autowired
    public MyService(MyRepository myRepository) {
        this.myRepository = myRepository;
    }

    public void performService() {
        myRepository.performDatabaseOperation();
        System.out.println("Performing service in MyService.");
    }
}

Setter Injection

// MyService.java
package com.example.springannotationconfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyService {
    private MyRepository myRepository;

    @Autowired
    public void setMyRepository(MyRepository myRepository) {
        this.myRepository = myRepository;
    }

    public void performService() {
        myRepository.performDatabaseOperation();
        System.out.println("Performing service in MyService.");
    }
}

@Value Annotation

The @Value annotation is used to inject property values from external configuration. Here is an example:

Application Configuration

// AppConfig.java
package com.example.springannotationconfig;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@ComponentScan(basePackages = "com.example.springannotationconfig")
@PropertySource("classpath:application.properties")
public class AppConfig {
}

MyComponent.java

// MyComponent.java
package com.example.springannotationconfig;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {
    @Value("${property1}")
    private String property1;

    public void showProperty() {
        System.out.println("Property 1: " + property1);
    }
}

Example Properties File

// application.properties
property1=Hello from MyComponent

Main Application Class

To use annotation-based configuration, you need to create an application context using AnnotationConfigApplicationContext. Here is an example:

Main Application Class

// SpringAnnotationConfigApplication.java
package com.example.springannotationconfig;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class SpringAnnotationConfigApplication {
    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        MyService myService = context.getBean(MyService.class);
        myService.performService();
    }
}

Key Points

  • @Component: Indicates a class is a Spring-managed component.
  • @Service: Specialization of @Component for service layer components.
  • @Repository: Specialization of @Component for data access layer components.
  • @Controller: Specialization of @Component for Spring MVC controller components.
  • @Autowired: Marks a dependency to be injected by Spring's dependency injection.
  • @Value: Injects property values from external configuration.

Conclusion

Spring's annotation-based configuration simplifies the process of configuring the Spring container and enhances the readability of the code. By leveraging annotations like @Component, @Service, @Repository, @Controller, @Autowired, and @Value, developers can manage bean definitions and application settings more effectively. This approach reduces the need for XML configuration and provides a more intuitive way to configure Spring applications. Happy coding!