0

I have an external jar file which inside only one AspectJ class.

@Aspect
public class SecurityAspect {

   @Pointcut("execution(* *(..)) && @annotation(external.package.Secure)")
 public void doCheck() {}

   @Before("doCheck()")
 public void applyCheck(JoinPoint joinPoint) {
       //sth...
   }

}

I want to trigger that Aspect using the @Secure annotation in a Spring MVC controller

@Secure
@RequestMapping(value = "/test", method = RequestMethod.GET)
public void test() {
  System.out.println("test");
}

The aspects-config.xml has all encessary for considering aspects on my project.

    <aop:aspectj-autoproxy>
     <aop:include name="log" /> (non external)
     <aop:include name="sec" />
    </aop:aspectj-autoproxy>

    <bean id="log"
    class="internal.package.LogAspect"
    factory-method="aspectOf" />

    <bean id="sec"
    class="external.package.SecurityAspect"
    factory-method="aspectOf" />

Eclipse do recognize that something is binded to test() but when I startup the server, Spring cannot find class external.package.SecurityAspect

Caused by: java.lang.ClassNotFoundException: external.package.SecurityAspect

In my application I already have a non external Aspect that makes log for utility and moving the Security Aspect into the Project's package is working fine.

The most comprehensive (for me) questions I came across:

this(1) but I'm not sure that's totally correct since Eclipse do recognize that also in external Jar... but maybe is my misunderstanding;

this(2) It makes sense! I removed my external library from maven, removed any pointers on application-context.xml (right?) imported by that interface, startup and... Error creating bean with name 'log' defined in file [path/to/aspects-config.xml]: No matching factory method found: factory method 'aspectOf()'. Check that a method with the specified name exists and that it is static. (why do complains about log aspect?)

this(3) talking about <context:load-time-weaver /> but it seems not working once added on my application context xml.

THis issue is only if I use EXTERNAL JAR not in the same package.

Can someone better clarify if it is actually not possible (this(3)) or possible make external Aspect to import in projects as a third party library?

Andrea Grimandi
  • 631
  • 2
  • 8
  • 32
  • 1
    The `aspectOf` should be used for aspects written in the traditional AspectJ syntax (the `aspect` type). With an `@Aspect` annotated class you should not use the factory method and just create an instance of the bean. – M. Deinum Jul 13 '18 at 10:06
  • Did not expected that this particular actually solved my problem (incredible O_O) you're awesome, thanks – Andrea Grimandi Jul 13 '18 at 10:29

1 Answers1

0

M. Deinum in comments below noticed that factory-method="aspectOf" usage is not necessary with non aspect types (then for all my @Aspect it is not).

Removed that, startup, no exceptions and now I have a working aspect.

Andrea Grimandi
  • 631
  • 2
  • 8
  • 32