Annotation DI Spring 14
Annotation DI Spring 14
Key Points:
● Purpose: Primarily used for assigning default values to instance variables and method
parameters.
● Functionality: Also allows developers to inject values from property files into class
members.
In Summary:
The @Value annotation is a versatile tool for setting default values and injecting external
properties into Spring beans.
Key Points:
● Purpose: Primarily used for assigning default values to instance variables and method
parameters.
● Functionality: Enables developers to inject values from property files into class
members.
In Summary:
The @Value annotation is a versatile tool for setting default values and injecting external
properties into Spring beans.
Important Points:
Key Points:
1. Method Invocation: Spring will automatically call a method annotated with @Value after
creating the bean.
2. Method Name: The method name can be any valid name, not necessarily a setter
method.
Example:
Java
@Component
public class Student {
public Student() {
System.out.println("Creating Student");
}
@Value("25")
public void setRoll(int roll) {
this.roll = roll;
System.out.println("Setter for roll called");
}
Explanation:
In this example, the setRoll method is annotated with @Value("25"). When the Student
bean is created, Spring will automatically call the setRoll method and assign the value "25" to
the roll instance variable.
Additional Notes:
● You can use @Value with any method, not just setters.
● You can use expressions or SpEL (Spring Expression Language) within the @Value
annotation.
● You can inject values from property files using @Value("${property.name}").
This means that @Value ensures automatic injection and invocation of the setter, while its
absence prevents automatic initialization.
Using @Value With Constructor in Spring
Key Points:
In Summary:
1.
2. Example:
○
In your Java Component class:
java
Copy code
@Value("${student.name}")
private String studentName;
1. Purpose:
The @PropertySource annotation is used to specify the location of the properties file
containing key-value pairs that you want to inject into Spring beans using the @Value
annotation.
2. Where to Use @PropertySource:
○ The @PropertySource annotation is applied in the @Configuration class.
Syntax:
java
Copy code
@PropertySource("classpath:application.properties")
public class AppConfig {
// Bean definitions and configuration settings
}
3.
properties
Copy code
student.name=Rahul
student.roll=101
java
Copy code
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
@Configuration
@ComponentScan(basePackages = "com.example")
@PropertySource("classpath:application.properties")
public class AppConfig {
// Configuration settings and beans
}
java
Copy code
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class Student {
@Value("${student.name}")
private String name;
@Value("${student.roll}")
private int roll;
Explanation:
To avoid exceptions for primitive types, use a default value in the @Value annotation to handle
missing properties gracefully:
java
Copy code
@Value("${student.roll:0}") // 0 as the default value
For String types, you can either allow null or provide a default value:
java
Copy code
@Value("${student.name:Unknown}") // "Unknown" as the default value
Summary:
● Primitive types require values in the properties file, or a default value should be
provided.
● String types can handle null values if a property is missing, and it’s best to provide a
default value when needed.
Handling Missing Property Values in Spring with
Default Values
To handle missing or commented-out properties in Spring, we can provide default values using
the @Value annotation. This ensures that the application doesn't throw an exception when a
property is missing or commented out in the .properties file.
Example Solution:
java
Copy code
public Student(@Value("${student.roll}") int roll,
@Value("${student.name:Guest}") String name) {
this.roll = roll;
this.name = name;
System.out.println("Student constructor called!");
}
Key Points:
○
2. String Types (String):
○ In this example, the student.name property uses
@Value("${student.name:Guest}"), meaning that if student.name is
missing or commented out in the .properties file, the default value "Guest"
will be used.
○ This helps avoid any issues with missing String properties.
Summary:
● Primitive types must have values defined in the properties file, or you need to provide a
default value to avoid exceptions.
● String types can handle missing values by using the @Value annotation with a default
value, ensuring the program runs smoothly even if a property is missing.