poodinis icon indicating copy to clipboard operation
poodinis copied to clipboard

Avoiding @Value Initialization for Existing Instances

Open mbierlee opened this issue 2 years ago • 0 comments

Discussed in https://github.com/mbierlee/poodinis/discussions/44

Originally posted by vnayar November 3, 2023 For context, when autowiring an instance that has already been pre-defined, the system avoids @Autowire for values that have already been set. This is very useful when adding fake components during testing.

This is seen in the test here: https://github.com/mbierlee/poodinis/blob/87c036d543156b423ab09c65aa0fdd71e12aa52e/test/poodinis/containertest.d#L148

However, the same behavior is not seen for members that have the @Value annotation. Those values get re-initialized. Thus, if you have an autowired component, such as "MyAppConfig" or something like that with many @Value properties, and then I try to inject an existing instance in a test, e.g. container.register!MyAppConfig.existingInstance(testConfig);, when the rest of the code tries to resolve that dependency, the @Value annotations will be processed again, and replace any values set for the test.

Is this a bug, or is it intended behavior with a known workaround? Can you disable re-evaluation of @Value annotations for existing instances that have been plugged into the DependencyContainer?

mbierlee avatar Nov 05 '23 12:11 mbierlee