Spring @Value Annotation for Injecting Values into Bean Properties

🌱 My Spring Journey : @Value Annotation in Spring This annotation is basically used to inject values into simple-type Spring bean properties. 🔹 There are different ways of injecting simple values into Spring bean properties : -> To inject direct values into Spring bean properties. -> To inject values gathered from a properties file. -> To inject system property values like os.name, os.version, etc. -> To inject environment variable values into Spring bean properties. 🔹 We can provide direct values to the @Value annotation, or we can specify a key using a placeholder @Value("${...}"). Difference between @Autowired and @Value : -> @Autowired is used to inject one Spring bean class object into a property (HAS-A relationship) of another Spring bean class. -> @Value is used to inject simple values into the properties of a Spring bean class. 🔹 Internal working : -> The moment the IOC container is created, it internally creates a built-in object called the Environment object, which dynamically collects : -> Key-value pairs defined in the configured properties file. -> Key-value pairs of fixed system properties. -> Environment variable names and their values. Later, these values are injected into Spring bean properties based on the @Value annotation. #SpringFramework #SpringBoot #Java #BackendDevelopment #DependencyInjection #LearningInPublic #MySpringJourney

@configurationproperty is more better than @value I guess

Like
Reply

To view or add a comment, sign in

Explore content categories