0

I am going over some of the code from the spring project that configuration classes like this:

@Configuration
@Import(value = {FhirAccessTokenServiceConfig.class, HealthDataServerServiceConfig.class})
public class FhirRouterServiceConfig {

    @Autowired
    private FhirAccessTokenService fhirAccessTokenService;
    @Autowired
    private HealthDataServerService healthDataServerService;

    @Bean
    public FhirRouterService fhirRouterService() {
        return new FhirRouterServiceImpl(fhirAccessTokenService, healthDataServerService);
    }
}

When it instantiate the bean object (FhirRouterServiceImpl in this example), it passes two service object as parameter which is autowired.

However, for some configuration classes, it is defined like this. I am going to use same class for example:

@Configuration
@Import(value = {FhirAccessTokenServiceConfig.class, HealthDataServerServiceConfig.class})
public class FhirRouterServiceConfig {

    @Bean
    public FhirRouterService fhirRouterService(final FhirAccessTokenService fhirAccessTokenService, final HealthDataServerService healthDataServerService) {
        return new FhirRouterServiceImpl(fhirAccessTokenService, healthDataServerService);
    }
}

Instead of autowiring two services, it uses parameter (final FhirAccessTokenService fhirAccessTokenService for this example) to instantiate the bean.

They both works, but I don't understand the difference and which is better way. I also found that not using @Configuration class at all could be better project structure. Can anybody explain what is happening and what is the best approach?

Jonathan Hagen
  • 580
  • 1
  • 6
  • 29

0 Answers0