使用spring的AOP功能实现方法增强的时候,如果被增强的类实现了某个接口则注解的方式无法实现方法的增强,并且会报错

学习springAOP遇到的问题:

** 分别使用XML和注解的方式,使用spring的AOP功能实现方法增强的时候,如果被增强的类实现了某个接口则注解的方式无法实现方法的增强,并且会报错:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.wy.service.impl.AccountImpl' available
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:346)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:337)
	at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1123)
	at com.wy.test.main(test.java:18)

注解实现的具体代码如下:

1、需要增强的类

1.1 接口

package com.wy.service;
public interface Account {
    void save();
    void withdraw();
}

1.2 实现类

package com.wy.service.impl;
import com.wy.service.Account;
import org.springframework.stereotype.Service;

//如果在这里不实现Account接口,则不会报错,也能实现方法的增强
@Service
public class AccountImpl{
    public void save() {
        System.out.println("存钱");
    }
    public void withdraw() {
        System.out.println("取钱");
    }
}

2、切面类

package com.wy.log;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class Logger {
    //切入点表达式
    @Pointcut("execution(* com.wy.service.impl.*.*(..))")
    public void pointcut(){}

    @Before("pointcut()")
    public void printLog(){
        System.out.println("打印日志");
    }
}

3、主配置类

package com.wy.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

@Configuration
@ComponentScan("com.wy")
@EnableAspectJAutoProxy
public class AppConfig {
}

3、测试类

package com.wy;

import com.wy.config.AppConfig;
import com.wy.service.Account;
import com.wy.service.impl.AccountImpl;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {
    public static void main(String[] args) {

        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfig.class);

        AccountImpl accountService = ctx.getBean(AccountImpl.class);
        accountService.withdraw();
    }
}

执行上述代码时就会报错。

XML实现的具体代码:

实现代码与注解相同,只不过需要去除所有类上的注解,XML配置如下:(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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
    
   <bean id = "accountService" class="com.wy.service.impl.AccountImpl"/>
    <bean id="log" class="com.wy.log.Logger"/>
    <aop:config>
        <aop:pointcut id="pt" expression="execution(* com.wy.service.impl.*.*(..))"/>
        <aop:aspect id="logAdvice" ref="log">
            <aop:before method="printLog" pointcut-ref="pt"/>
        </aop:aspect>
    </aop:config>
</beans>

项目的依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.wy</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>
    </dependencies>
</project>
个人觉得可能是Aspect使用的是CGLIB代理,未使用jdk的动态代理,所以不能处理接口。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值