Skip to content

Support beanFactory.getBean(Bar.class) signature to instantiate a bean. [SPR-1956] #6649

Closed

Description

@spring-projects-issues

David Medinets opened SPR-1956 and commented

Given a bean definition like this:

<bean id="com.foo.BarRepository" class="com.foo.BarRepositoryJDO">
<constructor-arg><ref bean="javax.jdo.PersistenceManagerFactory" /></constructor-arg>
</bean>

If the beanFactory.getBean(Bar.class) signature were supported (ie, added to the BeanFactory interface), it would be possible to do:

BarRepository barRepository = (BarRepository)beanFactory.getBean(BarRepository.class);

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.


Issue Links:

1 votes, 1 watchers

Activity

  1. spring-projects-issues commented on Aug 22, 2008

    @spring-projects-issues
    CollaboratorAuthor

    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!

  2. spring-projects-issues commented on Apr 17, 2009

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    We will be implementing the support originally requested in this issue; put a watch on #10200 to be notified of progress.

  3. added
    type: enhancement
    in: core
    status: duplicate
    on Jan 10, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: corestatus: duplicatetype: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions