Overriding a provided API with an alternative version
If an application provides (or uses a library that provides) classes that are also available in the Liberty profile, by default the classes from the Liberty profile are used. To change this so the application uses the alternative versions of these classes, the application must be configured in server.xml, or an included file.
If a web application includes classes that are also present in the server runtime environment, we might want to control which copy of each of those classes is used by the application. For example, if different versions of the classes are present in both the application and the server runtime environment, we must ensure that the version packaged in the application is used.
By default, classes from the Liberty profile runtime environment are used by all Java EE applications. We can override this behavior using the class loader configuration delegation attribute. This configuration is specific to a particular application, or to a shared library that can be selected for use by an application.
Example
In the following example, an application called Scholar needs to use classes that it provides (or provided in a library that it uses), rather than using the copies of the classes available in the Liberty profile.
- When the classes are packaged within the application, override the default parentFirst delegation behavior with a classloader element in server.xml.or a file that it includes:
<application id="" name="Scholar" type="ear" location="scholar.ear"> <classloader delegation="parentLast" /> </application>This tells the application class loader to look at the Liberty profile classes only after looking in the application and its associated libraries for a class.
- When the classes are packaged in a shared library, add the delegation attribute to the classloader element that configures the use of the shared library as follows:
<application id="" name="Scholar" type="ear" location="scholar.ear"> <classloader delegation="parentLast" commonLibraryRef="mySharedLib"/> </application> <library id="mySharedLib"> <fileset dir="${server.config.dir}/myLib" includes="*.jar" /> </library>
We can also use the privateLibraryRef attribute for private libraries in an application. See Share a library across multiple Java EE applications.
Parent topic: Configure class loaders and libraries for Java EE applicationsTasks:
Use a Java library with a Java EE application Share a library across multiple Java EE applications Provide global libraries for all Java EE applications Access third-party APIs from a Java EE application Remove access to third-party APIs for a Java EE application
Defining a utility project as a shared library
Set a web project to use shared libraries