// Launcher.java
import com.rippleworks.spring.Macbeth;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Launcher {
public static void main(String... args){
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(ProjectConfig.class);
Macbeth drama = context.getBean(Macbeth.class);
drama.perform();
context.close();
}
}
// Macbeth.java
package com.rippleworks.spring;
import com.rippleworks.spring.interfaces.Performance;
import org.springframework.stereotype.Component;
@Component
public class Macbeth implements Performance {
@Override
public void perform() {
System.out.println("Macbeth playing..");
}
}
// Audience.java
package com.rippleworks.spring;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class Audience {
@Pointcut("execution(* com.rippleworks.spring.Macbeth.perform(..))")
public void performance(){}
@Before("performance()")
public void silentCellphones() {
System.out.println("Cell phones are now silent.");
}
}
// ProjectConfig.java
@Configuration
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.rippleworks.spring")
public class ProjectConfig {
// If I comment out this method, I wont get a NotSuchBeanDefExp
// for class Macbeth. Weird :-(
@Bean
public Audience audience() {
return new Audience();
}
}
Bonjour, Je n'arrive pas à utiliser AspectJ Style AOP au printemps (IntelliJ IDEA Ultimate Version). Lorsque vous n'utilisez pas les aspects, la sortie est comme prévu. Mais lorsque je déclare l'audience de classe Aspect en tant que bean, j'obtiens NoSuchBeanDefinitionException pour la classe Macbeth.NoSuchBeanDefinitionException lors de la déclaration de plusieurs beans
27 août 2017 09:38:39 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh INFO: Rafraîchissant org.spring[email protected]799f7e29: Date de démarrage [Sun août 27 09:38:39 IST 2017]; racine de contexte hiérarchie Exception dans le thread « principal » org.springframework.beans.factory.NoSuchBeanDefinitionException: Non haricots de qualification de type « com.rippleworks.spring.Macbeth » disponible à org.springframework.beans.factory.support. DefaultListableBeanFactory.getBean (DefaultListableBeanFactory.java:353) à org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean (DefaultListableBeanFactory.java:340) à org.springframework.context.support.AbstractApplicationContext.getBean (AbstractApplicationContext. java: 1090) à Launcher.main (Launcher.java:10)
Merci pour votre temps.
pouvez-vous essayer de vous déclarer Macbeth sans '@ Component' et au lieu d'initialiser comme un' @ Bean' dans votre 'ProjectConfig' – sfat
J'ai essayé aussi. même exception. –
essayez d'utiliser '@EnableAspectJAutoProxy (proxyTargetClass = true)' alors. Il a à voir avec aop et ne pas avoir d'interfaces. AOP essaie généralement de passer par des interfaces et Spring essayera de faire la même chose en utilisant '@ EnableAspectJAutoproxy'. – sfat