-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Description
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.