Skip to content
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

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

Closed
wilkinsona opened this issue Oct 18, 2022 · 1 comment
Closed
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: documentation A documentation task
Milestone

Comments

@wilkinsona
Copy link
Member

wilkinsona commented Oct 18, 2022

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Oct 18, 2022
@sbrannen sbrannen added type: documentation A documentation task in: core Issues in core modules (aop, beans, core, context, expression) and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Jan 31, 2023
@sbrannen sbrannen added this to the 6.0.5 milestone Jan 31, 2023
@jhoeller jhoeller self-assigned this Feb 14, 2023
@jhoeller
Copy link
Contributor

The "Declaring a Bean" section does explain the impact of the return type declaration. However, the quoted example above is indeed misleading. I'll fix this to match the recommendation for full type exposure, and to actually bring it in sync with the XML variant.

@jhoeller jhoeller changed the title Clarify documentation on @Bean method return types and the impact it has on dependency injection Consistent @Bean method return type for equivalence with XML example Feb 14, 2023
@jhoeller jhoeller added the for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x label Feb 14, 2023
@github-actions github-actions bot added status: backported An issue that has been backported to maintenance branches and removed for: backport-to-5.3.x Marks an issue as a candidate for backport to 5.3.x labels Feb 14, 2023
mdeinum pushed a commit to mdeinum/spring-framework that referenced this issue Jun 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: backported An issue that has been backported to maintenance branches type: documentation A documentation task
Projects
None yet
Development

No branches or pull requests

4 participants