Reputation: 15294
I have the following code:
class Foo {
@Resource(lookup=url)
private DataSource ds;
}
But the url
is dynamic and will be loaded inside a method body:
class Bar {
public static void main(String[] args) {
String url = xxx;
DataSource ds;
//How to inject the datasource using Annotation?
}
}
I know how to use context lookup. I wish to know if there is any annotated way to inject.
Upvotes: 3
Views: 2063
Reputation: 5682
You can use Koin service locator for that purpose. It supports local variables injection inside method.
Upvotes: 0
Reputation: 1594
Short answer is you can't. There is no way for the code that performs the injection to lookup a local variable of a method. A class field on the other hand is accessible using class inspection methods available in the core java API.
Additionally, an annotation parameter can only be a constant expression.
Upvotes: 3