5

何が問題なのかはわかりませんが、Spring Boot (v1.1.6) を使用したセットアップで AOP が機能していないようです。

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

アドバイスしても何も起こりません。発火しません。

私は何か間違ったことをしていますか?

4

3 に答える 3

1

プライベート メソッドにアドバイスするには、特権アスペクトを使用する必要があります。

public privileged aspect MyAspect {
    // ...
}

しかし、AspectJ のドキュメントには次のように書かれています。

制限事項:特権アスペクトは注釈スタイルではサポートされていません。

そのため、@AspectJ スタイルではなく、ネイティブ構文を使用してください。ただし、それを行う前に、特権のない注釈スタイルのアスペクトが public メソッドで期待どおりに機能するかどうかをテストして、アスペクトが織り込まれている他の理由を除外します。

于 2014-09-08T09:15:09.440 に答える