Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoiding @Value Initialization for Existing Instances #45

Open
mbierlee opened this issue Nov 5, 2023 Discussed in #44 · 0 comments
Open

Avoiding @Value Initialization for Existing Instances #45

mbierlee opened this issue Nov 5, 2023 Discussed in #44 · 0 comments
Assignees
Labels

Comments

@mbierlee
Copy link
Owner

mbierlee commented Nov 5, 2023

Discussed in #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:

// Test autowiring does not autowire member where instance is non-null

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 mbierlee self-assigned this Nov 5, 2023
@mbierlee mbierlee added the bug label Nov 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant