Announcement Announcement Module
Collapse
No announcement yet.
AOP not working at all. Lost in "too many possibilities" Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP not working at all. Lost in "too many possibilities"

    I'm trying to get Spring AOP working on very simple example.

    I'm trying to catch all "setxxxx" method on a hibernate entity.

    Entity that I'm trying to monitor:

    Code:
    @Entity
    @Table(name="actions")
    public class Action implements Auditable {
    
    @Column(name="Action_Objet", nullable=true)	
    private String action_Objet;
    
    public void setAction_Objet(String value) {
    	System.out.println("action_object");
    	this.action_Objet = value;
    }
    Code:
    public interface Auditable {
    
    }
    application-config.xml
    Code:
    ...
    <context:component-scan base-package="com.mypackage"/>
    <context:annotation-config/>
    
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    
    <!-- my aspect -->
    <bean name="propertyChangeTracker" class="com.mypackage.aspect.PropertyChangeTracker"/>
    ...
    My aspect
    Code:
    import org.apache.log4j.Logger;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.aspectj.lang.annotation.Pointcut;
    
    @Aspect
    public class PropertyChangeTracker {
    	
    	private Logger logger = Logger.getLogger(this.getClass());
    
    	@Before("updateMethod()")
    	public void trackChange(JoinPoint point){
    		String name = point.getSignature().getName();
    		Object newValue = point.getArgs()[0];
    		logger.error(name + " is about to change to "+newValue+" on object "+point.getTarget());
    		System.err.println(name + " is about to change to "+newValue+" on object "+point.getTarget());
    	}
    	
    	@Pointcut("target(com.mypackage.internal.Auditable)")
    	public void updateMethod(){}
    }
    What am I doing wrong ??

    Should I use <bean class="org.springframework.aop.aspectj.annotation. AnnotationAwareAspectJAutoProxyCreator"/>
    Should I use <bean class="org.springframework.aop.aspectj.autoproxy.A spectJAwareAdvisorAutoProxyCreator"/>

    I also see a strange message in application-config around my aspect bean. It says :
    Code:
    Multiple annotations found at this lines:
    -Exception [org/springframework/web/bind/ServletRequestBindingException]
    -Exception [Absent code attribute in method that is not native or abstract in class file javax/servlet/ServetException]"

  • #2
    Try pointcut 'target(com.mypackage.internal.Auditable+)' or 'execution(* com.mypackage.internal.Auditable+.*(..))'

    Comment


    • #3
      Both doesn't work. Maybe I can't make a pointcut over an Hibernate entity...

      The closest I can get to something almost working is : catching the update method that will update the entity.
      For that, I have

      Code:
      @Pointcut("execution(* com.mypackage..*.update*(..))")
      In this case, I can see that "Beans Cross References" shows correctly my "com.mypackage.entitymanager.internal.HibernateAct ionManager" which implements an "com.mypackage.entitymanager.ActionManager".
      But even in this case, although configuration seems good, nothing is logger in console or in logger.

      I've tried many solutions
      Code:
      <aop:aspectj-autoproxy/>
      Code:
      <aop:aspectj-autoproxy proxy-target-class="true"/>
      Code:
      <aop:aspectj-autoproxy proxy-target-class="true">
      	<aop:include name="propertyChangeTracker"/>
      </aop:aspectj-autoproxy>
      Also with
      Code:
      <bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator"/>
      or with
      Code:
      <bean class="org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator"/>
      no luck...

      Anyway, with pointcut over HibernateActionManager.update, I don't get anymore the strange error about "Multiple annotations found at this lines"...

      Comment


      • #4
        I think I put my finder on it. My Auditable interface / Action class is not a bean. I guess I can't make AOP over a "non-bean".

        Comment


        • #5
          What do you mean under 'non-bean'?

          Comment


          • #6
            It's just a entity, so I guess I can't make AOP over it...
            This will be instanciated by Hibernate while retrieve data from datasource, so I guess Spring AOP can't interact at that point. There will be as many instance as records in database.

            I'm just guessing.

            Code:
            @Entity
            @Table(name="actions")
            public class Action implements Auditable {
            
            	public Action() {
            	}
            }
            Anyway, if we consider I could catch the HibernateActionManager, which is a @Repository bean, I could catch the Action object parameter and analyze it.
            But still no luck displaying anything althrough STS detects correctly

            See here: http://212.224.142.219/aop.jpg

            Many thanks for you follow up.

            Comment


            • #7
              I suggest chapter 6 of the reference guide. Your 'hunch' is right, with Spring AOP you can only advice spring configured beans. Object created outside of this cannot be adviced by Spring AOP.

              For that to work you will have to use AspectJ with either loadtime or compile time weaving. Proxy based AOP cannot help you here.

              Comment


              • #8
                Originally posted by cleclefl View Post
                ...

                Many thanks for you follow up.
                Welcome.

                As Marten said, Spring AOP is proxy-based and AspectJ is a rescue. Feel free to check how to eave with AspectJ for example here

                Comment


                • #9
                  Why not look at http://jboss.org/envers/ for entity versioning which I presume you want to do.

                  Comment


                  • #10
                    Envers is very interresting, but I'll keep this for later purpose. In this case, I just want to determine which of the ModelAttribute of Spring MVC or Hibernate / persistance is modifying my entity object. The goal is to find out which of the two I have to watch.

                    Comment

                    Working...
                    X