Plexus :: Default Container
Plexus :: Default Container is a implementation of the Plexus container framework, which can achieve dependent injection in the Java class library.Dependent injection is a design mode that can improve the testability, maintenance and scalability of the code.
Plexus is a lightweight dependency injection container framework. It provides a simple and powerful mechanism to manage the dependency relationship between the Java classes.Plexus container uses a mode called IOC (control reversal) to manage the dependency relationship between classes.In the IOC mode, the class is no longer responsible for the creation and management of its own dependencies, but handed over these responsibilities to the container.
To achieve dependency injection in the Java class library, you can follow the steps below:
Step 1: Add Plexus related dependencies
In the configuration file of the project, you need to add Plexus -related dependencies. For example, in the Maven project, you can add the following dependencies:
<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-containers</artifactId>
<version>1.7.2</version>
</dependency>
Step 2: Create a class that needs to be injected
Creation requires dependence in injection classes, such as:
public class Foo {
private Bar bar;
public void setBar(Bar bar) {
this.bar = bar;
}
public void doSomething() {
bar.doSomethingElse();
}
}
public class Bar {
public void doSomethingElse() {
System.out.println("Doing something else...");
}
}
Step 3: Write Plexus configuration file
Create a Plexus container configuration file in the project, also known as Components.xml.This file specifies the instance of how the Plexus container creates and injected class.For example, you can create a file called Components.xml, and add the following in it:
<component>
<role>com.example.Foo</role>
<implementation>com.example.Foo</implementation>
<requirements>
<requirement>
<role>com.example.Bar</role>
<role-hint>default</role-hint>
</requirement>
</requirements>
</component>
<component>
<role>com.example.Bar</role>
<implementation>com.example.Bar</implementation>
</component>
Step 4: Create a Plexus container
Create a Plexus container in the Java code and obtain an instance that needs to be injected through the container.For example:
DefaultPlexusContainer container = new DefaultPlexusContainer();
Foo foo = container.getComponent(Foo.class);
In this way, the Plexus container will automatically create an instance of the FOO class, and automatically injected the instinctive BAR object into the FOO object.The method of calling the FOO object can be called in the following way:
foo.doSomething();
Run the above code will output "Doing Something Else ...".
To achieve dependency injection through the Plexus container, it can effectively decompose the dependent relationship between the coupling class, and improve the measurement and scalability of the code.At the same time, Plexus also provides other functions, such as life cycle management and plug -in support, making development more convenient and flexible.