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 ?
Aucun commentaire:
Enregistrer un commentaire