Java Reference
In-Depth Information
At this point, NetBeans generates the following code:
package com.ensode.cdiintro.stereotype;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.enterprise.inject.Stereotype;
@Stereotype
@Retention(RUNTIME)
@Target({METHOD, FIELD, TYPE})
public @interface NamedSessionScoped {
}
Now, we simply need to add the CDI annotations that we want the classes annotated
with our stereotype to use. In our case, we want them to be named beans and have a
scope of session; therefore, we add the @Named and @SessionScoped annotations as
shown in the following code:
package com.ensode.cdiintro.stereotype;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Stereotype;
import javax.inject.Named;
@Named
@SessionScoped
@Stereotype
@Retention(RUNTIME)
@Target({METHOD, FIELD, TYPE})
public @interface NamedSessionScoped {
}
Now we can use our stereotype in our own code:
package com.ensode.cdiintro.beans;
 
Search WWH ::




Custom Search