2

How to Configure Dependency Injection in a Library Project?

Let me illustrate this question with the following example.

Maven Library Project

ReservationAPI

com.example.reservation-api

This project contains a convenience class called ReservationApiClient which uses a RestTemplate (from the Spring Framework) for making HTTP calls.

Is it possible to make the RestTemplate field @Autowired in this library project instead of instantiating it myself?

Maven Executable Project

org.company.application

This project is a Spring Boot application and uses the above ReservationAPI as a dependency. This app will create a @Bean for the convenience class ReservationApiClient contained in that library and will then execute its public methods which in turn make HTTP requests.

What is a good strategy and/or best practices for the scenario described above?

1 Answer 1

2

You can do this if you include autowiring in your library project although that means it would always need to be used with a Spring application context to get the value unless you also have getter/setter methods to use as well. However, I don't think using RestTemplate as an autowired object makes sense since there is nothing specific about a RestTemplate and unless you name the beans there is only one bean definition for a class. All of the methods for the RestTemplate require the URI there anyhow. So in this case I would just use the bean for your ReservationApiClient in your application.

One other way to do it is if you want to include Spring dependencies in your library (which I guess you already are by using RestTemplate) you can declare your ReservationApiClient as a @Service or @Component and then use the @ComponentScan annotation in your main Spring Boot project to search that library for components to include in the bean registry.

Another option is to use a feature like Spring Boot's Autoconfigure to create factories that use third party libraries and configure them per properties in your application settings. The auto configuration documentation would be a good place to start with this. You can see the starter projects they have on GitHub and then the associated Autoconfigure classes they have associated with these.

Let me know if any of this does not make sense.

Sign up to request clarification or add additional context in comments.

3 Comments

I chose a simple example just to make the question easier to read but RestTemplate does have a use case for being dependency injected. Setting properties on a RestTemplate such as TimeOut, etc. and then sharing that same object throughout the library as a singleton is why I like using the Spring Framework. But I guess you're right that it would need an application context which isn't the case for a non-executable library project.
Your 2nd suggestion using component scanning could work in this simple example, but what if the project was opensource? It would require the executing application to use the Spring Framework, which is a big constraint to make for a user of a library.
Right if you are using a library that does not include Spring framework you will want to declare beans in the application framework if they are simple. Otherwise you will probably want to create factories as the factories should be able to do other configuration. I think this Spring Boot feature for autoconfigure docs.spring.io/spring-boot/docs/current/reference/html/… would be what you are looking for to configure things the way you want using properties from your main application.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.