Skip to content

findAutowireCandidates() will either return candidates that are not selfReferenced or self referenced Collection/Map beans. Not both. #33136

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

Closed
ankitkpd opened this issue Jul 2, 2024 · 5 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid

Comments

@ankitkpd
Copy link

ankitkpd commented Jul 2, 2024

          Looks like after the fix `findAutowireCandidates()` will either return candidates that are not selfReferenced or candidates that are Collection/Map beans and are self referenced. 

But it won't return both i.e. non self referenced candidates that are being added with

for (String candidate : candidateNames) {
	if (!isSelfReference(beanName, candidate) && isAutowireCandidate(candidate, descriptor)) {
		addCandidateEntry(result, candidate, descriptor, requiredType);
	}
}

AND candidates that are Collection/Map beans & are self referenced being added with the fix i.e.

for (String candidate : candidateNames) {
	if (isSelfReference(beanName, candidate) &&
			(!(descriptor instanceof MultiElementDescriptor) || !beanName.equals(candidate)) &&
			isAutowireCandidate(candidate, fallbackDescriptor)) {
		addCandidateEntry(result, candidate, descriptor, requiredType);
	}
}

Because if condition if (result.isEmpty()) would prevent it to do so.

Is there a way to get autowired candidates of both types?

Originally posted by @ankitkpd in #13096 (comment)

@ankitkpd ankitkpd changed the title Looks like after the fix findAutowireCandidates() will either return candidates that are not selfReferenced or candidates that are Collection/Map beans and are self referenced. Not both. findAutowireCandidates() will either return candidates that are not selfReferenced or self referenced Collection/Map beans. Not both. Jul 2, 2024
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Jul 2, 2024
@snicoll
Copy link
Member

snicoll commented Jul 3, 2024

@ankitkpd rather than an analysis of our own code, please explain what you're trying to do. To speed up support, you can share a sample we can actually run to reproduce that behavior ourselves (no code in text please).

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Jul 3, 2024
@jhoeller jhoeller added the in: core Issues in core modules (aop, beans, core, context, expression) label Jul 4, 2024
@spring-projects-issues
Copy link
Collaborator

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Jul 10, 2024
@spring-projects-issues
Copy link
Collaborator

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spring-projects-issues spring-projects-issues closed this as not planned Won't fix, can't repro, duplicate, stale Jul 17, 2024
@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged or decided on labels Jul 17, 2024
@ankitkpd
Copy link
Author

@snicoll within my system I have a base class for all spring configurations that holds a few autowired list for e.g.

@Configuration
public class BaseSharedSpringConfiguration {

  @Autowired(required = false)
  public List<Foo> fooList;

  @Bean
  public Foo sharedFoo() {
    return new BaseSharedFoo();
  }
  
}

Then there two separate codebases & spring boot apps with their own spring configurations. One extends BaseSharedSpringConfiguration & Other do not for e.g.

@Configuration
public class OneSpringConfiguration extends BaseSharedSpringConfiguration {

  @Bean
  public Foo oneFoo() {
    return new OneTypeOfFoo();
  }
  
}
@Configuration
public class OtherSpringConfiguration {

  @Autowired
  BaseSharedSpringConfiguration baseSharedConfig;

  @Bean
  public Foo otherFoo() {
    return new OtherTypeOfFoo();
  }
  
}

With these spring configurations setup sharedFoo & oneFoo are not getting added to the autowired fooList.

With little bit of debugging into DefaultListableBeanFactory.findAutowireCandidates method it was realized that
it's because sharedFoo & oneFoo are considered as self referenced beans & otherFoo is not.

Thanks for your support.

@snicoll
Copy link
Member

snicoll commented Jul 17, 2024

With these spring configurations setup sharedFoo & oneFoo are not getting added to the autowired fooList.

You need to make the injection of the list lazy (or not using field injection) as you're trying in the same arrangement to contribute one instance and inject the list. If you have further questions, please follow-up on StackOverflow.

@snicoll snicoll added the status: invalid An issue that we don't feel is valid label Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

4 participants