Via @maciejwalkowiak and https://github.com/spring-projects/spring-boot/issues/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:
```java @Configuration public class AppConfig {
@Bean public MyService myService() { return new MyServiceImpl(); }
} ```
The preceding AppConfig class is equivalent to the following Spring
XML:
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.
Comment From: jhoeller
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.