无法从 Spring 运行 aspectj 示例,第 4 步 [英] Unable to run aspectj example from Spring in action 4th

查看:33
本文介绍了无法从 Spring 运行 aspectj 示例,第 4 步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对一个方面和其他协同工作的类有以下定义.

I have the following definition of an aspect and other classes that are co-working.

package concert;

public aspect CriticAspect {
    public CriticAspect() {}

    pointcut performance(): execution(* perform(..));


    afterReturning() : performance() {
        System.out.println(criticismEngine.getCriticism());
    }

    private CriticismEngine criticismEngine;

    public void setCriticismEngine(CriticismEngine criticismEngine) {
        this.criticismEngine = criticismEngine;
    }

}

CriticismEngine

package concert;

public interface CriticismEngine {
    String getCriticism();
}

CriticismEngineImpl

package concert;

public class CriticismEngineImpl implements CriticismEngine {
    public CriticismEngineImpl() {}

    public String getCriticism() {
        int i = (int) (Math.random() * criticismPool.length);
        return criticismPool[i];
    }

    // injected
    private String[] criticismPool;
    public void setCriticismPool(String[] criticismPool) {
        this.criticismPool = criticismPool;
    }
}

性能

package concert;

public interface Performance {
    void perform();
}

PerformanceImple

package concert;

public class Concert implements Performance {
    @Override
    public void perform() {
        System.out.println("Playing a concert!");
    }
}

配置

package concert.config;

import concert.Concert;
import concert.CriticAspect;
import concert.CriticismEngine;
import concert.CriticismEngineImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@EnableAspectJAutoProxy()
public class ApplicationConfiguration {
    @Bean
    public CriticAspect criticAspect() {
        return CriticAspect.aspectOf();
    }

    @Bean
    public CriticismEngine criticismEngine() {
        CriticismEngineImpl criticismEngine = new CriticismEngineImpl();
        String[] criticisms = { "Worst performance ever!",
                                "I laughed, I cried, then I realized I was at the wrong show.",
                                "A must see show!" };
        criticismEngine.setCriticismPool(criticisms);
        return criticismEngine;
    }

    @Bean
    public Concert concert() {
        return new Concert();
    }
}

主要

package concert;

import concert.config.ApplicationConfiguration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfiguration.class);
        Performance concert = context.getBean("concert", Performance.class);
        concert.perform();
    }
}

依赖关系

compile "org.springframework:spring-context:${springVersion}"
compile "org.springframework:spring-aop:${springAopVersion}"
compile "org.aspectj:aspectjrt:${aspectJRuntimeVersion}"
compile "org.aspectj:aspectjweaver:${aspectJWeaverVersion}"

但是intellij 说它找不到CriticAspect.我如何运行这个例子?还是我做错了什么?

But intellij says it cannot find CriticAspect. How I can run this example ? Or I'm doing something wrong ?

推荐答案

IMO @EnableAspectJAutoProxy 对于本机 AspectJ 方面没有意义.它仅适用于 Spring AOP 方面.所以你有两个选择:

IMO @EnableAspectJAutoProxy does not make sense for a native AspectJ aspect. It only works for Spring AOP aspects. So you have two options:

  • 要么通过 Ajc(AspectJ 编译器)将原生 AspectJ 方面直接编译到 Spring 应用程序中.在这种情况下,您不需要任何注释来运行方面,只需在运行时在类路径上aspectjrt.jar.
  • 或者您采用 Spring 手册(第 9.8 章)中描述的规范方式,并使用 LTW(加载时编织)进行原生 AspectJ 方面.这可以分别通过 @EnableLoadTimeWeaving 在您的配置中启用.见第 9.8.4 章,在 Spring 框架中使用 AspectJ 进行加载时编织.
  • Either you compile your native AspectJ aspect right into your Spring application via Ajc (AspectJ compiler). In this case you do not need any annotations to get the aspects running, only aspectjrt.jar on the classpath during runtime.
  • Or you go the canonical way described in the Spring manual (chapter 9.8) and use LTW (load-time weaving) for native AspectJ aspects. This can be enabled in your configuration via @EnableLoadTimeWeaving or <context:load-time-weaver/>, respectively. See chapter 9.8.4, Load-time weaving with AspectJ in the Spring Framework.

这篇关于无法从 Spring 运行 aspectj 示例,第 4 步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆