0
votes

Hello I'm newbie in Spring AOP. I have writed something like this:

My Annotation:

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

Aspect Class:

   @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;
        }

    }
}

Method from ManagedBean

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

And 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

I'm trying to make exception handler using Spring AOP and JSF messages but it does not fire the advice. Please help me.

1
Unless Spring is managing your ManagedBeans it cannot apply the aspects.Sotirios Delimanolis

1 Answers

0
votes

Spring AOP will only work on Spring managed beans i.e. beans in the ApplicationContext. As your JSF beans aren't managed by Spring but by the JSF container the AOP part isn't going to work.

To make it work either make your JSF managed beans Spring managed beans (see the Spring Reference Documentation for that) or switch to loadtime or compile time weaving of your Aspects.

A note on loadtime weaving is that it might nog work if your JSF classes get loaded before the Spring context is loaded, the newly registered custom classloader cannot modify the bytecode of already loaded classes.