×
☰ See All Chapters

Java module provides and uses directive

provides module directive is used to make service implementation module as a service provider. Let us consider module A is service provider. Module B is service implementer. Now all the implementation is available with module B, but client say module C should use module A not B. How module C get the implementation from module B where it has not included module B but has included only module A? This is possible using provides directive. To specify the service class to which implementation is provided, with keyword should be used with provides directive.

Syntax of provides directive

module com.java4coding.service.impl {

provides InterfaceOrAbstractClassNameAlongwithPackage with ImplementationClassNameAlongwithPackage;

}

Example for provides directive

module com.java4coding.service.impl {

provides com.java4coding.service.ServiceInterface with com.java4coding.service.impl.ServiceImpl;

}

For the implementation module to specify the implementation using provides, the service provider module must declare that interface using uses directive.

Syntax of uses directive

module com.java4coding.service {

uses InterfaceOrAbstractClassNameAlongwithPackage;

}

Example for uses directive

module com.java4coding.service {

uses com.java4coding.service.ServiceInterface;

}

In the below example, com.java4coding.service is service provider module, com.java4coding.service.impl is an implementation module, com.java4coding.service.client is client module. Module com.java4coding.service has uses directive and specified the interface com.java4coding.service.ServiceInterface. Module com.java4coding.service.impl has provides directive specified that the module provides a service implementation, and it is a service provider.

java-module-provides-uses-0
 

All Chapters
Author