Unable to run aspectj example from Spring in action 4th

房东的猫 提交于 2019-12-12 03:09:49

问题


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

Performance

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

Configuration

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

Main

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

Dependencies

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

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


回答1:


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

  • 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.


来源:https://stackoverflow.com/questions/29435191/unable-to-run-aspectj-example-from-spring-in-action-4th

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!