50424

Spring AOP aspect used in a separate module

I have an aspect com.x.NiceAspect in one maven project module, and a class com.x.NiceClass in a separate maven module. Those modules have the same POM parent, together creating one project.

The thing I am trying to achieve is to have a universal aspect that I can include in all my maven modules in this project.

Now the pointcut in the NiceAspect is i.e. <strong>execution(* com.x...set(..))</strong> meaning all setter methods in mine package.

I want this aspect to run with the second module, especially to join NiceClass

How can I achieve this?

Moreover suppose that the Aspect wants an argument of a class that's defined in that second module, then I end up with a circular dependency..

Any help appreciated

Kind regards,

x.

Answer1:

    <li>

    I want this aspect to run with the second module, especially to join NiceClass

    If the pointcut matches a method from "another" module, the aspect will apply. You need to do nothing special, besides setting your dependencies correctly. e.g have your AOP configuration globally imported.

    </li> <li>

    Moreover suppose that the Aspect wants an argument of a class that's defined in that second module, then I end up with a circular dependency..

    The aspect dependency on the class argument should not result in circular dependency, since you have a parent POM that connects the two, and at runtime both of the modules are in a classpath.

    </li> </ul>

    In general, it is recommended to follow Aspect structure as described by Spring documentation => create an overall SystemArchitecture aspect, and then add more narrowed scope aspects as needed:

    @Aspect public class SystemArchitecture { /** * A join point is in the web layer if the method is defined * in a type in the com.xyz.someapp.web package or any sub-package * under that. */ @Pointcut("within(com.xyz.someapp.web..*)") public void inWebLayer() {} /** * A join point is in the service layer if the method is defined * in a type in the com.xyz.someapp.service package or any sub-package * under that. */ @Pointcut("within(com.xyz.someapp.service..*)") public void inServiceLayer() {} /** * A join point is in the data access layer if the method is defined * in a type in the com.xyz.someapp.dao package or any sub-package * under that. */ @Pointcut("within(com.xyz.someapp.dao..*)") public void inDataAccessLayer() {} /** * A business service is the execution of any method defined on a service * interface. This definition assumes that interfaces are placed in the * "service" package, and that implementation types are in sub-packages. * * If you group service interfaces by functional area (for example, * in packages com.xyz.someapp.abc.service and com.xyz.def.service) then * the pointcut expression "execution(* com.xyz.someapp..service.*.*(..))" * could be used instead. * * Alternatively, you can write the expression using the 'bean' * PCD, like so "bean(*Service)". (This assumes that you have * named your Spring service beans in a consistent fashion.) */ @Pointcut("execution(* com.xyz.someapp.service.*.*(..))") public void businessService() {} /** * A data access operation is the execution of any method defined on a * dao interface. This definition assumes that interfaces are placed in the * "dao" package, and that implementation types are in sub-packages. */ @Pointcut("execution(* com.xyz.someapp.dao.*.*(..))") public void dataAccessOperation() {} }

    This way any future aspects you may need, can reuse the over all System Architecture approach as well as live together with the actual module they are applied to.

    Answer2:

    If you have all your projects declared as sub projects in the main pom the one containing the aspect (and thus the aspect itself) will be available to the other two, and basically any of the subprojects will have as dependency the other subprojects.

    You will not have a circular dependency due to packaging:

      <li>

      if you package any of your prject as an executable jar, then in order for it to run you need to add the other two subbrojects' jars to the first one's classpath when u run it

      </li> <li>

      if you package any of your subprojects as a war (and the other two as jars) then when packaging with maven the other two subproject's jars will be added to the war's lib folder effectively making them available to the war's classpath at runtime.

      </li> </ul>

Recommend

  • Interaction with python's matplotlib figure: assign value to selected features
  • Java logging across multiple threads
  • $_POST as a function parameter [duplicate]
  • Ext JS, change grid column configs default values globally
  • Do commands run from current directory in a shell script?
  • Is it expensive to create the Thread object or to actually start the thread?
  • Importing matplotlib on Ubuntu
  • Should a C# struct have only read-only properties
  • Whats the best way of persisting data to Isolated Storage on Windows Phone 7?
  • Imports in __init__.py and `import as` statement
  • Tinymce strips attributes on submit
  • python - calculate orthographic similarity between words of a list
  • What version of Java should I use with Cassandra 2.0?
  • CS1703: In Xamarin.Droid, should I use the .Net Standard windowsruntime.dll located in Mono.Framewor
  • Why does Sencha 2 only work in Webkit browsers?
  • Programatically open file in visual studio
  • Cordova Apache wrong module path
  • Haskell: function composition with anonymous/lambda function
  • Creating a C++ function that calls other Lua function
  • Is it better to use the “hidden” CSS attribute or fetch each set of new images?
  • runtime error when linking ffmpeg libraries in qt creator
  • If I am creating a simple client server application in IntelliJ, how should this work?
  • Why isn't my “Fizz Buzz” test in R working?
  • Ruby on Rails App deployed to heroku showing “We're sorry, but something went wrong”
  • Validate jQuery plugin, field not required
  • Detecting null parameter in preprocessor macro
  • Android Studio Can't Find tools.jar
  • Android Studio 1.3 RC3. Google Play services out of date. Requires 7571000 but found 6774470
  • How do I include a SWC in an AS2 Flash project?
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • Breeze - Deleted Items nav properties bug
  • Asynchronous UI Testing in Xcode With Swift
  • Projection media query: browser support and workarounds?
  • How can I use Kendo UI with Razor?
  • ActionScript 2 vs ActionScript 3 performance
  • php design question - will a Helper help here?
  • AngularJs get employee from factory
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4