Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot AOP load time weaving

Not sure what is going wrong, but AOP just doesn't seem to be working in my setup with spring boot (v1.1.6).

@Configuration
@ComponentScan
@EnableJpaRepositories
@EnableTransactionManagement
@EnableAutoConfiguration
@EnableCaching
@EnableLoadTimeWeaving
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

And in the aspect class

@Aspect
public class MyAspect {
  @AfterReturning(pointcut = "execution(private * com.myapp.service.MyService.test(..)) && args(str1,str2)", argNames = "str1,str2")
    public void advice(String str1, String str2) throws IOException {
        System.out.println("Advising after returning");
    }
}

In the service class that needs the advice

@Service
public class MyService {
  public void test(String str1, String str2) throws IOException {
    System.out.println("Test method in service");
    //rest of the implementation
  }
}

I also have a META-INF/aop.xml like so

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
    <weaver>
        <!-- only weave classes in our application-specific packages -->
        <include within="com.myapp.*"/>
    </weaver>

    <aspects>
        <!-- weave in just this aspect -->
        <aspect name="com.myapp.aspect.MyAspect"/>
    </aspects>

</aspectj>

When I run the application with -javaagent:path/to/spring-instrument-4.1.0.RELEASE.jar

I get this message on the console

2014-09-05 08:42:12.500  INFO 65053 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
[AppClassLoader@58644d46] warning javax.* types are not being woven because the weaver option '-Xset:weaveJavaxPackages=true' has not been specified
2014-09-05 08:42:13.114  INFO 65053 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2014-09-05 08:42:13.156  INFO 65053 --- [           main] o.s.b.a.e.jmx.EndpointMBeanExporter      : Registering beans for JMX exposure on startup
[AppClassLoader@58644d46] error can't determine implemented interfaces of missing type org.springframework.security.config.http.SessionCreationPolicy
when weaving type org.springframework.boot.actuate.autoconfigure.ManagementServerProperties$Security
when weaving classes 
when weaving 
 [Xlint:cantFindType]
[AppClassLoader@58644d46] error can't determine implemented interfaces of missing type org.springframework.security.config.http.SessionCreationPolicy
when weaving type org.springframework.boot.actuate.autoconfigure.ManagementServerProperties$Security
when weaving classes 
when weaving 
 [Xlint:cantFindType]

Nothing happens with the advice though. It won't fire.

Am I doing something wrong?

like image 226
sat Avatar asked Oct 21 '22 02:10

sat


2 Answers

In order to advise private methods you need to use a privileged aspect:

public privileged aspect MyAspect {
    // ...
}

But the AspectJ documentation says:

Limitations: Privileged aspects are not supported by the annotation style.

So please use native syntax, not @AspectJ style. Before you do that, though, test if non-privileged, annotation-style aspects work as expected with public methods in order to exclude other reasons for your aspects being woven.

like image 100
kriegaex Avatar answered Oct 22 '22 21:10

kriegaex


I had the same problem. In my case I had to annotate my aspect class with the annotation @Component.

 @Aspect
 @Component
 public class MyAspect {
     ...
 }

The following link has a spring boot AOP example that helped me

https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-aop

like image 33
C.L.S Avatar answered Oct 22 '22 22:10

C.L.S