0

I am using Load-time weaving on Tomcat server for transaction management in spring with aspcetj mode and I am finding hard to understand it conceptually.

My transaction manager configuration is as follows

<bean id="transactionManagerRW" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactoryReadWrite" />
    </bean>
<tx:annotation-driven  mode="aspectj" transaction-manager="transactionManagerRW"/>

For load time weaving to work I have added following dependencies in my code

compile (
        'org.aspectj:aspectjrt:1.8.4',
        'org.aspectj:aspectjweaver:1.8.4',
        'org.springframework:spring-aspects:5.0.0.RELEASE',
)

My service class is as follows

package com.temp.request.service
class ServiceImpl {

        @Transactional
        public CreateRequest CreateRequest (){
           // some business logic here
        } 

          @Transactional
        public void deleteRequests(){
           // some business logic here
        }   

   } 

context.xml is as follows

 <Context path="/">
        <Loader loaderClass="org.springframework.instrument.classloading.tomcat.TomcatInstrumentableClassLoader"/>
    </Context> 

and aop.xml is as follows

<aspectj>
    <weaver options="-verbose -showWeaveInfo">
         <include within="com.temp.request.service..*"/>
    </weaver>   
</aspectj>

According to spring's documentation AnnotationTransactionAspect is created and woven in to target object during load time weaving (In this case object of ServiceImpl class ) but how even though I have not specified aspect name (AnnotationTransactionAspect in this case) in aop.xml spring got to know that during AnnotationTransactionAspect need to be woven in my ServiceImpl class?

In spring tuttorial they have specified ProfilingAspect in aop.xml in this link https://docs.spring.io/spring/docs/3.0.0.M3/reference/html/ch08s08.html as follows

<aspectj>

    <weaver>

        <!-- only weave classes in our application-specific packages -->
        <include within="foo.*"/>

    </weaver>

    <aspects>

        <!-- weave in just this aspect -->        
        <aspect name="foo.ProfilingAspect"/>

    </aspects>

  </aspectj>

In spring documentation it is specified as : (https://docs.spring.io/spring/docs/3.0.0.M4/spring-framework-reference/html/ch10s05.html)

Annotate your classes (and optionally your classes' methods) with the @Transactional annotation, and then you link (weave) your application with the org.springframework.transaction.aspectj.AnnotationTransactionAspect defined in the spring-aspects.jar file. The aspect must also be configured with a transaction manager. You can of course use the Spring Framework's IoC container to take care of dependency-injecting the aspect. The simplest way to configure the transaction management aspect is to use the element and specify the mode attribute to asepctj

Here line highlighted in bold says you link (weave) your application with the org.springframework.transaction.aspectj.AnnotationTransactionAspect Edit : While during deploying application I can see following line on the console

weaveinfo Join point 'method-execution(com.temp.request.service.CreateRequest com.temp.request.service.ServiceImpl ()' in Type 'com.temp.request.service.ServiceImpl' (ServiceImpl.java:5) advised by around advice from 'org.springframework.transaction.aspectj.AnnotationTransactionAspect'

So this line proves that AnnotationTransactionAspect is woven successfully in ServiceImpl class

My basic questions are :

How to link (weave) my application with the AnnotationTransactionAspect of spring?

How Spring got to know that it needs to weave AnnotationTransactionAspect in ServiceImpl class?

Whether aop.xml is necessary for successful load time weaving of AnnotationTransactionAspect ?

Pawan Patil
  • 1,067
  • 5
  • 20
  • 46
  • You have annotations @Transcational in your service, so it is how it knows. – Krzysztof Cichocki Sep 13 '19 at 09:57
  • Oooh - combination of Gradle, Spring XML config and even JavaEE XML config. That's a very bohemian combination of the very, very old (no, obsolete) and the rather new. Fascinating – Boris the Spider Sep 13 '19 at 10:09
  • @KrzysztofCichocki what is ht use of aop.xml then ? from aop.xml spring got to know in which classes it should weave AnnotationTransactionAspect. – Pawan Patil Sep 13 '19 at 11:00
  • You find more information about `@Transactional` [here](https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#aop-ajlib-other). The aspect comes from `spring-aspects` in this case. – kriegaex Sep 19 '19 at 12:15

1 Answers1

0

Spring has built-in support for the annotation @Transactional, for more info, you could study the source code:

https://github.com/spring-projects/spring-framework/blob/master/spring-tx/src/main/java/org/springframework/transaction/annotation/Transactional.java

and documentation:

https://github.com/spring-projects/spring-framework/blob/master/spring-tx/src/main/java/org/springframework/transaction/annotation/Transactional.java

Krzysztof Cichocki
  • 6,294
  • 1
  • 16
  • 32
  • My question is how spring is guessing the location (package name) of methods of class which contains Transactinal annotation? Is it because of aop.xml? – Pawan Patil Sep 13 '19 at 12:54
  • I'm not sure, if it loads it from the aop.xml in your case, I don't have experience with that. Try to rename this file or comment it out, then check if it will be resolved or not, that should answer your question. – Krzysztof Cichocki Sep 13 '19 at 13:00
  • @PawanPatil you question makes no sense. You can configured **load** time weaving - there is no guessing. When the classloader **loads** a class it is checked, and if neccessary fiddled with. – Boris the Spider Sep 14 '19 at 06:40
  • You said when classloader loads the class it is checked so does it mean every class is checked for weaving available on classpath? – Pawan Patil Sep 14 '19 at 06:42
  • @PawanPatil yes. Every single class. Which is why configuring the filters in `aop.xml` is helpful - a decision can be made faster. Although, all that XML processing isn't free either... – Boris the Spider Sep 14 '19 at 06:49
  • Do you have any reference documentation link regarding this ? @Transactional annotations is checked for weaving while loading of every single class? – Pawan Patil Sep 14 '19 at 06:51
  • Yes, the documentation for AspectJ covers this in detail. – Boris the Spider Sep 14 '19 at 07:05
  • Correction, @KrzysztofCichocki: AspectJ is **never** proxy-based, Spring AOP **always** is. You should not confuse or mix up the two concepts so as not to confuse the OP. – kriegaex Sep 19 '19 at 12:10