Plexus :: default Container in Java Library
Plexus :: default Container in Java Library
introduce
Plexus is a lightweight Java class library that is used to achieve the function of dependent injection and control reversal.Among them, Plexus :: Default Container is the default container implementation of the Plexus framework, providing developers with a simple and powerful way to manage and call components.
This article will introduce how to configure and use components with Plexus :: DEFAULT Container, and provide some Java code examples to help readers better understand and apply this framework.
1. Introduce dependencies
First of all, introduce the dependence of Plexus :: Default Container in the construction configuration file (such as Pom.xml).For example, when using Maven to build a project, you can add the following code to the <DependenCies> tag:
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-container-default</artifactId>
<version>1.7.1</version>
</dependency>
2. Create components and accessors
Before using the Plexus framework, create a component that needs to be managed and called.A component is an ordinary Java class that must implement an interface or inherit an abstract class, and provides some public methods for other components to call.The following is an example:
public interface Calculator {
int add(int a, int b);
}
public class SimpleCalculator implements Calculator {
public int add(int a, int b) {
return a + b;
}
}
3. Configure Plexus Container
Before using the Plexus Container, you need to perform some basic configuration.Create an XML file called "Components.xml" for the configuration information of the specified component.For example, the following is an example of configured SimpleCalCulator components:
<component>
<role>com.example.Calculator</role>
<implementation>com.example.SimpleCalculator</implementation>
</component>
In this example, the root element specifies the full -limited name of the interface or abstract class of the component, and the Implementation element specifies the full -limited name of the implementation class of the component.
4. Use Plexus Container
Where to use components, you can obtain the corresponding instance through Plexus Container.First, create a defaultplexusContainer object, and load the configuration information of the component through the loadcomPonents method.Then obtain an instance of the component through the lookup method.The following is an example code:
import org.codehaus.plexus.DefaultPlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
public class Application {
public static void main(String[] args) {
try (DefaultPlexusContainer container = new DefaultPlexusContainer()) {
container.loadComponents(new File("components.xml"));
Calculator calculator = container.lookup(Calculator.class);
int result = calculator.add(3, 4);
System.out.println("Result: " + result);
} catch (ComponentLookupException e) {
e.printStackTrace();
}
}
}
In this example, first created a defaultplexusContainer object, and used the LoadComponents method to load the configuration file "Components.xml".Then, the Calculator interface is obtained through the lookup method, and the ADD method is called to calculate.Finally, print the calculation results.
Summarize
Through the above steps, you can easily use Plexus :: Default Container to manage and call components.First, the dependencies are introduced, then the component and accessor is created, and the Plexus Container configuration is performed.Finally, obtain component instances through Plexus Container, and call its method for business processing.
I hope this article will understand and use Plexus :: Default Container.For more detailed information about this type of library, please refer to the official documentation and example code.