-
Notifications
You must be signed in to change notification settings - Fork 6.1k
AuthenticationConfiguration respects primary beans #6035
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
Conversation
Assert.isTrue(primaryBeanNames.size() == 1, () -> "Found " + primaryBeanNames.size() | ||
+ " beans for type " + interfaceName + " marked as primary"); | ||
beanName = primaryBeanNames.get(0); | ||
} else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fails if the result is 0 bean names
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe that is checked at line 152 - if there are 0 bean names, we short circuit and return null
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks you are correct.
lazyTargetSource.setTargetBeanName(beanNamesForType[0]); | ||
String beanName; | ||
if (beanNamesForType.length > 1) { | ||
List<String> primaryBeanNames = Arrays.stream(beanNamesForType) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be able to be simplified to something like:
applicationContext.getAutowireCapableBeanFactory().resolveNamedBean(Foo.class);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @rwinch, would that replace whole BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, interfaceName);
part? If I understand correctly, since the implementation of resolveNamedBean
handles primary, this method could just become something along the lines of
LazyInitTargetSource lazyTargetSource = new LazyInitTargetSource();
String beanName;
try {
beanName = applicationContext.getAutowireCapableBeanFactory().resolveNamedBean(interfaceName).getBeanName();
} catch (NoSuchBeanDefinitionException ex) {
return null;
}
lazyTargetSource.setTargetBeanName(beanName);
lazyTargetSource.setBeanFactory(applicationContext);
ProxyFactoryBean proxyFactory = new ProxyFactoryBean();
proxyFactory = objectPostProcessor.postProcess(proxyFactory);
proxyFactory.setTargetSource(lazyTargetSource);
return (T) proxyFactory.getObject();
However, is this method still lazy if we do that? It looks to me like resolveNamedBean
will init an instance of the bean.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for pointing that out. I missed that it did create the bean too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the PR! Please see my inline comments for requested changes.
Resolves #3912 .
I know that @shazin created #4136 as well, however it is now nearly two years old and full of conflicts.