Using objectos :: auto :: Annotions framework to implement the Java class library relying on the relying (Implementing Runtime Dependency Injection for Java Libraries user :: auto :: Annotation F ramework)
Use ObjectOS :: Auto :: Annotion framework to implement the java library when runtime rely on injection
introduce
In Java development, it is an important design mode that relying on injection (DI), which can help us improve the maintenance and testability of code.By dependent injection, we can decide the dependencies of the class library from the code, so that we can more easily conduct unit testing and modular development.
Objectos :: auto :: Annotion is an open source Java framework that provides a simple way to achieve relying in injection during runtime.With this framework, we can hand over the work of injecting to the framework to complete, thereby reducing the burden of developers, making the code clearer and maintaining.
The principle of relying in injection work
Objectos :: auto :: Annotion The framework to read the annotation information of the Java class to achieve dependency injection.Developers need to add corresponding annotations to the attributes that need to be injected to tell the type of the attribute and how to obtain the example.
Basic usage
1. Import the dependencies of ObjectOS :: Auto :: Annotion framework.You can use Maven or Gradle for dependence management.
<dependency>
<groupId>org.objectos</groupId>
<artifactId>auto-annotations</artifactId>
<version>1.0.0</version>
</dependency>
2. In the class that needs to be injected, use the category of the annotation mark of `@Autoannotations`.
@AutoAnnotations
public class MyLibrary {
// The attributes needed to be injected
}
3. Use the `@autowed` annotation mark on the attributes that need to be injected.You can specify the specific implementation class through the annotation of `@qualifier`.
@AutoAnnotations
public class MyLibrary {
@Autowired
private MyDependency myDependency;
}
4. Use the method of `Annotations.configure (Object)` in the startup class to configure the class library.
public class Main {
public static void main(String[] args) {
MyLibrary myLibrary = new MyLibrary();
Annotations.configure(myLibrary);
// ... Use the injecting dependencies
}
}
Example
Below is a simple example, demonstrating the process of relying in injection when running with Objectos :: Auto :: Annitation.
import org.objectos.auto.annotations.*;
@AutoAnnotations
public class MyLibrary {
@Autowired
private MyDependency myDependency;
public void doSomething() {
myDependency.doSomething();
}
}
public interface MyDependency {
void doSomething();
}
@Autowired
public class MyDependencyImpl implements MyDependency {
@Override
public void doSomething() {
System.out.println("Doing something...");
}
}
public class Main {
public static void main(String[] args) {
MyLibrary myLibrary = new MyLibrary();
Annotations.configure(myLibrary);
myLibrary.doSomething();
}
}
In the above examples, the `MyLibrary` class needs to rely on the implementation class of the` MyDependency` interface `myDependencyIMPL`.We use the method of `Annotations.configure (OBJ)` to hand over the dependency injection work of the `MyLibrary` object to the framework, and in the method of` mylibrary.dosomething () `` myDependency` can be used directly.
Summarize
By using ObjectOS :: Auto :: Annotion framework, we can easily implement the java class library to rely on injection when runtime.This framework provides a simple way to deal with the injecting problem, reduce the burden of developers, and make the code more intuitive and maintainable.I hope that this article will help you understand the working principle of injecting at runtime and the use of objectos :: auto :: Annotion framework.