当前位置: 首页 > 知识库问答 >
问题:

声明多个bean时的NoSuchBeanDefinitionException

栾烨华
2023-03-14
// 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();
    }
}

你好,Iam在spring(IntelliJ IDEA终极版)中使用AspectJ风格的AOP时遇到了麻烦。不使用方面时,输出与预期相同。但是当我将方面类受众声明为Bean时,我得到了Macbeth类的NoSuchBeanDefinitionException。

2017年8月27日上午9:38:39 org.springframework.context.annotationconfigapplicationcontext准备refresh信息:刷新org.springframework.context.annotation.annotationconfigapplicationcontext@799f7e29:启动日期[Sun 8月27日09:38:39 IST 2017];线程“main”org.springframework.beans.factory.nosuchBeanDefinitionException:在org.springframework.beans.factory.support.defaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:353),在org.springframework.beans.factory.support.defaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:340),在main(launcher.java:10)

谢谢你抽出时间。

共有1个答案

李睿
2023-03-14

试图直接在建议类型上提供切入点。是否可以直接在@before中使用切入点表达式进行检查。

@Before("execution(* com.rippleworks.spring.Macbeth.perform(..))")
public void silentCellphones() {
    System.out.println("Cell phones are now silent.");
}
 类似资料:
  • 我正在对这些注释使用基本集成测试: 运行测试时,出现以下错误: 该错误是由以下注释引起的:SpringBootTest和DataMongoTest,包括BootstrapWith,如下所示: 我需要继续使用SpringBootTest进行SpringBootTest。WebEnvironment。RANDOM\u端口,但我还需要来自DataMongoTest的嵌入式mongodb 有什么建议吗?

  • 问题内容: 我想知道以下情况是否可能。出于测试目的,我希望在应用程序上下文中为不同的测试声明不同的模拟类。这些是使用Jersey REST客户端的验收测试。有没有办法在运行时动态声明bean?Spring是否有API允许在加载上下文后更改应用程序上下文? 问题答案: 关于第一个问题,你可以在运行时通过BeanDefinitionRegistry.registerBeanDefinition()方法

  • 这在这个回答中出现了,并且: 在Apple LLVM 9.1.0 CLANG-902-0.39.2中,的引用第一个并打印“1”。 GCC 8.2不接受此源文本。,抱怨:“错误:以前声明为'static‘的变量重新声明为'extern'”。 C 2018 6.2.2 4规定: 由于有两个先验声明,因此以下每个“if”子句的条件为真,第一个用于第一个先验声明,第二个用于第二个先验声明: ……如果前面声

  • 问题内容: 我要实现的目标是创建一个包含多个功能的模块。 module.js: 我的问题是,这是一个对象类型,而这是一个URL字符串,但是当我遇到该问题时,它总是抱怨该类型是错误的。 在这种情况下,如何声明多个module.exports? 问题答案: 您可以执行以下操作: 要不就: 然后在调用脚本中:

  • 问题内容: 在我的Spring-Boot-App中,我想根据(未加载的)spring-profiles有条件地声明一个Bean。 条件: 到目前为止,我的解决方案(有效): 有没有更优雅(更短)的方式来解释这种情况? 特别是我想在这里摆脱Spring Expression Language的使用。 问题答案: 从Spring 5.1.4(Spring Boot 2.1.2中包含)开始,可以在配置文