Spring Boot with Kotlin - Value annotation not working as expected
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
When @Value appears broken in a Kotlin Spring Boot project, the root cause is usually not Kotlin itself. It is usually one of a few practical issues: the class is not Spring-managed, the property key is wrong, the ${...} expression was written incorrectly for Kotlin strings, or @Value is being used where @ConfigurationProperties would be a better fit.
Kotlin does make some of these mistakes more visible. Constructor injection, non-null types, and immutable properties expose configuration problems sooner than older Java-style field injection patterns.
Make Sure the Bean Is Actually Managed by Spring
@Value works only when Spring creates the object:
If you instantiate the class manually with ApiSettings(...), no injection happens. That is the first thing to verify before changing annotations or properties files.
Kotlin Strings Need Escaped ${...} in Annotations
This is one of the most Kotlin-specific mistakes. Because Kotlin uses $ for string interpolation, the @Value expression must escape the dollar sign:
Not this:
The second form is interpreted by Kotlin as string interpolation, not as the Spring placeholder you intended.
Prefer Constructor Injection in Kotlin
Constructor injection works naturally with immutable Kotlin code:
This is usually safer than field injection with lateinit, because the dependency is provided at construction time and the property can remain a val.
Field injection can work:
But it is easier to misuse, especially in tests and manual object creation.
Verify Property Keys and Profiles
A lot of @Value failures are simple configuration mismatches:
Then check the active profile explicitly:
If the wrong profile is active, the property may genuinely not exist in the resolved environment. Before assuming the annotation is broken, confirm:
- the property key spelling
- the active profile
- the property source that should provide the value
Use @ConfigurationProperties for Grouped Settings
If you have many related settings, multiple @Value annotations are usually the wrong abstraction. A typed properties class is clearer:
Then enable it in configuration:
This is usually easier to validate, test, and maintain than a scattering of unrelated @Value expressions.
Common Pitfalls
The biggest Kotlin-specific mistake is forgetting to escape the dollar sign in the placeholder string. If the annotation uses "${...}" instead of "\${...}", the expression is wrong before Spring even sees it.
Another common issue is putting @Value on a class that Spring does not manage. Manual instantiation bypasses the entire injection process.
Developers also use @Value on too many individual fields when the configuration really belongs in a typed properties object. That makes the code harder to read and harder to validate.
Finally, do not ignore the simple possibility of a bad property key or wrong profile. Those remain the most common failures in ordinary Spring Boot projects.
Summary
- '
@Valueworks only on Spring-managed objects.' - In Kotlin, write placeholders as
"\${...}", not"${...}". - Constructor injection is the cleanest default for Kotlin Spring code.
- Check property keys and active profiles before deeper debugging.
- Use
@ConfigurationPropertieswhen several related settings belong together.
Related reading
- Spring Boot with redirecting with single page angular2
- Spring boot with RefreshScope PostConstruct PreDestroy
- spring boot with spring security Error creating bean with name 'securityFilterChainRegistration
- Spring Boot without the web server
- Spring Data JPA How to use Kotlin nulls instead of Optional
- Sqlite File Location Core Data
- Spring Cloud AWS SQS fails to connect to service endpoint locally
- Spring Cloud Stream and Kafka Integration Error Handling

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.