简体   繁体   中英

Understanding of Load-time weaving with AspectJ in the Spring Framework for application deployed on Tomcat

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM