Just pass the beans you need as a parameter to the method.
@Bean
public UserService getUserService() {
return new InMemoryUserService();
}
@Bean
public BookingService getBookingService(UserService userServ) {
return new InMemoryBookingServiceImpl(userServ);
}
Here when Spring gets to the getBookingService
it will see that it requires a bean of type UserService
and will look for one in the context.
See the docs
All the dependency injection rules apply. Like if no bean of that type is found an exception is thrown, or if more that one bean of that type is found you have to use @Qualifier
to specify a name of the bean that you want, or mark one of the beans with @Primary
Another option is to directly use the the method that produces the dependency bean:
@Bean
public UserService getUserService() {
return new InMemoryUserService();
}
@Bean
public BookingService getBookingService() {
return new InMemoryBookingServiceImpl(getUserService());
}