拦截器不适用于 JSF 托管 bean?
Interceptors don't work with JSF managed beans?
我决定试试 interceptors
我的第一个拦截器绑定注解是
@Inherited
@InterceptorBinding
@Target({TYPE})
@Retention(RUNTIME)
public @interface WithLog {
// No parameters required
}
拦截器class是
@Interceptor
@WithLog
public class LogInterceptor {
@AroundInvoke
private Object logMethod(InvocationContext context) throws Exception {
System.out.println("Method " + context.getMethod().getName() +
" of class " + context.getTarget().getClass().getName() + " was called.");
return context.proceed();
}
@PostConstruct
private void construct(InvocationContext context) {
System.out.println("@Postconstruct of " +
context.getMethod().getDeclaringClass().getName() + " started.");
}
}
所以,我想为 JSF 托管 bean 添加简单的日志记录:
@ManagedBean(name = "departmentRootMB")
@ViewScoped
@WithLog
public class DepartmentRootMB implements Serializable {
long serialVersionUID = 0L;
// . . . properties, methods
}
我读到,要启用拦截器,我需要创建 beans.xml
。我在 WEB-INF
目录中创建了一个:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated">
<interceptors>
<class>ru.edu.pgtk.weducation.interceptors.LogInterceptor</class>
</interceptors>
</beans>
我重建项目但没有效果。错误在哪里?我做错了什么?我使用带有标准组件(WELD、EclipseLink、JSF 2.2.7)的 glassfish 4.1
感谢您的宝贵时间和最诚挚的问候。
Interceptors don't work with JSF managed beans?
正确。
用 CDI bean 管理工具替换 JSF bean 管理工具。
换句话说,将@ManagedBean
和朋友替换为@Named
和朋友。无论如何,JSF bean 管理工具在未来的 Java EE 版本中将被弃用以支持 CDI。现在是迁移的好机会。
另请参阅:
- Backing beans (@ManagedBean) or CDI Beans (@Named)?
我决定试试 interceptors
我的第一个拦截器绑定注解是
@Inherited
@InterceptorBinding
@Target({TYPE})
@Retention(RUNTIME)
public @interface WithLog {
// No parameters required
}
拦截器class是
@Interceptor
@WithLog
public class LogInterceptor {
@AroundInvoke
private Object logMethod(InvocationContext context) throws Exception {
System.out.println("Method " + context.getMethod().getName() +
" of class " + context.getTarget().getClass().getName() + " was called.");
return context.proceed();
}
@PostConstruct
private void construct(InvocationContext context) {
System.out.println("@Postconstruct of " +
context.getMethod().getDeclaringClass().getName() + " started.");
}
}
所以,我想为 JSF 托管 bean 添加简单的日志记录:
@ManagedBean(name = "departmentRootMB")
@ViewScoped
@WithLog
public class DepartmentRootMB implements Serializable {
long serialVersionUID = 0L;
// . . . properties, methods
}
我读到,要启用拦截器,我需要创建 beans.xml
。我在 WEB-INF
目录中创建了一个:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated">
<interceptors>
<class>ru.edu.pgtk.weducation.interceptors.LogInterceptor</class>
</interceptors>
</beans>
我重建项目但没有效果。错误在哪里?我做错了什么?我使用带有标准组件(WELD、EclipseLink、JSF 2.2.7)的 glassfish 4.1
感谢您的宝贵时间和最诚挚的问候。
Interceptors don't work with JSF managed beans?
正确。
用 CDI bean 管理工具替换 JSF bean 管理工具。
换句话说,将@ManagedBean
和朋友替换为@Named
和朋友。无论如何,JSF bean 管理工具在未来的 Java EE 版本中将被弃用以支持 CDI。现在是迁移的好机会。
另请参阅:
- Backing beans (@ManagedBean) or CDI Beans (@Named)?