Skip to content

Consistent @Bean method return type for equivalence with XML example #29338

@wilkinsona

Description

@wilkinsona

Via @maciejwalkowiak and spring-projects/spring-boot#32763, I think Framework's reference documentation on @Bean methods and the return type in their method signature could be clarified. The documentation contains the following Java config and XML examples:

@Configuration
public class AppConfig {

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}

The preceding AppConfig class is equivalent to the following Spring XML:

<beans>
    <bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>

The Java config example is depriving the bean factory of some type information. It will only know that the myService bean is a MyServiceImpl once the bean has been created. This can have an impact on injection points that consume a MyServiceImpl as injection will fail if it's attempted before myService has been created.

The XML sample is described as being equivalent to the Java config example. Strictly speaking, I don't think that's accurate. In the XML case, the bean factory will know that the bean is a MyServiceImpl from the outset and injection of a MyServiceImpl will succeed irrespective of bean creation ordering.

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: documentationA documentation task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions