Example project to show the various ways we have to inject packages in the context of springboot
In Spring Boot, the @Autowired annotation is used for dependency injection. When placed on a field, a setter method, or a constructor, Spring will automatically search for an instance of the corresponding class and inject it into that field, method, or constructor during bean initialization.
@Service
public class AutowiredService {
@Autowired
public Repository repository;
}
@Service
public class CreateInstanceService {
private final Repository repository;
public CreateInstanceService() {
this.repository = new Repository();
}
}
@Service
@RequiredArgsConstructor
public class FinalService {
private final Repository repository;
}
When we load a list of interfaces, what we will get is a list of all the implementations created for that interface
@Service
@RequiredArgsConstructor
public class MainInterfaceService {
private final List<ServiceInterface> services;
}
In this case,
OptionalService
is conditioned to a propertyservice.optional.enabled
indicating that a class must be loaded, otherwise, the context would not have the class loaded and would be null.
@Service
@RequiredArgsConstructor
public class MainOptionalService {
private Optional<OptionalService> optionalService;
@Autowired
public MainOptionalService(final Optional<OptionalService> optionalService) {
this.optionalService = optionalService;
}
}
@ConditionalOnProperty(name = "service.optional.enabled", havingValue = "true")
public class OptionalService {
...
}