-
Notifications
You must be signed in to change notification settings - Fork 37.7k
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
Support BeanFactory#getBean(Class<T> requiredType) [SPR-5529] #10200
Comments
Chris Beams commented getBean(Class<T> requiredType) will also need to account for excluding any beans marked as 'primary' from the collection of matching beans. |
Chris Beams commented I suppose we'll need to consider providing variants of
I'm not necessarily advocating that we do, but it should be considered and discussed nonetheless. |
Chris Beams commented Suggested JavaDoc:
|
Chris Beams commented A side note: the 'may be shared or independent' language found throughout the JavaDoc in BeanFactory seems to me to be antiquated. I assume this was an early way of referring to singleton and prototype scopes respectively? Let's consider revising/eliminating that phrase altogether. |
Juergen Hoeller commented We have BeanFactoryUtils.beanOfType(ListableBeanFactory, Class) which is pretty similar. This is the only such method in BeanFactoryUtils that does not simply provide ancestor-traversal behavior on top of ListableBeanFactory... so I suppose it's worth having in the core interfaces. I would add it to ListableBeanFactory, though, since it implies some level of bean definition traversal and matching. We could also call it getBeanOfType, which would nicely match the existing getBeansOfType method in ListableBeanFactory. Juergen |
Grzegorz Borkowski commented Though not directly, but somehow related to it is my request #9787. Perhaps it is also worth considering as candidate to inclusion in Spring 3.0. |
Chris Beams commented Note that I've just committed the initial revision of |
Chris Beams opened SPR-5529 and commented
In alignment with Java 5 style and migration of JavaConfig features into core, support the following signature on the BeanFactory interface:
This will need to be implemented by the following classes:
AbstractBeanFactory
SimpleJndiBeanFactory
StaticListableBeanFactory
AbstractApplicationContext
There are also several locations throughout the Spring test codebase that call getBean(null). These calls will have to be disambiguated as getBean((String)null).
With the above in mind, the addition of getBean(Class<T>) does introduce a minor backward compatibility issue for anyone calling getBean(null). Given that this is extremely unlikely (because it's meaningless to do so), it's probably not a significant concern.
Implementation approach:
Apply logic similar to that in getBeansOfType(), iterating through all beans, building up a collection of beans that match the given 'requiredType' parameter. If the resulting list contains exactly one bean, return it. If the list contains zero beans, throw a NoSuchBeanDefinitionException. If the list contains more than one matching bean, throw a BeansException specific to the issue. No suitable BeansException currently exists for this, so a suggestion would be 'AmbiguousBeanLookupException extends BeansException' with an appropriate error message, something to the effect of: "3 beans match requested type com.acme.Foo. Consider using getBean(String, Class<T>) to disambiguate. Matching bean names are: ['foo1', 'foo2', 'foo3']
Qualified access by type:
Currently we're supporting <T> T getBean(String, Class<T>). This is a qualification-by-bean-name scenario. We may also want to support qualification-by-
@Qualifier
. Haven't given much thought to this, but something to the effect of:<T> T getBean(Class<? extends Annotation> qualifier, Class<T> requiredType)
Such that a client can define two classes of the same supertype:
@Production
@Service
public XyzCustomerService implements CustomerService { ... }
@Testing
@Service
public TestCustomerService implements CustomerService { ... }
Where
@Production
and@Testing
are both meta-annotated as@Qualifier
The user can then register bean definitions for both classes and access the production instance as follows:
ApplicationContext ctx = ...
CustomerService productionInstance = ctx.getBean(Production.class, CustomerService.class);
The latter portion of this issue may well be moved to an issue all its own. Just including it here for completeness & concision.
Issue Links:
@Configuration
classesThe text was updated successfully, but these errors were encountered: