AOP加载时间的编织

AOP加载时间的编织

本文介绍了Spring Boot AOP加载时间的编织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道出了什么问题,但是AOP在我的Spring Boot(v1.1.6)安装程序中似乎不起作用.

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);
    }
}

在方面类中

@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
  }
}

我也有像这样的META-INF/aop.xml

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>

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

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.

我做错什么了吗?

推荐答案

在代码中添加以下更改,它应该可以工作.不需要创建aop.xml.这些更改会将外观添加到容器中并进行编织.

Add below changes to your code and it should work. Do not need to create aop.xml. These changes will add the aspect to container and also weave it.

阅读-启用Spring AOP或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 {
     ...
 }

这篇关于Spring Boot AOP加载时间的编织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 23:35