0

您好,我是 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 消息制作异常处理程序,但它不会触发建议。请帮我。

4

1 回答 1

0

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

要使其工作,要么使您的 JSF 托管 bean 成为 Spring 托管 bean(参见Spring 参考文档),要么切换到加载时或编译时编织 Aspects。

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

于 2013-08-19T12:04:37.063 回答