user6321158
user6321158

Reputation:

NoSuchBeanDefinitionException when declaring more than one bean

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

Hello, Iam having trouble using AspectJ Style AOP within spring ( IntelliJ IDEA Ultimate Version). When not using aspects, the output is as expected. But when I declare Aspect class Audience as a Bean, I get NoSuchBeanDefinitionException for Macbeth class.

Aug 27, 2017 9:38:39 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@799f7e29: startup date [Sun Aug 27 09:38:39 IST 2017]; root of context hierarchy Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.rippleworks.spring.Macbeth' available at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1090) at Launcher.main(Launcher.java:10)

Thank you for your time.

Upvotes: 1

Views: 219

Answers (1)

Tamiliniyan Devarajan
Tamiliniyan Devarajan

Reputation: 86

Trying to provide pointcut directly on advice type. Can you check by using your pointcut expression directly within @before.

@Before("execution(* com.rippleworks.spring.Macbeth.perform(..))")
public void silentCellphones() {
    System.out.println("Cell phones are now silent.");
}

Upvotes: 0

Related Questions