Page 1 of 1

Need help intercepting broadleaf-profile services with AOP

Posted: Thu May 02, 2013 4:00 pm
by dlavoie
Hi !

First of all, I just want to express my love for this beautifull framework !

I am currently developping an extension to broadleaf that will feed an ERP instance (OpenERP for the curious). I want my newly created customer, products and orders to be feed to a temporary database. A seperate process will be responsible to handle the integration into OpenERP (I don't want my FRONT end Broadleaf to handle backoffice matters, I aim high availability for broadleaf). Once completed, I might share my connectors with the community since OpenERP is a great open source solution for accounting and business related stuff.

Back to my problem, I've read about the Bean Mergin functionnality and it seems like making wonders. However, I was interested into take an AOP approch with Spring AOP.

I built an Aspect who intercepts calls to org.broadleafcommerce.profile.core.service.CustomerService.registerCustomer(). Then I created a simple Junit tests who launches a Spring container with a mocked version of org.broadleafcommerce.profile.core.service.CustomerService. My aspect works perfectly when calling my registerCustomer method.

Next step, integrate my new aspect (as a jar dependency) inside the site project (the demo one). I've added my "Aspect Projet" as a dependency insite site/pom.xml. Then added the classpath of that aspect through a context:component-scan inside site/../WEB-INF/applicationContext-servlet.xml.

First run, my @PostContruct method inside my aspect is called properly meaning that Broadleaf's Spring Container instanciated it nicely. However, my final test upon calling CustomerService.registerCustomer fails. A debuging session proved me my that my test reaches the registerCustomer method properly. The method is simply not intercepted by my aspect.

Could it be that the CustomerService is not instanciated inside the same spring container than the one who is responsible of the applicationContext-servlet.xml. Another question I have is the following : "Is it possible to intercept methods from the "broadleaf-profile" module with Aspect imported by dependency in the site project ?". Is there any other Spring AOP configuration I might need to add inside the config files ?

I am sorry for theses tricky questions. But if it is impossible, I will fall back to the bean merging process which is well documented in your online help. If you don't see reason why my approch isn't suposed to work, I'll be glad to upload some snipets of my code. Sorry again for the long post, but some expert knowledge of broadleaf would be highly appreciated :)

Thanks in advance !

EDIT 2013-05-02 : I have resigned myself to use the context merging capability of Broadleaf and it proved to be pretty efficient. My functionality is working properly. However, I am still curious to know if there is anyway to use AOP over the core services of Broadleaf.

Re: Need help intercepting broadleaf-profile services with AOP

Posted: Tue May 07, 2013 4:18 pm
by phillipuniverse
First off: interesting idea to use AOP rather than just bean overriding. I would say that's probably overkill; it seems more appropriate to subclass as your other solution has done. Of course, if you are wanting to release this back to the community then perhaps AOP is more appropriate rather than having another developer need to subclass your version of customerService if they needed some other custom functionality.

To answer your specific question about the application context, you are right; the CustomerService bean is in the root application context so that other beans outside of the servlet can depend on it. I am not completely familiar with all the ins and outs of AOP and when/how/where everything has to occur, but it makes sense to me that this might have to occur within the same Spring context (and not a child context; which I believe is the relationship that the servlet context has). Adding additional beans and things to the root application context for Broadleaf essentially means that you need to put your beans in some applicationContext.xml that is included in the patchConfigLocations in web.xml (which, if you have previous Spring experience, is basically analogous to contextConfigLocations).

You also have a slightly unique case in that you are putting your aspects in another project. Here is what I would do in your situation:

  • Define an applicationContext-aspects.xml in your aspects dependency project
  • Include any of your aspect configurations (component scans, etc) in applicationContext-aspects.xml
  • Reference applicationContext-aspects.xml in the patchConfigLocations parameter of your web.xml. This will probably just be classpath:/applicationContext-aspects.xml.

Thanks for taking the time to elaborate your approach. I would love to hear if my above suggestion works and in general we would love to hear about how you are using OpenERP. Feel free to shoot us an email at info@broadleafcommerce.org if you would like to discuss more about helping get the word out about your integration.

Re: Need help intercepting broadleaf-profile services with AOP

Posted: Sat May 11, 2013 4:21 am
by dlavoie
Thanks Phillip for taking the time. I will try that and revert to you with a feedback. Your proposal seems indeed a good approach.

I'll also take some time to send you some input about my integration with OpenERP.

Re: Need help intercepting broadleaf-profile services with AOP

Posted: Sun May 12, 2013 3:43 am
by dlavoie
Way to go Phillip !!!

Many thanks ! Your approach managed to fix my issue. My aspect is now properly intercepting calls to the CustomerService. I understand that it might be overkill but AOP is definitely more plug & play than overriding beans.

All I have to do to integrate the connector is to import the jar and add the applicationContext inside the web.xml.

I'll be going further with my development. In the mean time, I"ll be preparing you a mail about how I am connecting BLC to OpenERP.

Have a good day and thanks again !

Regards,
Daniel Lavoie
dlavoie@cspinformatique.com