【问题标题】:JSF and Spring AOP @Around not beeing invokedJSF 和 Spring AOP @Around 未被调用
【发布时间】:2013-08-19 20:04:37
【问题描述】:

您好,我是 Spring AOP 的新手。 我写了这样的东西:

我的注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ExceptionHandling {
    String onSuccess();
    String onFailture();
}

方面类:

   @Aspect
public class ExceptionHandler implements Serializable {

@Pointcut(value="execution(public * *(..))")
public void anyPublicMethod() {

}


@Around("anyPublicMethod() && @annotation(exceptionHandling)")
    public Object displayMessage(ProceedingJoinPoint joinPoint,ExceptionHandling exceptionHandling) throws FileNotFoundException {
        try{
            Object point = joinPoint.proceed();
            new PrintWriter(new File("D:\\log.txt")).append("FUUCK").flush();
            FacesMessageProvider.showInfoMessage(
                    FacesContext.getCurrentInstance(),exceptionHandling.onSuccess());
            return point;
        } catch(Throwable t) {
              new PrintWriter(new File("D:\\log.txt")).append("FUUCK").flush();
            FacesMessageProvider.showFatalMessage(
                    FacesContext.getCurrentInstance(),
                    exceptionHandling.onFailture());
             return null;
        }

    }
}

ManagedBean 中的方法

@ExceptionHandling(onSuccess=IMessages.USER_UPDATED,onFailture=IMessages.WRONG_DATA)
public void onClickUpdateFromSession(){
   onClickUpdate(sessionManager.getAuthenticatedUserBean());
}

还有 app-config.xml

 <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        ">
        <aop:aspectj-autoproxy/>
        <bean id="exceptionHandler" 
              class="eteacher.modules.ExceptionHandler"/> 
        <bean id="sessionManager"
              class="eteacher.modules.SessionManager"
              scope="session"/>





    </beans

我正在尝试使用 Spring AOP 制作异常处理程序 和 JSF 消息,但它不会触发建议。 请帮帮我。

【问题讨论】:

  • 除非 Spring 正在管理您的 ManagedBeans,否则它不能应用方面。

标签: java spring jsf aop


【解决方案1】:

Spring AOP 仅适用于 Spring 托管的 bean,即 ApplicationContext 中的 bean。由于您的 JSF bean 不是由 Spring 管理,而是由 JSF 容器管理,因此 AOP 部分将无法工作。

要使其工作,要么让您的 JSF 托管 bean 成为 Spring 托管 bean(参见Spring Reference Documentation),要么切换到loadtime 或编译时编织你的方面。

关于加载时间编织的注意事项是,如果您的 JSF 类在加载 Spring 上下文之前加载,它可能无法正常工作,新注册的自定义类加载器无法修改已加载类的字节码。

【讨论】:

  • 感谢您的回答。我提供了门面层作为弹簧豆,它的工作。我无法将 jsf 托管 bean 更改为 Spring 托管 bean,因为我使用 CDI,它不能一起工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-29
  • 2012-07-11
  • 2022-08-20
  • 1970-01-01
  • 2021-01-31
相关资源
最近更新 更多