- Java EE 8 Design Patterns and Best Practices
- Rhuan Rocha Jo?o Purifica??o
- 173字
- 2021-07-23 16:55:00
Implementing the PersistenceManagerFactory class
In the following code, we have the qualify used to inject the PersistenceManager class:
import javax.inject.Qualifier;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
@Qualifier
@Retention(RUNTIME)
@Target({TYPE, METHOD, FIELD, PARAMETER})
public @interface Persistence {
}
Looking at the following code, we can see that we have the PersistenceManagerFactory class, which is responsible for creating new instances of PersistenceManager. This class uses the @Singleton annotation, which is an EJB annotation used to create a singleton pattern with the Java EE mechanism. The getPersistenceManager method has the @Produces annotation, which is used to define a method responsible for creating a new instance. It also has the @Persistence annotation, which is used as a qualify:
import javax.ejb.Singleton;
import javax.enterprise.inject.Produces;
import java.util.HashSet;
import java.util.Set;
@Singleton
public class PersistenceManagerFactory {
Set<StageManager> stateManagers = new HashSet<StageManager>();
public @Produces @Persistence PersistenceManager
getPersistenceManager(){
//Logic to build PersistenceManager
return new PersistenceManager();
}
}
推薦閱讀
- Linux內核完全注釋(20周年版·第2版)
- 蘋果電腦玩全攻略 OS X 10.8 Mountain Lion
- 嵌入式Linux系統開發:基于Yocto Project
- PLC控制系統應用與維護
- Installing and Configuring Windows 10:70-698 Exam Guide
- 循序漸進學Docker
- 混沌工程實戰:手把手教你實現系統穩定性
- 竹林蹊徑:深入淺出windows驅動開發
- Android物聯網開發細致入門與最佳實踐
- Linux內核設計的藝術:圖解Linux操作系統架構設計與實現原理
- Windows 7應用入門與技巧
- OpenStack系統架構設計實戰
- Introduction to R for Quantitative Finance
- CentOS 6 Linux Server Cookbook
- 鴻蒙HarmonyOS手機應用開發實戰