- 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();
}
}
推薦閱讀
- Mastering vRealize Operations Manager(Second Edition)
- 從零開始寫Linux內核:一書學透核心原理與實現
- Learning Windows Server Containers
- 大學計算機應用基礎實踐教程(Windows 7+Office 2013)
- 精解Windows 8
- 高性能Linux服務器構建實戰:系統安全、故障排查、自動化運維與集群架構
- Mobile First Design with HTML5 and CSS3
- Linux內核觀測技術BPF
- Windows Server 2012網絡操作系統項目教程(第4版)
- Fedora 12 Linux應用基礎
- ElasticSearch Cookbook
- Linux設備驅動開發
- Advanced Infrastructure Penetration Testing
- Raspberry Pi入門指南
- 操作系統之哲學原理第2版