Can not inject config param in constructor
See original GitHub issueWhen I try to inject a param in the constructor of my class using the ConfigParam
decorator on the function argument and the Configurable
decorator in the function, as suggested by the docs, TypeScript is throwing an error on the Configurable
decorator.
This is my code:
@Configurable()
public constructor(@ConfigParam('app.env') private readonly env: string) { }
And this is the error: TS1206: Decorators are not valid here
.
I managed to make it work with this:
private readonly env: string;
public constructor(private readonly config: ConfigService) {
this.env = config.get('app.env');
}
but that defeats completly the purpose of the decorators.
Am I doing something wrong?
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (1 by maintainers)
Top Results From Across the Web
Can not inject config param in constructor · Issue #30 - GitHub
When I try to inject a param in the constructor of my class using the ConfigParam decorator on the function argument and the...
Read more >Constructor Injection using Parameter - java - Stack Overflow
The current logic is working fine now. Please suggest if this implementation is not good or will cause issue anywhere. Will it set...
Read more >Constructor Dependency Injection in Spring - Baeldung
This quick tutorial will explore a specific type of DI technique within Spring called Constructor-Based Dependency Injection, which simply ...
Read more >Assisted Injection - Dagger
Assisted injection is a dependency injection (DI) pattern that is used to ... This is true even if the constructor does not contain...
Read more >3.4.1.1 Constructor-based dependency injection - Spring
One possible solution is to edit the source code of some classes to be configured by setters rather than constructors. Alternatively, avoid constructor ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t think you can apply a method decorator on the constructor? We could develop a class decorator but this would also defeat the purpose as we’d override the constructor method to inject the ConfigService. Where are you using the configured variable?
It might be possible to use the static method on the ConfigService within your constructor.
Else I would use the
Configurable
decorator on the class’s methods where you would use the variable. Or another method would be to use a factoryNot really what you want though is it? But doesn’t inject into your provider.
Hello from the future !!
I’m stuck at this. was there any workaround for this ?