简体   繁体   中英

Third party JAR containing AspectJ pointcut in Spring MVC application

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. 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?

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.

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