Can I call a class in parent module from dependency module in spring boot.Say I have a project A springboot application and project B is dependency (common module ) .I am able to Autowire and use classes of B from A . But the vice versa is not happening. B is dependency so no yml is configured nor version is given .When I try to say Autowire aclass it gives me nulll.
-
can you give a more concrete example of what you want to do? In general it is a bad idea to have module A depend on B and B depend on A. In that case it is more wise to just use one module that contains the functionality of both or change your interfaces so that B doesnt rely on A. – benebo22 Jun 10 '21 at 13:46
3 Answers
You can use Spring bean configurations:
For example, you have a class BClass
from your project B
(not a Spring boot project), and you need to inject BClass
in your Spring boot project A
, you create a Spring configuration class/
@Configuration
public class AConfiguration {
@Bean
public BClass configureBClass() {
return new BClass();
}
}
Then you can inject BClass
in your Spring boot project:
@Autowired
BClass bClass;
Don't forget to add project B as a dependency, and you should create AConfiguration
class under your Spring boot main package.

- 2,322
- 1
- 12
- 26
-
I have to do vice versa... Inject A project class to B project B class . – Kittu Karan Jun 10 '21 at 14:57
-
If B is a Spring boot project, you can do the same as mentioned. It's not recommended though, you will get a circular dependency. – Ismail Jun 10 '21 at 15:04
If you mean that A has implementation of interfaces from B and you define your interfaces in module B, spring can autowire implementations from A in components contained in B:
//contained in A
@Component
class BookRetrievalServiceImpl implements BookRetrievalService {
@Override
findBookById(String id){
//TODO do something
}
}
//contained in B
interface BookRetrievalService {
Book findBookById(String id);
}
//contained in B
@Component
class BookBillingService {
private BookRetrievalService bookRetrievalService;
// BookRetrievalServiceImpl gets autowired here.
public BookBillingService(BookRetrievalService bookRetrievalService){
this.bookRetrievalService = bookRetrievalService;
}
public void calculateBilling(String bookId){
//TODO retrieve book and calculate...
}
}

- 241
- 2
- 5
If I understood your problem, you've two projects A and B where A is dependent on B. Technically, A has a maven dependency of B so A can access all the classes of B. But if you see, B doesn't know about A. Think like you're adding a Spring artifact in your POM to use its classes, but does it mean that Spring Artifact can also access your Classes, no, right?
Another thing, you said B is kind of common module, now if you make B dependent on A, it may not remain common.
As you've not given any concrete details, I can suggest you to try with Spring Events. With Spring Events, two Siblings can interact without having a dependency of one another. See here Spring Boot Application Events Explained

- 649
- 7
- 11