gpt4 book ai didi

java - 没有可用的 'concert.PerformanceImp' 类型的合格 bean

转载 作者:行者123 更新时间:2023-12-02 02:09:01 24 4
gpt4 key购买 nike

我还是 Spring Framework 的初学者,所以我尝试在 Spring AOP 中编写一个有关“简介”的程序,但在编译时遇到错误。请在下面的concert包中找到类:

PerformanceImp.java

package concert;

import org.springframework.stereotype.Component;

@Component
public class PerformanceImp implements Performance {
public void perform() {
System.out.println("This is the performance function");
}
}

Performance.java

package concert;

public interface Performance {
public void perform();
}

Encoreable.java

package concert;

public interface Encoreable {
void performEncore();
}

DefaultEncoreable.java

package concert;

import org.springframework.stereotype.Component;

@Component
public class DefaultEncoreable implements Encoreable {
public void performEncore() {
System.out.println("This is the performEncore function");
}
}

EncoreableIntroducer.java

package concert;
import org.aspectj.lang.annotation.Aspect;

import org.aspectj.lang.annotation.DeclareParents;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class EncoreableIntroducer {
@DeclareParents(value="concert.Performance+",
defaultImpl=DefaultEncoreable.class)
public static Encoreable encoreable;
}

ConcertConfig.java

package concert;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

@Configuration
@EnableAspectJAutoProxy
@ComponentScan("concert")
public class ConcertConfig {

}

主类:

Main.java

package concert;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(ConcertConfig.class);
PerformanceImp pi = (PerformanceImp) context.getBean(PerformanceImp.class);
((Encoreable) pi).performEncore();
pi.perform();
}
}

我收到错误:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'concert.PerformanceImp' available

请问有什么帮助吗?

最佳答案

默认情况下,您无法访问实现 (PerformanceImp),因为您启用了 AOP,它设置为目标接口(interface)而不是实现。如果您删除 EnableAspectJAutoProxy,您会发现代码可以正常工作。

要更多地了解 AOP 定位的工作原理,请查看此 Spring Documentation

Spring AOP can also use CGLIB proxies. This is necessary to proxy classes rather than interfaces. CGLIB is used by default if a business object does not implement an interface. As it is good practice to program to interfaces rather than classes; business classes normally will implement one or more business interfaces. It is possible to force the use of CGLIB, in those (hopefully rare) cases where you need to advise a method that is not declared on an interface, or where you need to pass a proxied object to a method as a concrete type.

所以你有两个选择:

  1. 尝试从 ApplicationContext 获取 bean 时获取接口(interface)。
  2. 启用 AOP 来定位具体类。

要执行第 2 点,请按如下方式修改注释:

@EnableAspectJAutoProxy(proxyTargetClass = true)

关于java - 没有可用的 'concert.PerformanceImp' 类型的合格 bean,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50308666/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com