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 リファレンス ドキュメントを参照) 、アスペクトのロードタイムまたはコンパイル タイム ウィービングに切り替えます。

ロードタイム ウィービングに関する注意点は、Spring コンテキストがロードされる前に JSF クラスがロードされると機能しなくなる可能性があることです。新しく登録されたカスタム クラスローダーは、既にロードされているクラスのバイトコードを変更できません。

于 2013-08-19T12:04:37.063 に答える