How to use the Weld SE (Core) framework in the development of the Java library to implement the relying in the relying injection
In the development of the Java library, dependency injection is a common design mode, which can be implemented through the Weld SE (core) framework.This article will introduce how to use the Weld SE framework in the development of the Java library to achieve dependency injection, and provide some Java code examples.
What is dependent injection?
Dependent injection is a design mode to decide the dependency relationship of an object from the code.By dependent injection, we can easier to manage and replace the dependencies between objects to make the code more flexible and testable.
Weld SE (Core) Framework Introduction
Weld is an open source dependency injection framework that implements the Java CDI (Contexts and Dependency Inject) specification.Weld SE is a sub -project of Weld, which is specially used in the use of dependency injection in the standard Java SE environment.Using Weld SE, we can achieve dependency injection in the development of the class library, and better manage the dependency relationship between categories.
Here are some steps that use Weld SE to achieve dependent injection in the development of the Java library:
Step 1: Add dependencies
First, we need to add Weld SE dependency items to the project construction file (such as Pom.xml).You can introduce Weld SE in the following way:
<dependency>
<groupId>org.jboss.weld.se</groupId>
<artifactId>weld-se</artifactId>
<version>3.1.4.Final</version>
</dependency>
Step 2: Create the bean class
Next, we need to create one or more bean classes.The BEAN class is a dependent class that is used to achieve our business logic.For example, assuming we are developing a library library, we can create a Book class as a Bean class:
public class Book {
private String title;
public Book(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
}
Step 3: Add annotation
In the BEAN class, we need to use WELD's annotations to identify dependencies and scope.Common annotations are:
-`@ApplicationScoped`: A bean instance is shared throughout the application.
-`@Sessionscoped`: a bean instance is shared during HTTP session.
-` `` `q - - -: A bean instance is shared during each HTTP request.
-`@inject`: for dependent injection in other categories.
-`@Named`: Used to specify a name for Bean.
In our Book class, we can use the `@named` annotation to specify a name:
@Named("book")
public class Book {
// ...
}
Step 4: Create the entrance class
We need to create an entry class to start the Weld SE framework and initialize the dependent dependency injecting container.Usually, we can complete these operations in the main method of this class.For example, we create a Library class as the entrance class:
public class Library {
public static void main(String[] args) {
Weld weld = new Weld();
WeldContainer container = weld.initialize();
/ Third
Book book = container.select(Book.class).get();
System.out.println(book.getTitle());
weld.shutdown();
}
}
In the above code, we created a WELD object and called its initialize method to initialize the dependent dependence in injection container.Then, we use the select method of the container to obtain instances of the Book class and output its title.Finally, we call the Shutdown method of the WELD object to close the container.
Step 5: Run the application
Now, we can run the entrance class Library to observe whether the dependency injection is successful.The title of the book class should be displayed on the console output.
Summarize
By using the Weld SE (core) framework, we can achieve dependent injection in the development of the Java library.This article introduces the steps of using the Weld SE framework, including adding dependencies, creating Bean class, adding annotations and creating entrance classes.I hope this article will understand how to achieve dependency injection in the development of the Java library.