Spring Boot @Scope Example

1. Introduction

The @Value annotation in Spring Boot is commonly used for injecting values into fields in Spring-managed beans. Whether the values come from a properties file, system properties, or have default literals, @Value makes accessing these values straightforward.

Key Points:

1. @Value is used to inject externalized values into Spring beans, such as from a properties file.

2. It supports SpEL (Spring Expression Language), which allows for more complex expressions within the annotation.

3. The annotation can be used on fields, methods, and method/constructor parameters.

2. Implementation Steps

1. Define a property in the application.properties file or another externalized configuration.

2. Use the @Value annotation in your class to inject the defined property.

3. Optionally, use @Value with SpEL to compute values dynamically.

4. Create a bean or component in which you will inject the value.

5. Run the Spring Boot application and observe the injected value.

3. Implementation Example

Here is the complete code that demonstrates the usage of @Scope annotation:
// Step 1: Define a property in application.properties (usually located in src/main/resources)
# application.properties
welcome.message=Hello from @Value

// Step 2: Create a component class that uses the @Value annotation to inject a property
@Component
public class WelcomeComponent {

    // Injecting the property into a field with @Value
    @Value("${welcome.message}")
    private String welcomeMessage;

    public void printWelcomeMessage() {
        // This method will print the injected message
        System.out.println(welcomeMessage);
    }
}

// Step 4: Create the main application class
@SpringBootApplication
public class ValueAnnotationExampleApplication implements CommandLineRunner {

    @Autowired
    private WelcomeComponent welcomeComponent;

    public static void main(String[] args) {
        SpringApplication.run(ValueAnnotationExampleApplication.class, args);
    }

    @Override
    public void run(String... args) {
        // Step 5: Invoke the method to print the welcome message
        welcomeComponent.printWelcomeMessage();
    }
}

Output:

Hello from @Value

Explanation:

1. application.properties contains a property welcome.message which holds the string to be injected.

2. @Value("${welcome.message}") is used to inject the value of welcome.message from the properties file into the welcomeMessage field of WelcomeComponent.

3. WelcomeComponent has a method printWelcomeMessage that prints the value of the welcomeMessage field to the console.

4. ValueAnnotationExampleApplication class is annotated with @SpringBootApplication, making it the entry point for the Spring Boot application.

5. The run method of ValueAnnotationExampleApplication is executed after the application context is started, invoking printWelcomeMessage on WelcomeComponent.

6. The output is "Hello from @Value", which is the value defined in application.properties and injected into WelcomeComponent via the @Value annotation.


Comments