org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type '全类名'

该错误是在练习Spring - aop的时候出现的

先说总结有利于大家还有没有必要往后面在继续看下去:错误出在获取bean的时候,我获取的是该接口的实现类类型,解决办法就是获取该接口的类型,不要获取该接口的实现类在这里插入图片描述
项目架构
在这里插入图片描述
Maven

<dependencies>
        <!--   spring核心包     -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>
        <!--aop 需要的包-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.1</version>
        </dependency>

        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>

        <!-- spring 核心包 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.0.RELEASE</version>
        </dependency>

        <!-- 日志包-->
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>

    </dependencies>

xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 扫描com.stone.dome包下的所有注解 -->
    <context:component-scan base-package="com.stone.aopdome"/>

    <!--    开启基于注解的aop 功能-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>


</beans>

Operation类

@Component
public interface Operation
{
    //  加法
     double add(double i,double j);
    //  减法
     double subtract(double i,double j);
    //  乘法
     double mul(double i,double j);
    //  除法
      double div(double i,double j);
}

Operation目标类

@Component
public class OperationImpl implements Operation
{
    @Override
    public double add(double i, double j)
    {
        return i+j;
    }

    @Override
    public double subtract(double i, double j)
    {
        return i-j;
    }

    @Override
    public double mul(double i, double j)
    {
        return i*j;
    }

    @Override
    public double div(double i, double j)
    {
        return i/j;
    }
}

OperationProxy 目标实现类

@Aspect
@Component
public class OperationProxy
{
    //   execution切入点表达式 (访问修饰符 返回值类型 目标执行的方法)
    @Before("execution(public double com.stone.aopdome.operation.impl.OperationImpl.add(double,double))")
    public void logbefore(){
        System.out.println("我是前置通知");
    }

    @After("execution(public double com.stone.aopdome.operation.impl.OperationImpl.add(double,double))")
    public void logAfter(){
        System.out.println("我是后置通知");
    }


    @AfterThrowing("execution(public double com.stone.aopdome.operation.impl.OperationImpl.add(double,double))")
    public void logException(){
        System.out.println("我是异常通知");
    }
    @AfterReturning("execution(public double com.stone.aopdome.operation.impl.OperationImpl.add(double,double))")
    public void logFinal(){
        System.out.println("我是最终通知");
    }
}

SpringAopTest 测试类

public class SpringAopTest
{
    private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");

    @Test
    public void testaop1(){
        Operation bean = ioc.getBean(OperationImpl.class);
        double add = bean.add(12.0, 20.0);
        System.out.println(add);

    }
}

个人本来以为是缺少了什么Maven依赖造成的 ,可原因并不是如此,而是我获取bean的时候,是获取了该类的实现类,解决错误的办法就是在获取bean的时候,不要获取目标对象的实现类,直接获取该接口类就行

错误代码
在这里插入图片描述

错误修改
在这里插入图片描述

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
和提到了org.springframework.beans.factory.NoSuchBeanDefinitionException异常,这个异常是在使用Spring自动注入时可能遇到的问题。异常信息中指出找不到匹配的bean类。这种情况经常发生在尝试注入的bean类没有定义的情况下。解决方法是在需要注入的类上添加@Component、@Repository、@Service或@Controller等注解,或者在其子类上添加这些注解。如果注入的是抽象类或接口,不需要在抽象类或接口上添加这些注解,只需要在其子类上添加注解即可。 中提供了一个示例,其中class A中的B类型的变量b未能找到相应的bean类,因此抛出了该异常。要解决这个问题,应该先创建B类,并在B类上添加相应的注解,然后再进行注入操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [异常spring 异常org.springframework.beans.factory.NoSuchBeanDefinit](https://blog.csdn.net/qq_40739049/article/details/83092986)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [异常org.springframework.beans.factory.NoSuchBeanDefinitionException](https://blog.csdn.net/u013473691/article/details/52790227)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值