import static java.lang.annotation.ElementType.FIELD; import static java.lang.annotation.ElementType.METHOD; import static java.lang.annotation.ElementType.TYPE; import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; import javax.enterprise.inject.Stereotype; import javax.inject.Named; import org.omnifaces.cdi.ViewScoped; @Named @ViewScoped @Documented @Stereotype @Target({ TYPE, METHOD, FIELD }) @Retention(RUNTIME) public @interface ViewModel { }注:org.omnifaces.cdi.ViewScoped 是一个兼容CDI的viewscoped.作用和JSF的View范围一样.
@ViewModel public class LoginBean { ... }
@RequestScoped @Stereotype @Retention(RUNTIME) @Target(TYPE) public @interface Action {}但是Bean也可以自己重写这个默认值:
@Dependent @Action public class DependentScopedLoginAction { ... }
@RequestScoped @Transactional(requiresNew=true) @Secure @Stereotype @Retention(RUNTIME) @Target(TYPE) public @interface Action {}
这样你的业务代码里就看不到transaction和security等技术相关的内容了
@RequestScoped @Transactional(requiresNew=true) @Secure @Named @Stereotype @Retention(RUNTIME) @Target(TYPE) public @interface Action {}如上所示,stereotype可以使用@Named,这样所有的使用此注解的Bean都会有默认的EL NAME.
现在LoginAction bean 的默认EL NAME就是 loginAction.
@Alternative @Stereotype @Retention(RUNTIME) @Target(TYPE) public @interface Mock {}
在对应Bean上使用
@Mock public class MockLoginAction extends LoginAction { ... }
bean.mxl配置,此处仅需如下写入,org.mycompany.testing.Mock就是stereotype注解.这样所有使用@Mock的Bean会被启用.
<beans> <alternatives> <stereotype>org.mycompany.testing.Mock</stereotype> </alternatives> </beans>
@Auditable @Action @Stereotype @Target(TYPE) @Retention(RUNTIME) public @interface AuditableAction {}
@Named @RequestScoped @Stereotype @Target({TYPE, METHOD, FIELD}) @Retention(RUNTIME) public @interface Model {}Instead of using JSF managed beans, just annotate a bean @Model, and use it directly in your JSF view!