Skip to Content
author's profile photo Former Member
Former Member

AOP in hybris not working

Hi , I am working on a simple AOP functionality , where I want my advice to be called before the excution of the de.hybris.platform.cronjob.jalo.CronJob.sendEmail(). But my aspect is not even called when I used annotation based configuration. I had just made my aspect's bean entry in myextention-spring.xml and bean class="org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator".Created an aspect for that as well. My standalone sample AOP app is working perfectly. Is there any thing I am doing wrong.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

6 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on Feb 10, 2015 at 07:25 PM

    Hi Ankit,

    Some steps that I can suggest:

    • In your extension add to the resources folder a folder called META-INF and place aop.xml here.

    • Create a class to act as your aspect in the src - {Pakage - path}.YourAspect.java

    • In this class you need to decorate it with the @Aspect annotation and use a pointcut to in your case the Cronjob.sendEmail method. This would like something like the following:

    @Pointcut("execution(* de.hybris.platform.cronjob.jalo.CronJob.sendEmail(..))") public void theExample() { //Leave empty }

    You also add your advice here which references this pointcut:

    //Example with Around advice

    @Around("theExample()") public Object theMethod(final ProceedingJoinPoint pjp) { //Your logic log.info("ASPECT INVOKED");

    Object o = pjp.proceed; / Don't forget in an around advice to to call the jointpoints proceed/ return o;

    • Within the aop.xml are located your weaves. So it would be something like:

     <aspectj>
         <weaver options="-Xset:weaveJavaxPackages=true">
             <include within="com.yourpackage.aspects.." />
             <include within="de.hybris.platform.cronjob.jalo.." />
         </weaver>
         <aspects>
             <aspect name="com.yourpackage.aspects.YourAspect" />
         </aspects>
     </aspectj>
    

    You will experience performance problems if you weave the entire platform. i.e.Just include your package and the cronjob package in the include within. The .. includes subfolders while the * acts as a wildcard.

    Now if you've added the aspect jar as an argument to the VM on startup these classes should be weaved through load time weaving.

    I hope this information proves useful to you.

    Best Regards,

    Ben

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Thanks for solution. It worked for me in 6.0 and I did not had to use aop.xml. Used only annotations. Now wanted to check if I have to pointcut on all overriden perform() method of all the extended cronjobs like mycronjob1.perform() , mycronjob2perform() , how should I define the pointcut in one single annotation. Is this possible ? If not do I have to define multiple methods for each extended cronjob class?

  • author's profile photo Former Member
    Former Member
    Posted on Feb 10, 2015 at 08:40 PM

    Hi ANKIT,

    I think the Ben solution will work without any problem.

    But just I have a little doubt about your bean configuration. Spring by default has enabled two types of aop one is JDK and another one is CGLIB both are integrated in the core so you do not need to add external libraries to the app. But by default ( if you do not specify it spring will use the JDK ) that it will not work in several case : 1. the class with the method that you want apply the advice does not implement an interface ( your case ) 2. if the method it is not public.

    If you want to tell spring to use the CGLIB you should set proxy-by-class=true in aop config tag ( if you used the xml configuration ).

    check this out : http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop-api.html#aop-pfb-proxy-types

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Feb 09, 2015 at 06:17 PM

    Hi Ankit,

    Have you configured load time weaving by adding the aspectj weaver jar to the JVM for startup?

    https://wiki.hybris.com/display/release5/Spring+AOP+in+the+hybris+Commerce+Suite#SpringAOPinthehybrisCommerceSuite-ConfiguringLoad-TimeWeaving

    Kind Regards,

    Ben

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Feb 10, 2015 at 09:07 AM

    Thanks Ben for your quick reply,But I Just wanted to ask, Can we add an aspect to a JALO or not. Thanks

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Feb 10, 2015 at 10:26 AM

    That is possible based on the information that Ben provided. It is not possible to annotate this in Spring because spring does not know about jalo classes but instead you add those aspects when a class is loaded. I have used this technique and it works fine. Read the documentation ones mores :)

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on Jul 14, 2017 at 10:31 PM

    Hi All,

    I was trying to write AOP for classes available in web folder,followed the steps mentioned in below link.

    https://help.hybris.com/6.0.0/hcd/8c62d88286691014874bc5b8e7244c7a.html#loio8c62d88286691014874bc5b8e7244c7a__springaopinthehybriscommercesuite-configuringload-timeweavinginhybriscommercesuite

    But my aop is not being invoked,need to weave classes in platform HAC.

    Regards,

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.