How can I inject a property value into a Spring Bean which was configured using annotations?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
In the world of Java development, Spring Framework has revolutionized how developers write and organize their code, majorly simplifying the deployment and management of enterprise-grade applications. One of its core features is dependency injection (DI), which allows for loose coupling between components. When using annotations to configure Spring Beans, injecting property values becomes streamlined and efficient.
Understanding Dependency Injection in Spring
Dependency Injection in Spring is performed primarily through either field injection, setter injection, or constructor injection. Spring's @Autowired annotation can autowire bean dependencies. However, to directly inject values from property files or other sources, Spring provides the @Value annotation.
Using the @Value Annotation
The @Value annotation is used for injecting values into Spring Beans, and it can be placed on fields, methods, or method/constructor parameters. It supports SpEL (Spring Expression Language), which adds powerful querying capabilities.
Basic Property Injection
Start by declaring property sources in your Spring configuration. If using a properties file named app.properties, you might have something like:
You can then use @Value to inject these properties into your beans:
Here, Spring resolves ${app.host} and ${app.port} using the properties defined in app.properties.
Advanced Scenarios and SpEL
Spring Expression Language (SpEL) provides a powerful way to interact with bean properties dynamically. For instance, if you want to calculate a property value or refer to another bean, SpEL will be quite handy:
In this example, Spring injects the system property user.region if available; otherwise, it defaults to 'US'.
Applying Profiles with Conditional Property Injection
Spring Profiles provide a way to segregate parts of the application configuration and make it available only in certain environments. You can use the @Profile annotation to specify which profiles a bean belongs to, and use conditional property values with profiles as follows:
This will inject the development database URL only when the 'development' profile is active.
Using PropertyPlaceholderConfigurer or @PropertySource
Alternatively, Spring offers other ways to manage properties:
- PropertyPlaceholderConfigurer: This allows for customization of property resolution, including properties from different sources and fallback values.
- @PropertySource: Annotate your configuration classes to define which property files to load:
Troubleshooting Common Issues
Injecting properties might not always go as expected. Common issues include:
- Properties not found: Ensure the properties file is correctly located and spelled in
@PropertySource. - Incorrect SpEL syntax: Validate expressions used in
@Value. - Type mismatches: Ensure that the property type in the file matches the field type in your bean.
Summary Table
| Feature | Annotation | Description |
| Basic Injection | @Value | Injects literal values, properties, or expressions into beans. |
| Expression Language | @Value with SpEL | Allows conditional logic, dynamic values, and referencing other beans. |
| Environmental Conditions | @Profile | Conditions bean creation based on active profiles, useful for different environments. |
| External Properties | @PropertySource | Specifies external properties files to include in the Spring Context. |
Utilizing these methods to inject property values into Spring Beans using annotations not only makes the code cleaner but also enhances its flexibility and maintainability.

