You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support beanFactory.getBean(Bar.class) signature to instantiate a bean. [SPR-1956] #6649
This syntax is better than getBean("barRepository") because the Java compiler verifies the class name is spelled correctly. While advanced IDEs (ie, Eclipse) support does handle class name verification, not all people will be using such an IDE.
It may or may not be a good idea to require the Class to actually be an interface to ensure that 'best practices' are followed.
This is quite old issue, but I think it is worth considering especially after releasing 2.5 and in the light of JavaConfig.
I'm saying it can be useful after 2.5, becuase now it is possible to define all application beans using stereotype annotations, only leaving some infrastructure definitions to XML. However, in such a case the standard procedure of creating ApplicationCOntest and getting the bean by its name is cumbersome: it is possible that this bean has no explicit name, because it was not defined in XML. In such a case it would be better to be able to get such bean from context by its type.
The JavaConfigApplicationContext class has a method:
T getBean(Class<T> type)
So it would be better to extract this method to some interface, maybe already existing (ListableBeanFactory?), and enhance all implementations, so that it is present in e.g. XmlApplicationContext too.
The methods can be:
T getBean(Class<T> type) - return bean if there is exactly one bean of this type in context, if there is more throw exception, in zero then return null or throw exception (I'm not sure which would be more natural and consistent with other methods)
Collection<T> getBeans(Class<t> type) - return all (zero or more) beans of given type
and maybe also variations, taking into account only beans annotated with some "Qualifier" (I'm not sure about proper syntax for this):
At this momement, if I want to get the bean of given type from context, and I know there is only one, I have three choices:
context.getBeansOfType(MyType.class).values().iterator().next(); // awkward
context.getBean(context.getBeanNamesForType(MyType.class)); // verbose
BeanFactoryUtils.beanOfType(context., MyType.class); // still verbose, and not well-known
//compare with desired one:
context.getBean(MyType.class); // i like it!
Activity
spring-projects-issues commentedon Aug 22, 2008
Grzegorz Borkowski commented
This is quite old issue, but I think it is worth considering especially after releasing 2.5 and in the light of JavaConfig.
I'm saying it can be useful after 2.5, becuase now it is possible to define all application beans using stereotype annotations, only leaving some infrastructure definitions to XML. However, in such a case the standard procedure of creating ApplicationCOntest and getting the bean by its name is cumbersome: it is possible that this bean has no explicit name, because it was not defined in XML. In such a case it would be better to be able to get such bean from context by its type.
The JavaConfigApplicationContext class has a method:
T getBean(Class<T> type)
So it would be better to extract this method to some interface, maybe already existing (ListableBeanFactory?), and enhance all implementations, so that it is present in e.g. XmlApplicationContext too.
The methods can be:
T getBean(Class<T> type) - return bean if there is exactly one bean of this type in context, if there is more throw exception, in zero then return null or throw exception (I'm not sure which would be more natural and consistent with other methods)
Collection<T> getBeans(Class<t> type) - return all (zero or more) beans of given type
and maybe also variations, taking into account only beans annotated with some "Qualifier" (I'm not sure about proper syntax for this):
T getBean(Class<T> type, Class<? extends Annotation> annotatedWith)
Collection<T> getBeans(Class<t> type, Class<? extends Annotation> annotatedWith)
At this momement, if I want to get the bean of given type from context, and I know there is only one, I have three choices:
context.getBeansOfType(MyType.class).values().iterator().next(); // awkward
context.getBean(context.getBeanNamesForType(MyType.class)); // verbose
BeanFactoryUtils.beanOfType(context., MyType.class); // still verbose, and not well-known
//compare with desired one:
context.getBean(MyType.class); // i like it!
spring-projects-issues commentedon Apr 17, 2009
Chris Beams commented
We will be implementing the support originally requested in this issue; put a watch on #10200 to be notified of progress.