上QQ阅读APP看书,第一时间看更新
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();
}
}