Front-End dev here working on my first Java Spring Boot API. I've been reading many articles on the "best practices" in Spring/Spring Boot and have been attempting to refactor my code to follow those practices.
Below I have an example of a generic class I use to handle all HTTP requests for my various services. Originally I had this class annotated with the @Component annontation, but as I mentioned I am trying to learn and follow Spring "best practices." In particular I am interested in implementing what this article on best practices describes (Number 3 & 4 in the article). That says one should avoid using @component, because we don't want to be tightly coupled to the Spring framework and we want to avoid "entire class path scanning."
@Slf4j
public class HttpService {
private HttpServletRequest request;
private RestTemplate restTemplate;
public HttpService(HttpServletRequest request, RestTemplateBuilder restTemplateBuilder) { ... }
public String get(String url, String id) { ... }
}
With the @component
annotation my service works as expected, but when I remove it I get the exception:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
This seems to be a pretty common exception in Java as there is a LOT of questions about it, but those solutions have not worked for me.
My question I'm hoping the community can help me answer is two part:
- How do I correctly make this a useable generic class that is not tightly coupled to Spring?
- Is this actually a correct approach for Spring development or am I reading too deeply into this article?