【问题标题】:Spring Boot AOP load time weavingSpring Boot AOP 加载时间编织
【发布时间】:2014-09-05 15:59:05
【问题描述】:

不确定出了什么问题,但 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]

但建议没有任何反应。它不会着火。

我做错了吗?

【问题讨论】:

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


    【解决方案1】:

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

    public privileged aspect MyAspect {
        // ...
    }
    

    但是AspectJ documentation 说:

    限制:注释样式不支持特权方面。

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

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题。在我的例子中,我必须用注解@Component 来注解我的切面类。

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

      以下链接有一个对我有帮助的 spring boot AOP 示例

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

      【讨论】:

        【解决方案3】:

        将以下更改添加到您的代码中,它应该可以工作。不需要创建 aop.xml。这些更改会将方面添加到容器并编织它。

        阅读 -- Enable Spring AOP or AspectJ

        @Configuration
        @ComponentScan
        @EnableJpaRepositories
        @EnableTransactionManagement
        @EnableAutoConfiguration
        @EnableCaching
        @EnableAspectJAutoProxy -- Use this instead of @EnableLoadTimeWeaving
        
        public class Application {
            public static void main(String[] args) {
                SpringApplication.run(Application.class, args);
            }
        }
        
        @Aspect
         @Component/@Configurable -- Both of them work
         public class MyAspect {
             ...
         }
        

        【讨论】:

          猜你喜欢
          • 2019-05-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多