学习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>