Spring Boot AOP 加载时间编织

标签 spring aspectj spring-boot spring-aop load-time-weaving

不知道出了什么问题,但 AOP 似乎在我的 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);
    }
}

在方面类
@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");
    }
}

在需要建议的服务类中
@Service
public class MyService {
  public void test(String str1, String str2) throws IOException {
    System.out.println("Test method in service");
    //rest of the implementation
  }
}

我也有这样的 META-INF/aop.xml
<!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>

当我使用 -javaagent:path/to/spring-instrument-4.1.0.RELEASE.jar 运行应用程序时

我在控制台上收到此消息
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]

但是,建议没有任何 react 。它不会着火。

难道我做错了什么?

最佳答案

为了建议私有(private)方法,您需要使用 特权 方面:

public privileged aspect MyAspect {
    // ...
}

但是 AspectJ documentation说:

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



所以请使用 native 语法,而不是@AspectJ 风格。但是,在您这样做之前,请测试非特权、注释样式的方面是否与公共(public)方法按预期工作,以便排除您的方面被编织的其他原因。

关于Spring Boot AOP 加载时间编织,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25689814/

相关文章:

java.lang.IllegalStateException : No thread-bound request found, 方面的异常

spring-boot - Spring Boot 后台作业

使用 MockMvc、MockHttpServletRequestBuilder::with user over https 进行 Spring 安全测试

java - 为什么oldCartRemovalJob 被放置在<custom-name>commercewebservices 中?

java - Spring Data JPA 空指针异常

java - 烧烤条形码生成器不使用网络应用程序显示条形码编号

java - 了解有效的对象创建

java - 覆盖 AspectJ 中的方法参数

java - 在这种情况下性能优于 AspectJ 的方面框架?

java - Spring命令设计模式的默认实现