没有可用的'concert.PerformanceImp'类型的合格Bean [英] No qualifying bean of type 'concert.PerformanceImp' available

查看:73
本文介绍了没有可用的'concert.PerformanceImp'类型的合格Bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然是Spring Framework的初学者,因此我尝试在Spring AOP中编写有关介绍"的程序,但编译时遇到错误.请在包concert中的类下面找到:

I am still a beginner in Spring Framework so I tried to code a program about "introduction" in Spring AOP but I am facing an error while compiling. Please find below the classes in the package concert:

PerformanceImp.java

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

Performance.java

package concert;

public interface Performance {
    public void perform();
}

Encoreable.java

Encoreable.java

package concert;

public interface Encoreable {
    void performEncore();
}

DefaultEncoreable.java

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

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

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

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,它将设置为目标接口而不是实现.如果您删除EnableAspectJAutoProxy,您会看到代码可以正常工作.

You cannot access the implementation (PerformanceImp) by default, because you enabled AOP, which sets to target interfaces instead of implementation. If you would remove EnableAspectJAutoProxy, you would see the code would work fine.

要进一步了解AOP定位的工作原理,请查看以下

To understand a bit more about how AOP targeting works, take a look at this Spring Documentation

Spring AOP也可以使用CGLIB代理.这是代理的必要条件 类而不是接口.如果业务是默认情况下,则使用CGLIB 对象未实现接口.因为这是一个好习惯 编程接口而不是类;正常的商务舱 将实现一个或多个业务接口.有可能 在您需要的那些(非常罕见)情况下,强制使用CGLIB 告知未在接口或您所在位置声明的方法 需要将代理对象作为具体类型传递给方法.

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时,请使用接口.
  2. 启用AOP来定位具体类.
  1. Take the interface when trying to get the bean from the ApplicationContext.
  2. Enable AOP to target concrete classes instead.

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

To do this point #2, modify your annotation as follows:

@EnableAspectJAutoProxy(proxyTargetClass = true)

这篇关于没有可用的'concert.PerformanceImp'类型的合格Bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆