Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Virgo » How do you set up Spring AOP/AspectJ/Virgo
How do you set up Spring AOP/AspectJ/Virgo [message #608511] Thu, 02 September 2010 23:20 Go to next message
Rich Mayfield is currently offline Rich Mayfield
Messages: 44
Registered: August 2010
Member
Is there a good tutorial/description of what one needs to do to set up AspectJ load time weaving alongside Spring AOP in Eclipse Virgo?

I see mention that this is feasible, but don't see anything documentation.

Thanks in advance
Re: How do you set up Spring AOP/AspectJ/Virgo [message #609272 is a reply to message #608511] Thu, 09 September 2010 10:15 Go to previous messageGo to next message
Glyn Normington is currently offline Glyn Normington
Messages: 1222
Registered: July 2009
Senior Member
I'm afraid there isn't such a tutorial or description, but it should work. ;)

If you declare

<context:load-time-weaver aspectj-weaving="on"/>

in your application context, the kernel should automatically replace Spring's standard LoadTimeWeaver with a KernelLoadTimeWeaver implementation that's OSGi-aware.

Spring's AspectJ-based load time weaving support should then just work as expected.

Please could you try that and let us know if you hit a problem. If you are successful and fancy providing a little tutorial or description, or at least posting your findings here, that would be a nice contribution to the project.
Re: How do you set up Spring AOP/AspectJ/Virgo [message #676583 is a reply to message #609272] Sun, 05 June 2011 13:43 Go to previous messageGo to next message
Matt Fellows is currently offline Matt Fellows
Messages: 28
Registered: February 2011
Junior Member
I'm actually not sure there is a need for a tutorial - it is as simple this:

1. Add
<context:load-time-weaver aspectj-weaving="on"/>
to your application context as Glyn mentioned

2. Ensure that the AspectJ weaver is on the classpath in addition to any other spring dependencies mentioned in the AOP documentation for your case (For me, i required it for @Configurable http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/aop.html#aop-atconfigurable).

Domain objects are being byte-code woven - happy days!

You do not need to modify the Virgo\Tomcat startup script, create a bundle-fragment for the catalina classloader or anything of the sort mentioned on various other websites. It just works.
Re: How do you set up Spring AOP/AspectJ/Virgo [message #684900 is a reply to message #676583] Thu, 16 June 2011 12:57 Go to previous messageGo to next message
Sven Panko is currently offline Sven Panko
Messages: 13
Registered: February 2011
Location: Germany
Junior Member
I am also struggling with getting LTW with Virgo 2.1.0 running. Here's what I have thus far:
* I created a bundle A with a spring context in META-INF/spring that contains the <context:load-time-weaver aspectj-weaving="on" /> element
* The same (!) bundle contains a META-INF/aop.xml that defines the aspects to be loaded and the classes to consider for LTW
* I see this in my logs when the bundle starts:
[2011-06-16 14:50:05.536] region-dm-6 o.e.virgo.kernel.dmfragment.internal.LoadTimeWeaverPostProcessor Found load-time weaver bean for bundle 'foo.bar_1.1.0.SNAPSHOT [175]'. Switching to ServerLoadTimeWeaver.

My aop.xml looks like this:
<aspectj>
    <weaver options="-verbose -debug">
        <include within="**"/>
    </weaver>

    <aspects>
        <aspect name="foo.ProfileAspect"/>
    </aspects>
</aspectj>


But - I don't get any output from my profiling aspect. No information from the weaver (debug/verbose) output. Am I missing anything here? Apart from the above-mentioned configuration, I didn't add anything to my Spring context or to the Virgo installation. Note that the aspect class is exported by another bundle that was loaded before the above-mentioned bundle (the packages are correctly resolved)...

Thanks in advance

Sven
Re: How do you set up Spring AOP/AspectJ/Virgo [message #727757 is a reply to message #684900] Wed, 21 September 2011 23:08 Go to previous messageGo to next message
rob.gratz is currently offline rob.gratz
Messages: 1
Registered: September 2011
Junior Member
Has anyone provided any input into this? I am seeing the exact same behavior.

Any help would be greatly appreciated.

Thanks.
Re: How do you set up Spring AOP/AspectJ/Virgo [message #728073 is a reply to message #727757] Thu, 22 September 2011 13:41 Go to previous messageGo to next message
Rick Cole is currently offline Rick Cole
Messages: 1
Registered: September 2011
Junior Member
Has anyone found a solution to this? We have added the context:load-time-weaver to our application context. The behavior I am seeing is that the postProcessBeanFactory() method on the BeanFactoryPostProcessors of the bundle application context is getting called BEFORE the AspectJWeavingEnabler is created. The result is that the postProcessBeanFactory() method on the AspectJWeavingEnabler is never called which I assume means load time weaving is not going to work.

[Updated on: Thu, 22 September 2011 13:42]

Report message to a moderator

Re: How do you set up Spring AOP/AspectJ/Virgo [message #734114 is a reply to message #728073] Thu, 06 October 2011 17:55 Go to previous messageGo to next message
Carlos Salinas is currently offline Carlos Salinas
Messages: 32
Registered: March 2011
Location: Avd. de la Argentina 132,...
Member
Hi Rich,

I think the workound for your problem is the following in the following snippet (Ill explain it in detail afterwards):

<!-- Consume the Logging Aspect as a service -->
<osgi:reference id="loggingAspect" interface="com.thingtrack.customtrack.log.LoggingAspect" />

<!-- The DAO bean-->
<bean id="countDAO" class="com.thingtrack.customtrack.dao.internal.CountDAOImpl">
  <property name="entityManager">
    <ref bean="entityManager" />
  </property>
</bean>

<!-- The Proxy Factory Bean 'advices' using the logging aspect service above -->
<bean id="serviceTemplate" abstract="true" class="org.springframework.aop.framework.ProxyFactoryBean">
 <property name="target">
   <list>
     <value>countDAO</value>
   </list>
 </property>
 <property name="interceptorNames">
   <list>
    <value>loggingAspect</value>
   </list>
 </property>
</bean>

<!-- OSGi configuration for the Load Time Weaver -->
<context:load-time-weaver aspectj-weaving="on" />

<!-- AOP configuration and choose CGLIB as Code Generation Library -->
<aop:config proxy-target-class="false">
  <aop:pointcut expression="execution(* com.thingtrack.customtrack.dao..*.*(..))"     id="loggingPointCut"/>
  <aop:aspect ref="loggingAspect">
    <aop:before method="logEntry" pointcut-ref="loggingPointCut"/>
    <aop:after method="logExit" pointcut-ref="loggingPointCut"/>
    <aop:after-returning method="logAfterReturn" pointcut-ref="loggingPointCut"     returning="returnedValue"/>
    <aop:after-throwing method="logAfterThrowing" pointcut-ref="loggingPointCut" throwing="throwable"/>
  </aop:aspect>
</aop:config>


In code i want to show how to advice using Spring AOP the DAO bean 'countDAO' using a Aspect bean, loggingAspect, previouly registered as a service by another bundle that contains all my app´s logging aspects. So to weave 'countDAO' at load time with the aspect i have to configure in the Application Context the Spring ProxyFactoryBean and join together the aspect with the target class, the dao. In this way you will not have problems generating the necesary proxy.

This is works with CGLIB and JDKDynamicProxies too.
If you have any doubt or problem let know.

Re: How do you set up Spring AOP/AspectJ/Virgo [message #757153 is a reply to message #734114] Wed, 16 November 2011 19:33 Go to previous messageGo to next message
Martin W. is currently offline Martin W.
Messages: 8
Registered: June 2011
Junior Member
Hi everybody,

Is there a way to weave aspects across multiple Virgo bundles?

I.e.: Having 3 bundles:
1st bundle: provides an interface
2nd bundle: imports the interface, provides an implementation, defines aspects (weaving works fine here)
3rd bundle: imports the interface, provides another implementation, aspects should be woven here as well (I could not get this to work)
I want to avoid explicitly importing anything from the 2nd bundle in the 3rd bundle.

Thanks in advance for any advice on the issue...

Best regards,
Martin
Re: How do you set up Spring AOP/AspectJ/Virgo [message #757334 is a reply to message #757153] Thu, 17 November 2011 18:47 Go to previous messageGo to next message
Martin W. is currently offline Martin W.
Messages: 8
Registered: June 2011
Junior Member
Martin W. wrote on Wed, 16 November 2011 14:33

Is there a way to weave aspects across multiple Virgo bundles?


Hi again,

After doing a lot of reading I'm assuming it is possible. But just to be sure, can anyone tell me if it is possible ACROSS BUNDLES (define aspect in one bundle, weave into class of another bundle)?

If so, do I have to make any modifications to the Virgo configuration for LTW to work across bundles? I did not find any tutorials specifically for Virgo, and as I've never worked with other OSGI frameworks, it's difficult for me to use other tutorials here....

Furthermore, is it possible without any modifications whatsoever in the bundle where the aspect should be woven? (again, I'm assuming it should work)

However, after spending like a week of time, I could not get it work. Even without the restriction of not making modifications in the 2nd bundle.

Unfortunately the source-code in bug 333646 is not available completely anymore....

I'm using Virgo 3.0.1.RELEASE (fresh installation), my IDE is SpringSource Tool Suite. Here's what I did:

1. create an 'EclipseRT OSGI Bundle' "base"
in there is one interface ("IProfiler") defining a method void execute() -- writes to System.out
the package is being exported

2. create another bundle "component"
one class ("BarProfiler"), implementing IProfiler. and one class "BarProfilerCaller". BarProfilerCaller has a field of type IProfiler.
In the spring configuration XML file, both classes are being instantiated, and setProfiler in BarProfilerCaller is called, which immediately calls execute().

3. 3rd bundle: "aspect1"
MyAspect.aj:
package aspect;

public aspect MyAspect {

	pointcut all() : execution(* *.*(..)) && !within(MyAspect);

	after() returning : all() {
		System.out.println("Hello World from MyAspect as well....");
	}

}

I get a warning there: "advice defined in aspect1.MyAspect has not been applied [Xlint:adviceDidNotMatch]"


aop.xml:
<aspectj>
    <weaver>
	<include within="**"/>
    </weaver>
    <aspects>
        <aspect name="aspect.MyAspect"/>
    </aspects>
</aspectj>


spring/aspect-context.xml:
<beans
  .....
  <!--   <aop:aspectj-autoproxy/>     -->
  <context:load-time-weaver aspectj-weaving="on"/>    
</beans>


MANIFEST.MF:
Manifest-Version: 1.0
Bundle-Version: 1.0.0
Bundle-Name: Aspect POC
Bundle-ManifestVersion: 2
Bundle-SymbolicName: aspect1
Import-Package: base,
 org.aspectj.weaver;version="[1.6.10.RELEASE,1.6.10.RELEASE]",
 org.aspectj.weaver.ast;version="[1.6.10.RELEASE,1.6.10.RELEASE]",
 org.aspectj.weaver.bcel;version="[1.6.10.RELEASE,1.6.10.RELEASE]",
 org.aspectj.weaver.bcel.asm;version="[1.6.10.RELEASE,1.6.10.RELEASE]",
 org.aspectj.weaver.internal.tools;version="[1.6.10.RELEASE,1.6.10.RELEASE]",
 org.aspectj.weaver.loadtime;version="[1.6.10.RELEASE,1.6.10.RELEASE]",
 org.aspectj.weaver.loadtime.definition;version="[1.6.10.RELEASE,1.6.10.RELEASE]",
 org.aspectj.weaver.ltw;version="[1.6.10.RELEASE,1.6.10.RELEASE]",
 org.aspectj.weaver.model;version="[1.6.10.RELEASE,1.6.10.RELEASE]",
 org.aspectj.weaver.patterns;version="[1.6.10.RELEASE,1.6.10.RELEASE]",
 org.aspectj.weaver.reflect;version="[1.6.10.RELEASE,1.6.10.RELEASE]",
 org.aspectj.weaver.tools;version="[1.6.10.RELEASE,1.6.10.RELEASE]",
 org.osgi.framework,
 org.osgi.framework.hooks.weaving;version="[1.0.0,1.0.0]",
 org.springframework.aop;version="[3.0.5.RELEASE,3.0.5.RELEASE]",
 org.springframework.aop.framework;version="[3.0.5.RELEASE,3.0.5.RELEASE]",
 org.springframework.beans.factory.config;version="[3.0.5.RELEASE,3.0.5.RELEASE]",
 org.springframework.context.weaving;version="[3.0.5.RELEASE,3.0.5.RELEASE]",
 org.springframework.core;version="[3.0.5.RELEASE,3.0.5.RELEASE]",
 org.springframework.instrument.classloading;version="[3.0.5.RELEASE,3.0.5.RELEASE]"
Import-Bundle: com.springsource.net.sf.cglib;version="2.2.0"
Eclipse-SupplementBundle: base,
 component


When i deploy the bundles: for base and component I get something like
[2011-11-17 19:36:03.996] INFO  region-dm-8                  o.e.virgo.kernel.dmfragment.internal.LoadTimeWeaverPostProcessor  Load-time weaving not enabled for bundle 'component_1.0.0 [124]', 

(unless i add "<context:load-time-weaver aspectj-weaving="on" />" to the spring config. - but then i don't get the output of the aspect either...)

For the aspect bundle I get:
[2011-11-17 19:40:44.708] INFO  region-dm-2                  o.e.virgo.kernel.dmfragment.internal.LoadTimeWeaverPostProcessor  Found load-time weaver bean for bundle 'aspect1_1.0.0 [124]'. Switching to ServerLoadTimeWeaver. 


What am I missing?
Thanks in advance for any hints!

All the best,
Martin

[Updated on: Fri, 18 November 2011 08:35]

Report message to a moderator

Re: How do you set up Spring AOP/AspectJ/Virgo [message #757421 is a reply to message #757334] Fri, 18 November 2011 12:02 Go to previous messageGo to next message
Frieder Heugel is currently offline Frieder Heugel
Messages: 61
Registered: October 2010
Location: Basel, CH
Member
Hi

Martin W. wrote on Thu, 17 November 2011 19:47

But just to be sure, can anyone tell me if it is possible ACROSS BUNDLES (define aspect in one bundle, weave into class of another bundle)?

If so, do I have to make any modifications to the Virgo configuration for LTW to work across bundles? I did not find any tutorials specifically for Virgo, and as I've never worked with other OSGI frameworks, it's difficult for me to use other tutorials here....


Yes it is possible to define an aspect in one bundle and weave code into a class located in another bundle. You already mentioned https://bugs.eclipse.org/bugs/show_bug.cgi?id=333646 there is everything you need to get it running. All the needed code is in the attachments, there isn't anything missing as far as I can tell.

Martin W. wrote on Thu, 17 November 2011 19:47

Furthermore, is it possible without any modifications whatsoever in the bundle where the aspect should be woven? (again, I'm assuming it should work)


It depends. As long as you don't introduce new dependencies it should work w/o any modification. As soon as you're introducing new dependencies to the target bundle that aren't covered by the package import/require bundle headers the problems will start. You can work around that with dynamic package imports but that could cause other troubles and it means you have to touch the target bundles at least once. Furthermore it will limit you as you have to follow a certain package strucutre (unless you just import everything).

With the latest OSGi spec 4.3 (please correct me if I'm wrong) a new approach has been introduced using a whiteboard pattern for bytecode weaving. You can find some information about that on http://www.slideshare.net/mfrancis/bytecode-weaving-in-osgi-enhance-your-classes-not-your-dependency-graph-tim-ward. I haven't tried to use it with Virgo yet so I can't tell you if it's already working or not. But it provides a way to change the manifest of the target platform w/o touching the bundle itself so I think that's pretty much what you're looking for. Afaik the Apache Aries project is using it for their persistence stuff so you may find some example code there on how to use it.
Re: How do you set up Spring AOP/AspectJ/Virgo [message #757499 is a reply to message #757421] Fri, 18 November 2011 16:19 Go to previous messageGo to next message
Martin W. is currently offline Martin W.
Messages: 8
Registered: June 2011
Junior Member
Hi,

Thank you very much Frieder for you reply! Smile

I tried the example code from bug 333646. Unfortunatly it does not work for me.

First of all I had make a few modifications: (see attachments)
- changed required version numbers of imports in MANIFESTs (removed the maximum)
- added some imports (quite some of them are probably not required, but I suppose they won't harm)
- replaced MyAspect.aj with an equivalent MyAspect.java (somehow with the .aj I got compile errors)

Still, it seems that the Aspect is not detected correctly, when the bundles are deployed. I'm deploying the bundles manually (i.e. no PAR or plan), and i tried different orders.

There is no mentioning of aop.xml or MyAspect anywhere in the logs (also attached). Like sven and rob mentioned earlier, I don't see any output on System.out from MyAspect either. Sad

Also, in the logs it says
[2011-11-18 17:09:05.209] INFO  region-dm-12                 o.e.virgo.kernel.dmfragment.internal.LoadTimeWeaverPostProcessor  Load-time weaving not enabled for bundle 'org.example.spring_1.0.0 [117]', 


I'd appreciate if somebody could take a look into it...

All the best,
Martin


EDIT 2011-11-21: In the previous upload I forgot the @Aspect annotation. Uploaded a new version, but still the same behaviour... Sad

[Updated on: Mon, 21 November 2011 09:33]

Report message to a moderator

icon10.gif  Re: How do you set up Spring AOP/AspectJ/Virgo [message #757781 is a reply to message #757499] Mon, 21 November 2011 16:10 Go to previous messageGo to next message
Martin W. is currently offline Martin W.
Messages: 8
Registered: June 2011
Junior Member
Alright guys. It finally works.... Very Happy I'll post details later on.

All the best,
Martin
icon1.gif  Re: How do you set up Spring AOP/AspectJ/Virgo [message #757787 is a reply to message #757781] Mon, 21 November 2011 16:54 Go to previous messageGo to next message
Martin W. is currently offline Martin W.
Messages: 8
Registered: June 2011
Junior Member
Hello everybody,

Here's what I did to set up Virgo (3.0.1) for weaving of aspects across bundles.

Good luck! Razz

Best regards,
Martin

------------------------------------------------------

1. Download Virgo 3.0.1.RELEASE and unpack it.
http://www.eclipse.org/virgo/download/

2. Download Equinox SDK (currently the latest release is 3.7.1)
http://download.eclipse.org/equinox/

3. From the Equinox download copy org.eclipse.equinox.weaving.hook_1.0.100.v20110502.jar to Virgo's /lib folder

Edit /lib/org.eclipse.virgo.kernel.launch.properties:

Under launcher.bundles add the following entry: (note: all lines except the last for a property should end with comma and backslash)
file:lib/org.eclipse.equinox.weaving.hook_1.0.100.v20110502.jar


4. From the Equinox download copy org.eclipse.equinox.weaving.aspectj_1.0.1.v20110502.jar to Virgo's /repository/usr folder

Edit /config/org.eclipse.virgo.kernel.userregion.properties:

Under packageImports add the following entry: (note: all lines except the last for a property should end with comma and backslash)
org.eclipse.equinox.service.weaving


Under initialArtifacts add the following entry: (note: separate with comma only --- no backslash)
repository:bundle/org.eclipse.equinox.weaving.aspectj


5. Start Virgo

6. Deploy the org.example.tracking bundle
(this is the aspect bundle)
(attached to this message)

7. Deploy the org.example.spring bundle
(attached to this message)

[Updated on: Mon, 21 November 2011 16:55]

Report message to a moderator

Re: How do you set up Spring AOP/AspectJ/Virgo [message #758247 is a reply to message #757787] Tue, 22 November 2011 12:34 Go to previous messageGo to next message
Frieder Heugel is currently offline Frieder Heugel
Messages: 61
Registered: October 2010
Location: Basel, CH
Member
Hi

@Martin
Nice to hear that you've solved your issue.

@Virgo maintainers
Would it be possible to put that into the FAQ of the Wiki or somewhere else in the documentation?
icon5.gif  Re: How do you set up Spring AOP/AspectJ/Virgo [message #758544 is a reply to message #758247] Wed, 23 November 2011 15:52 Go to previous messageGo to next message
Martin W. is currently offline Martin W.
Messages: 8
Registered: June 2011
Junior Member
Hello again,

Unfortunately my snap bundles don't work anymore now. The error message is a little bit weird... The bundle can't be resolved because of a missing import package.
constraint: <Import-Package: org.eclipse.equinox.weaving.hooks; version="[0.0.0,0.0.0]">


In the attached log-file I see (in lines 42/43)
Import-Package: [...] org.eclipse.equinox.weaving.hooks
 ;version="[0.0.0, 0.0.0]"

Why would it import a package for version 0.0.0 of a bundle that isn't mentioned anywhere it the MANIFEST of the snap or the host bundle? How are these imports made up?

When I replace /lib/org.eclipse.virgo.kernel.launch.properties and /config/org.eclipse.virgo.kernel.userregion.properties with the original files everything works fine. Except that my aspect bundles don't work anymore... Sad

Is this a known issue? Should I file a bug?

Thanks in advance for your replies...

All the best,
Martin
  • Attachment: log.zip
    (Size: 31.57KB, Downloaded 87 times)
Re: How do you set up Spring AOP/AspectJ/Virgo [message #870530 is a reply to message #676583] Tue, 08 May 2012 14:46 Go to previous messageGo to next message
Martin W. is currently offline Martin W.
Messages: 10
Registered: June 2011
Junior Member
Matt Fellows wrote on Sun, 05 June 2011 09:43
it is as simple this:

1. Add
<context:load-time-weaver aspectj-weaving="on"/>
to your application context as Glyn mentioned


Hi everybody,

Matt's instructions work for me - but only as long as class and aspect are in the same bundle. But what do I have to do to weave across bundles?

I have exported the package of my aspect, but the aspect is not woven in the other bundle. Adding <context:load-time-weaver aspectj-weaving="on"/> in the other bundle - to "switch to ServerLoadTimeWeaver" - does not help.

Unfortunately my solution (posted on Mon, 21 November 2011) does not work with Virgo 3.5.0.M4.

Can anybody give me some advice?

EDIT: I attached the sample projects from my previous solution --- modified to use the libraries as of Virgo version 3.5.0.M4.

All the best,
Martin

[Updated on: Tue, 08 May 2012 14:56]

Report message to a moderator

Re: How do you set up Spring AOP/AspectJ/Virgo [message #1007342 is a reply to message #870530] Tue, 05 February 2013 15:14 Go to previous message
Eduardo Frazão is currently offline Eduardo Frazão
Messages: 118
Registered: January 2012
Senior Member
Hi Matt. I was with a problem like yours. Here is working with the setup described in this post: http://www.eclipse.org/forums/index.php/t/451783/
Previous Topic:Configurations and service.factoryPid
Next Topic:Is it normal to get a dump for invalid char in manifest.mf?
Goto Forum:
  


Current Time: Mon Sep 22 10:12:50 GMT 2014

Powered by FUDForum. Page generated in 0.03621 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software