0

I can not intercept the JSF managed bean methods via ASpectj. DO you know how to make it possible. I mean, does spring only intercept its own context class methods?

Aspect Class

package com.netas.afad.log;


import java.util.Arrays;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import com.netas.afad.log.annotation.DBOperation;
import com.netas.afad.log.annotation.GuiOperation;

@Aspect
public class LogMonitor {

    int count;


      @Around(value = "@annotation(guiOperation)")
        public Object LogGUIOperation(final ProceedingJoinPoint joinPoint, final GuiOperation guiOperation) throws Throwable {
          count++;        
          final long startMillis = System.currentTimeMillis();
            try {
                System.out.println("Starting GUI operation");
                final Object retVal = joinPoint.proceed();
                return retVal;
            } finally {
                final long duration = System.currentTimeMillis() - startMillis;
                System.out.println("GUI" + joinPoint.getSignature() + " took " + duration + " ms"+ "count:"+count+ " threadId:"+ Thread.currentThread().getId());
                //System.out.println("count:"+count+ " threadId:"+ Thread.currentThread().getId());
            }       
      }



      @Around(value = "@annotation(dbOperation)")
        public Object LogDBOperation(final ProceedingJoinPoint joinPoint, final DBOperation dbOperation) throws Throwable {
          count++;        
          final long startMillis = System.currentTimeMillis();
            try {
                System.out.println("Starting DB operation");
                final Object retVal = joinPoint.proceed();
                return retVal;
            } finally {
                final long duration = System.currentTimeMillis() - startMillis;
                System.out.println("DB Completed.  Call to " + joinPoint.getSignature() + " took " + duration + " ms"+ "count:"+count+ " threadId:"+ Thread.currentThread().getId());
                //System.out.println("count:"+count+ " threadId:"+ Thread.currentThread().getId());
            }       
      } 


}

LoginBean.java

package com.netas.afad.bean;

import java.io.Serializable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.netas.afad.log.annotation.GuiOperation;
import com.netas.afad.service.UserService;

@Component
@Scope("request")
public class LoginBean implements Serializable {

    private static final long serialVersionUID = 1L;

    @Autowired
    UserService userService;

    private long id;

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public long getId() {
        return id;
    }


    public void setId(long id) {
        this.id = id;
    }


    @GuiOperation(name="TESTGUIOP")
    public void yetkiGuncelle(){
        userService.yetkiGuncelle(id, 5);
    }

}

package com.netas.afad.log.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention (RetentionPolicy.RUNTIME)
public @interface GuiOperation {

    String name();
}

Spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:faces="http://www.springframework.org/schema/faces"
    xmlns:int-security="http://www.springframework.org/schema/integration/security"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:ehcache="http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring"
    xsi:schemaLocation="http://www.springframework.org/schema/integration/security 
                        http://www.springframework.org/schema/integration/security/spring-integration-security-2.0.xsd
                        http://www.springframework.org/schema/aop 
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                        http://www.springframework.org/schema/integration 
                        http://www.springframework.org/schema/integration/spring-integration.xsd
                        http://www.springframework.org/schema/beans 
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/faces 
                        http://www.springframework.org/schema/faces/spring-faces-2.0.xsd
                        http://www.springframework.org/schema/tx 
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                        http://www.springframework.org/schema/context 
                        http://www.springframework.org/schema/context/spring-context.xsd
                        http://www.springframework.org/schema/util 
                        http://www.springframework.org/schema/util/spring-util-3.0.xsd
                        http://www.springframework.org/schema/task 
                        http://www.springframework.org/schema/task/spring-task-3.0.xsd
                        http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring 
                        http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

    <context:component-scan base-package="com.netas.afad" />

    <aop:aspectj-autoproxy />
    <bean id="performanceLogger" class="com.netas.afad.log.LogMonitor" />

</beans>
Ahmet Karakaya
  • 9,899
  • 23
  • 86
  • 141
  • make your aspect as a spring managed bean. You can annotate it with a `@Component` annotation – Hrishikesh Feb 18 '14 at 11:10
  • Spring will only intercept beans it knows about (beans managed by the applicationcontext). If your view bean is managed by JSF then spring doesn't know the bean and will not intercept it. Your current code doesn't have an annotation so nothing will be intercepted. – M. Deinum Feb 18 '14 at 11:12
  • @Hrishikesh It is a spring managed bean as it is declared explicitly in the spring.xml file. – M. Deinum Feb 18 '14 at 11:14
  • @M.Deinum Absolutely, you are correct. i just realised it and was ablout to remove the comment :) – Hrishikesh Feb 18 '14 at 11:15
  • I have modified spring.xml as following has solved the problem. . But when Bean Classes context changed from Component to ManagedBean, ManagedProperty, Method interceptor does not work. – Ahmet Karakaya Feb 18 '14 at 12:08
  • As mentioned Spring will only manage beans it knowns about. A JSF bean is outside the scope of spring and as such will not be intercepted. If you want this use load- or compile timeweaving. – M. Deinum Feb 18 '14 at 13:54

0 Answers0