I want to introduce CDI to existing project. Currently we are creating new objects inside for loop like
Context context;
for(String string : strings) {
context = new Context();
// do some operations with string.
// set outcomes of above operation in context.
}
Now if I am injecting Context
as
@Inject Context context;
I'm getting same instance every time.
I'm wondering if there is any way available in CDI to create new Context
inside loop?
As long as you do not use special scopes, you can use
@Inject
Instance<B> bProvider;
...
B b = bProvider.get();
and you will get a new "B" everytime. Check out this gist.