Optional dependency (aware of module)

I have an independent module A, and a different module B. Module A is independent and should work without module B, but whenever module B is installed with module A, I want to be able to use module B’s services and classes.

I have read in the documentation that one module can only access another module’s services if it is marked as required. However, if I do this, then my module A can’t exist independently. Will aware_of_module work? If not, is it even possible to do what I said above, or is there a workaround?

Thank you!

@zachraduban I am not exactly sure that I understood you here, but i will give it a try.

You can omit the required from your maven dependency scope, and handle the unavailability of the services programatically.

You can use @SprinngBean(required=false,value="moduleBService") in your controllers, or handle null pointers.

ModuleBService service=Context.getService(ModuleBService.class); 

    if(service!=null){
         // your code here
    }

Yes you can opt, to omit the scope required, but then you have to be extra-careful when using the beans from the module.

1 Like

That’s what aware_of_module is intended for, did you take a look at this page?

1 Like

Thank you for replying!

Yes, I already read the documentation, and it said under the Requiring Modules section that “A module can only access the api/service of another module if it is marked as required.” Hence, I assumed that aware_of_module could not do it.

But as I tried using aware_of_module, it gave me a BeanCreationException error.

https://pastebin.com/28XqbvZJ

I have configured my dependencies in root pom.xml and omod/pom.xml, and have added the aware_of_module in my config.xml file.

Am I lacking something? Or does aware_of_module not really work in these types of situations?

Thanks for the reply!

I also use programmatic checking if a certain module is installed before I start using a service method from it, however, I still need the dependency for my module A to be able to use methods/service from module B.

Thank you for all the replies. I used aware_of_module and it worked. I just wasn’t aware that it had to be placed on top of require_module in the config.xml file. Placing it below caused it to have errors.